Jump to content

Randomly select images from a request


ttmt

Recommended Posts

 

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");
?>

Link to comment
Share on other sites

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>

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.