I'm a novice programmer with a very small knowledge of loops and arrays.
Maybe someone can help me clean up my code.
Goal: I have a table with the fields: image, link, imagetype, and description. There are ten different types of image-types defined by a value 1-10. For each image-type, there may be 5-20 images.
I want to select one random image(row) of each image-type in the table. Then, I want to shuffle the results so that the images are displayed in a random order too.
Here is my programming vomit
I receive the following error:<?
// Connect to the database
mysql_connect ('localhost', 'name', 'password') ;
mysql_select_db (name');
// Edit this number to however many links you want displaying
$num_displayed = 1 ;
// Select random rows from the database
$result1 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 1) ORDER BY RAND() LIMIT $num_displayed");
$result2 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 2) ORDER BY RAND() LIMIT $num_displayed");
$result3 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 3) ORDER BY RAND() LIMIT $num_displayed");
$result4 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 4) ORDER BY RAND() LIMIT $num_displayed");
$result5 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 5) ORDER BY RAND() LIMIT $num_displayed");
$result6 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 6) ORDER BY RAND() LIMIT $num_displayed");
$result7 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 7) ORDER BY RAND() LIMIT $num_displayed");
$result8 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 8 ) ORDER BY RAND() LIMIT $num_displayed");
$result9 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 9) ORDER BY RAND() LIMIT $num_displayed");
$result10 = mysql_query ("SELECT * FROM someimages WHERE (imagetype = 10) ORDER BY RAND() LIMIT $num_displayed");
// For all the rows that you selected
$row1 = mysql_fetch_array($result1);
$row2 = mysql_fetch_array($result2);
$row3 = mysql_fetch_array($result3);
$row4 = mysql_fetch_array($result4);
$row5 = mysql_fetch_array($result5);
$row6 = mysql_fetch_array($result6);
$row7 = mysql_fetch_array($result7);
$row8 = mysql_fetch_array($result8);
$row9 = mysql_fetch_array($result9);
$row10 = mysql_fetch_array($result10);
// $placement = array ("$row1", "$row2", "$row3", "$row4", "$row5", "$row6", "$row7", "$row8", "$row9", "$row10");
$number = array ($row1, $row2, $row3, $row4, $row5, $row6, $row7, $row8, $row9, $row10);
srand ((float)microtime()*1000000);
shuffle ($number);
while (list (, $number) = each ($number)) {
// Display them to the screen...
?>
<a href="<? print $number["link"]; ?>"><img="http://www.foobar.com/images/<? print $number["image"]; ?>"></a>
<?
}
?>
I would really appreciate it if someone can help.Warning: Variable passed to each() is not an array or object in /foobar.com/httpdocs/test.php on line 39