Jump to content

swap image


proctk

Recommended Posts

The below code displays an image on a page I want to somehow set it up so that when the user clicks the image it advances to the next photo in the query. any ideas how to do this thank you

 

$photoId = $_GET['photoID'];

//get image
$query_get_photo = ("select * from image_files WHERE image_id = '$photoId'");
$get_photo = mysql_query($query_get_photo )or die("SQL Error: $query_get_photo <br>" . mysql_error());

     $photo = mysql_fetch_assoc($get_photo);
     $foundPhoto = $photo['image_name'];
     $imageOwner = $photo['user_id'];
// Get album Author
    $query_get_author = ("select * from users WHERE user_id = '$imageOwner'");
    $get_author = mysql_query($query_get_author )or die("SQL Error: $query_get_author <br>" . mysql_error());

     $author = mysql_fetch_assoc($get_author);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div id="outer">
            <div id="hdr">
                  <!--Header-->             
                 <?php include('../design/topbanner.php'); ?>
</div>
            
        <div id="bar">
        <!--Subheader-->
        <h3 style="margin-left:250px; color:#848484; padding-top:10px;"><?php echo $_SESSION['fname'].' '. $_SESSION['lname']; ?></h3>
            </div>
        
        <div id="bodyblock">
          <!--body Lock-->
          
            <div id="l-col">
                        
            <?php include('../searchMembers.php'); ?>
                    </div>
            
          <div id="cont">
         <!--<p style="font-size:10px; padding:5px;"><a href="photos.php">Back to Albums</a></p>-->
          <div class="imageFull">
   <p style="font-size:10px; color:#848484; font-style:italic;">Create Date: <?php echo $photo['create_date']; ?> </p>
    <p style="text-align:center"><img src="../user_images/<?php echo $foundPhoto; ?>" /></p>
            
        <p style="text-align:right; padding-right:20px; margin-top:5px; font-size:10px;">From the Album:<br />
        <?php echo $photo['album']; ?> <span style="color:#000000">by</span> <?php echo $author['first_name'].' '.$author['last_name']; ?></p>    
        <p><?php echo $photo['details']; ?></p>

Link to comment
Share on other sites

I could easily assign the values to an array.

 

I'm not sure how to get it to advance to the next photo when the image is clicked

