Jump to content

random generation


canabatz

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.