aftab_jii Posted April 15, 2010 Share Posted April 15, 2010 Hi, I am trying to customize a script that shows random images.. i am obviously doing something wrong because the script does not show any images can someone help me? <?php //this is the file for all the function function getRandomImage($dir,$type='random') { global $errors,$seed; if (is_dir($dir)) { $fd = opendir($dir); $images = array(); while (($part = @readdir($fd)) == true) { if ( @eregi("(gif|jpg|png|jpeg)$",$part) ) { $images[] = $part; } } // adding this in case you want to return the image array if ($type == 'all') return $images; if ($seed !== true) { mt_srand ((double) microtime() * 1000000); $seed = true; } $key = mt_rand (0,sizeof($images)-1); return $dir . $images[$key]; } else { $errors[] = $dir.' is not a directory'; return false; } } $query = "SELECT * FROM images WHERE is_published=1"; $result = mysql_query ($query); // Run the query while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { $image_id = $row['image_id']; $image_filename = "uploaded_images/" . $image_id . ".jpg"; $image = getRandomImage($image_filename); echo 'The value of image is: ' . $image . '<br \>'; echo "<img src='$image_filename' width='519' height='353' border='0' usemap='#Map' \>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/198678-customizing-a-php-script/ Share on other sites More sharing options...
jcbones Posted April 15, 2010 Share Posted April 15, 2010 You are attempting to use the function for a use it wasn't designed for. This function is design to pass a directory full of images to it, and it will select a random image from that directory. You are trying to give it an image name, but I don't know how you expect it to return a random image from that. Instead try: <?php //this is the file for all the function function getRandomImage($dir,$type='random') { global $errors,$seed; if (is_dir($dir)) { $fd = opendir($dir); $images = array(); while (($part = @readdir($fd)) == true) { if ( @eregi("(gif|jpg|png|jpeg)$",$part) ) { $images[] = $part; } } // adding this in case you want to return the image array if ($type == 'all') return $images; if ($seed !== true) { mt_srand ((double) microtime() * 1000000); $seed = true; } $key = mt_rand (0,sizeof($images)-1); return $dir . $images[$key]; } else { $errors[] = $dir.' is not a directory'; return false; } } $image = getRandomImage('uploaded_images/'); echo "<img src='$image' width='519' height='353' border='0' usemap='#Map' \>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/198678-customizing-a-php-script/#findComment-1042647 Share on other sites More sharing options...
TeddyKiller Posted April 15, 2010 Share Posted April 15, 2010 It also depends on what your trying to customize the script into.. Quote Link to comment https://forums.phpfreaks.com/topic/198678-customizing-a-php-script/#findComment-1042657 Share on other sites More sharing options...
teamatomic Posted April 15, 2010 Share Posted April 15, 2010 When you define the function you have: function getRandomImage($dir,$type='random') it should be: function getRandomImage($dir,$type) Then when you call the function you use something like: getRandomImage($imgdir,'random') Next problem: The type=random does not matter cause if it not "all" the function returns one random image. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198678-customizing-a-php-script/#findComment-1042710 Share on other sites More sharing options...
aftab_jii Posted April 16, 2010 Author Share Posted April 16, 2010 @jcbones so where could i find something to show images that has is_published=1 @teamatomic your idea worked half ways it showed all the images on one page instead of as a sideshow Quote Link to comment https://forums.phpfreaks.com/topic/198678-customizing-a-php-script/#findComment-1042978 Share on other sites More sharing options...
jcbones Posted April 16, 2010 Share Posted April 16, 2010 Un-tested, but you can try: <?php $query = "SELECT * FROM images WHERE is_published!=1"; $result = mysql_query ($query); // Run the query while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { $images_not_published[] = $row['image_id']; } function getRandomImage($dir,$type='random') { global $errors,$seed,$images_not_published; if (is_dir($dir)) { $fd = opendir($dir); $images = array(); while (($part = @readdir($fd)) == true) { if ( @eregi("(gif|jpg|png|jpeg)$",$part) ) { $images[] = $part; } } $images = array_values(array_diff($images,$images_not_published)); // adding this in case you want to return the image array if ($type == 'all') return $images; if ($seed !== true) { mt_srand ((double) microtime() * 1000000); $seed = true; } $key = mt_rand (0,sizeof($images)-1); return $dir . $images[$key]; } else { $errors[] = $dir.' is not a directory'; return false; } } $image = getRandomImage('uploaded_images/'); echo "<img src='$image' width='519' height='353' border='0' usemap='#Map' \>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/198678-customizing-a-php-script/#findComment-1043067 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.