//get image
$query_get_photo = ("select * from image_files WHERE album = '$album' AND user_id = '$owner_id'");
$get_photo = mysql_query($query_get_photo )or die("SQL Error: $query_get_photo <br>" . mysql_error());

  while($photo = mysql_fetch_assoc($get_photo);
 $image_id = $photo['image_id'];
/ 

Link to comment
Share on other sites

All you have to do is create a link of the current image...or add a "Next" link.

 

But you still haven't answered my question...how do you determine which image should be displayed as the "next" image?

 

Here is a one query example of your query above:

 

<?php

$photoId = $_GET['photoID'];

$query = "SELECT if.image_name, if.album, if.photo, if.create_date, if.details, u.first_name, u.last_name " .
     "FROM image_files if " .
     "  LEFT JOIN users u ON if.user_id = u.user_id " .
     "WHERE image_id = " . $photoId;

$result = mysql_query($query)or die("SQL Error: $query <br>" . mysql_error());
$photo = mysql_fetch_assoc($result);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div id="outer">
<div id="hdr">
	  <!--Header-->             
	 <?php include('../design/topbanner.php'); ?>
</div>
            
<div id="bar">
	<!--Subheader-->
	<h3 style="margin-left:250px; color:#848484; padding-top:10px;"><?php echo $_SESSION['fname'].' '. $_SESSION['lname']; ?></h3>
</div>
        
<div id="bodyblock">
  <!--body Lock-->
  
	<div id="l-col">
		<?php include('../searchMembers.php'); ?>
	</div>

	<div id="cont">
		<div class="imageFull">
			<p style="font-size:10px; color:#848484; font-style:italic;">
				Create Date: <?php echo $photo['create_date']; ?>
			</p>

			<p style="text-align:center">
				<img src="../user_images/<?php echo $foundPhoto; ?>" />
			</p>

			<p style="text-align:right; padding-right:20px; margin-top:5px; font-size:10px;">
				From the Album:<br />
				<?php echo $photo['album']; ?>
				<span style="color:#000000">by</span>
				<?php echo $photo['first_name'] . ' ' . $photo['last_name']; ?>
			</p>    

			<p>
				<?php echo $photo['details']; ?>
			</p>

Link to comment
Share on other sites

This query could return more then one row

//get image
$query_get_photo = ("select * from image_files WHERE album = '$album' AND user_id = '$owner_id'");
$get_photo = mysql_query($query_get_photo )or die("SQL Error: $query_get_photo <br>" . mysql_error());

  while($photo = mysql_fetch_assoc($get_photo){
 $image_id = $photo['image_id'];

}

Link to comment
Share on other sites

An example:

 

<?php

$photoId = $_GET['photoID'];

$query = "SELECT if.image_name, if.album, if.photo, if.create_date, if.details, u.first_name, u.last_name " .
     "FROM image_files if " .
     "  LEFT JOIN users u ON if.user_id = u.user_id " .
     "WHERE image_id = " . $photoId;

$result = mysql_query($query)or die("SQL Error: $query <br>" . mysql_error());
$photo = mysql_fetch_assoc($result);

$query = "SELECT image_id " .
	 "FROM image_files " .
	 "WHERE album = '" . $album . "' AND user_id = " . $owner_id . " " .
	 "ORDER BY image_id ASC";

$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$ids[] = $row['image_id'];
}

foreach ($ids as $key => $id) {
if ($id == $photoId) {
	$next = $ids[$key + 1];
	$prev = $ids[$key - 1];
	break;
}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div id="outer">
<div id="hdr">
	  <!--Header-->             
	 <?php include('../design/topbanner.php'); ?>
</div>
            
<div id="bar">
	<!--Subheader-->
	<h3 style="margin-left:250px; color:#848484; padding-top:10px;"><?php echo $_SESSION['fname'].' '. $_SESSION['lname']; ?></h3>
</div>
        
<div id="bodyblock">
  <!--body Lock-->
  
	<div id="l-col">
		<?php include('../searchMembers.php'); ?>
	</div>

	<div id="cont">
		<div class="imageFull">
			<p style="font-size:10px; color:#848484; font-style:italic;">
				Create Date: <?php echo $photo['create_date']; ?>
			</p>

			<p style="text-align:center">
				<img src="../user_images/<?php echo $foundPhoto; ?>" />
			</p>

			<p style="text-align:right; padding-right:20px; margin-top:5px; font-size:10px;">
				From the Album:<br />
				<?php echo $photo['album']; ?>
				<span style="color:#000000">by</span>
				<?php echo $photo['first_name'] . ' ' . $photo['last_name']; ?>
			</p>    

			<p>
				<?php echo $photo['details']; ?>
			</p>

			<p>
				<table width="100%">
					<tr>
						<td style="text-align: left; width: 50%;"><a href="<?php echo $_SERVER['PHP_SELF'] . "&?photoID=" . $prev; ?>">Prev</a></td>
						<td style="text-align: right; width: 50%;"><a href="<?php echo $_SERVER['PHP_SELF'] . "&?photoID=" . $next; ?>">Next</a></td>
					</tr>
				</table>
			</p>

Link to comment
Share on other sites

thank you for the help. As you can tell I'm a rookie at this stuff. I'm having a hard time following your query as I have not worked with left join.  I'm not able to figure out why this error is occuring

 

error

SQL Error: SELECT if.image_name, if.album, if.photo, if.create_date, if.details, u.first_name, u.last_name FROM image_files if LEFT JOIN users u ON if.user_id = u.user_id WHERE image_id = 184

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if LEFT JOIN users u ON if.user_id = u.user_id WHERE image_id = 184' at line 1

 

$query = "SELECT if.image_name, if.album, if.photo, if.create_date, if.details, u.first_name, u.last_name " .
     "FROM image_files if " .
     "  LEFT JOIN users u ON if.user_id = u.user_id " .
     "WHERE image_id = " . $photoId;

Link to comment
Share on other sites

your correct.  I made the change and the error went away, however The query returns nothing my next question does your example script of to different examples. as there are duplicate variables $result, $query.

 

thank you

Link to comment
Share on other sites

Since we use the first query with the first mysql_query call, it becomes useless, and we can reuse that variable name.  Same for $result...we get the values out of the first $result with mysql_fetch_assoc, which means we don't need it any more and can reuse the $result variable name.

 

If you aren't getting the expected results be sure to plug the queries into mysql query browser or phpMyAdmin and see if they are simply not matching anything, or if there is an error somewhere

Link to comment
Share on other sites

HI I think the issue is this from the below statement

count($ids) - 1 it the first image_id is 7. the ids run from 7-10.  the link for the last image returns an image id of 6.

 

thank you

 

$next = ($key + 1 > (count($ids) - 1) ? $ids[0] : $ids[$key + 1]);

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.