ttmt Posted June 10, 2009 Share Posted June 10, 2009 I'm working on a simple photo gallery. I have the uploading, scaling of the images and placing them in image and thumb directories all working. The images are group into different sections. At the moment I'm selecting all the thumbnails from the different sections and displaying them. My problem is I want to display random thumbnails from the different sections. So this code selects the thumbnails relating to each section and displays them. <?php function thumbs($image_id){ global $image_Dir; global $image_thumb; $query = "SELECT p.photo_id, p.filename, p.caption FROM keyword_photo kp, photos p WHERE kp.key_id = $image_id AND kp.photo_id = p.photo_id"; /*LIMIT 7";*/ $result = mysql_query($query); confirm_query($result);// checks query worked while($row = mysql_fetch_array($result)){ $thumb = $image_thumb . $row[1]; $image = $image_Dir.$row[1]; echo "<li><img src=\"$thumb\" /></li>"; } } ?> What I want to do is select all the thumbnails for a section but then randomly display 7 of them. Hope this makes sense and someone can help. <?php require_once("includes/connection.php"); require_once("includes/functions.php"); ?> <?php $image_Dir = "images/";// image directory $image_thumb = $image_Dir . "thumbs/"; //thumbs directory $images_arr = array(); // $query = "SELECT key_id, words FROM keywords"; $result = mysql_query($query); confirm_query($result);// checks query worked ?> <?php function thumbs($image_id){ global $image_Dir; global $image_thumb; $query = "SELECT p.photo_id, p.filename, p.caption FROM keyword_photo kp, photos p WHERE kp.key_id = $image_id AND kp.photo_id = p.photo_id"; /*LIMIT 7";*/ $result = mysql_query($query); confirm_query($result);// checks query worked while($row = mysql_fetch_array($result)){ $thumb = $image_thumb . $row[1]; $image = $image_Dir.$row[1]; //echo "<li><img src=\"$thumb\" /></li>"; } } ?> <?php function oddEven($m){ if($m%2){ echo "section"; }else{ echo "section two"; } } ?> <?php include("includes/header.php"); ?> <div id="content"> <?php while($row = mysql_fetch_array($result)){ ?> <div class="<?php oddEven(current($row));?>"> <a href="gallery.php?photo_id=<?php echo $row[0]?>"> <h4> <?php echo $row[1]; ?> </h4> <div class="images"> <ul> <?php thumbs($row[0]);?> </ul> </div><!--images--> </a> </div> <?php } ?> </div><!--content--> <?php include("includes/footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/161662-randomly-select-images-from-a-request/ Share on other sites More sharing options...
ttmt Posted June 10, 2009 Author Share Posted June 10, 2009 I've created a simple version to try and explain better. I have one table with one column 'filename' which holds a reference to the images in images/thumbs. The query selects all from the filename name column and displays the images. At the moment there are 7 images in the DB. I want to display 4 that are randomly picked from the 7. <?php require_once("includes/connection.php"); require_once("includes/functions.php"); ?> <?php $image_Dir = "images/"; $image_thumb = $image_Dir . "thumbs/"; // ?> <ul> <?php $query = "SELECT filename FROM photos"; $result = mysql_query($query); confirm_query($result); while($row = mysql_fetch_array($result)){ $thumb = $image_thumb . $row[0]; echo "<li><img src=\"$thumb\" /></li>"; } ?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/161662-randomly-select-images-from-a-request/#findComment-853051 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.