hey guys I have few lines like the example below <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> <a href="link"><img src="link"></a> With php I would like to choose random 5 lines and display them. Help will be appreciated. Thank you
Put your links in a txt file called links.txt and run the code below <?php $dirs = file("links.txt"); shuffle($dirs); $folders = explode("\n", $dirs[0]); $dir_folder = $folders[0]; echo $dir_folder; ?> PHP:
<?php $dirs = file("links.txt"); shuffle($dirs); for ($i = 0; $i < 5; $i++) { echo $dirs[$i]; } ?> Code (markup):
I would not use a file to do this - I would use an array that you preset or populate with a database. Example of Array I would recommend to populate with a database - as it would be much more simple to just to ORDER BY rand in the query Array Way <?php $array_of_links = array( array('url' => 'http://www.myurl.com/1/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'), array('url' => 'http://www.myurl.com/2/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'), array('url' => 'http://www.myurl.com/3/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'), array('url' => 'http://www.myurl.com/4/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'), array('url' => 'http://www.myurl.com/5/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg') ); // next all you have to do is just get random $randomized_links = shuffle($array_of_links); // to use them foreach( $randomized_links as $link ): echo "<a href='" . $link['url'] . "'>" . "<img src='" . $link['image_src'] . "' alt='" . $link['anchor'] . "'>" . "</a>"; endforeach; ?> Code (markup): Note: This Code May Break I didn't test the code - but you should get the general idea from it.