canabatz Posted December 14, 2009 Share Posted December 14, 2009 hi i need help with generating this numbers from 1-12 i want to generate in random order 60 times in total the numbers 1-12 and generate 5 results from each number. so in the total of the 60 return result i will have 5 time 1 5 time 2 5 time 3 and so on and on to 5 times 12 i want to be able to do it with images 12 images ,5 time each! please point me to the right direction!! thanx Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 14, 2009 Share Posted December 14, 2009 u want to display 12 random images or 12 images five times Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 14, 2009 Share Posted December 14, 2009 Meh, you can do something like this: for ($i=0; $i>=12;$i++) { //echo "5 x $i = ". ($i*5). "<br/>"; echo '<img src="'.($i*5).'.png"/><br/>"; } Either way, you can set it to say or write dynamically using a loop, using 5.png, 10.png, 15.png etc. Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 14, 2009 Author Share Posted December 14, 2009 i want to build a table with 12 columns and 5 rows i want randomly position the images in the cells , on any refresh of the page the images will be in a different cell. there is only 12 images ,not more! thanx Quote Link to comment Share on other sites More sharing options...
raytri Posted December 14, 2009 Share Posted December 14, 2009 You could put the image names into a MYSQL table, and then query it five times using UNION ALL and ORDER BY RAND(): $picturequery=mysql_query("SELECT * from Pictures UNION ALL SELECT * from Pictures UNION ALL SELECT * from Pictures UNION ALL SELECT * from Pictures UNION ALL SELECT * from Pictures ORDER BY RAND()"); while ($picturearray=mysql_fetch_array($picturequery)) {echo $picturearray['PictureName'];} You can probably do the same thing more elegantly using joins, but I'm still trying to wrap my head around join syntax. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 If your images change all the time, than I would suggest using a database like raytri says. You don't need the Union operator though, thats used when you want to join the results from two result sets, and there is no need for that here since you would only need one. If they stay static for the most part, than you could just use an array instead of having to make a database. Similar to what ray said, you could use the shuffle() function which will take an array and make it random. for example $images = array("img1.jpg", "img2.jpg", "img3.jpg"); shuffle($images); foreach($images as $image){ echo $image . "<br />"; } Quote Link to comment Share on other sites More sharing options...
The Eagle Posted December 14, 2009 Share Posted December 14, 2009 I recommend putting them into a MySQL database. Quote Link to comment Share on other sites More sharing options...
raytri Posted December 14, 2009 Share Posted December 14, 2009 If your images change all the time, than I would suggest using a database like raytri says. You don't need the Union operator though, thats used when you want to join the results from two result sets, and there is no need for that here since you would only need one. Except that he wants to take those 12 images and repeat each image 5 times (for a total of 60 images), and have those 60 images display randomly. So he either needs to pull the data five times in MYSQL, or pull it once, duplicate the array four times in PHP, merge those into one combined array, shuffle it and then loop through it. I imagine the latter is more efficient (only one database query instead of five), but the code would be more complicated. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 If your images change all the time, than I would suggest using a database like raytri says. You don't need the Union operator though, thats used when you want to join the results from two result sets, and there is no need for that here since you would only need one. Except that he wants to take those 12 images and repeat each image 5 times (for a total of 60 images), and have those 60 images display randomly. are you suggesting he can't use a simple array to do this? Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 14, 2009 Author Share Posted December 14, 2009 i all ,thanx for the help ,i mange to do it with your help ,and the way Mikesta707 pointed me here is the code ,without database for the moment ,only shuffle the colors ,instead fo images! <table width="250" border="1" cellspacing="0" cellpadding="0"> <tr> <td> <? $images = array("red", "blue", "white", "green", "yellow", "pink", "gray", "brown", "#00F7FF", "orange", "#BAA87E", "#868A08"); $colcounter = 0; $numcols = 4; for ($k=1; $k<=5; $k++){ shuffle($images); echo "<div>"; $i = 0; while($i < 1){ if(($i % 5) == 0){ echo "<br style='clear:both' />"; } foreach($images as $image){ //then display picture echo "<div class='row_block'>"; echo "<table width='20' border='1' cellspacing='0' cellpadding='0' bgcolor='$image'> <tr> <td> </td> </tr> </table> "; echo "</div>";} $i = $i + 1; } echo"</div>"; } ?> </td> </tr> </table> thank you ,thank you! Quote Link to comment Share on other sites More sharing options...
raytri Posted December 14, 2009 Share Posted December 14, 2009 are you suggesting he can't use a simple array to do this? No. I was looking at what you wrote in terms of MYSQL syntax. I'm relatively new to it, so I was thinking two things: 1. You could pull the data once and then manipulate it as a PHP array, but the code would be more complicated than just pulling it already randomly sorted from the database. I was trying to keep the code as simple as possible. 2. UNION ALL is the only way I know to make multiple passes of the same query in MYSQL, short of putting the query inside a loop or writing the query five times. Is there another way to have MYSQL pull five copies of the same query results? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.