proctk Posted July 30, 2007 Share Posted July 30, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/ Share on other sites More sharing options...
hitman6003 Posted July 30, 2007 Share Posted July 30, 2007 How do you determine which is the next photo? Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311455 Share on other sites More sharing options...
proctk Posted July 30, 2007 Author Share Posted July 30, 2007 when The image is clicked it advances to the next photo in the query. like a slide show Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311462 Share on other sites More sharing options...
hitman6003 Posted July 30, 2007 Share Posted July 30, 2007 How do you determine which photo is the next one to display? Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311464 Share on other sites More sharing options...
proctk Posted July 30, 2007 Author Share Posted July 30, 2007 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']; / Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311485 Share on other sites More sharing options...
hitman6003 Posted July 30, 2007 Share Posted July 30, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311488 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 Your question is part of my problem. I thought about assigning the image_ids to an array then some set the image url to somehow advance to the next photo Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311493 Share on other sites More sharing options...
hitman6003 Posted July 31, 2007 Share Posted July 31, 2007 Is the next photo the next id? Is it the next id in the album? Is there a date field? If so, would the next photo be the next newest one? Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311496 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 it would be the next image_id returned from the query. the sort order does not matter. Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311500 Share on other sites More sharing options...
hitman6003 Posted July 31, 2007 Share Posted July 31, 2007 it would be the next image_id returned from the query By what query? Your query only returns one row. Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311503 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311508 Share on other sites More sharing options...
hitman6003 Posted July 31, 2007 Share Posted July 31, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311512 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311517 Share on other sites More sharing options...
hitman6003 Posted July 31, 2007 Share Posted July 31, 2007 "if" is probably a reserved word.... change it to something different, "imf" or something like that, then change all of the "if.col_name" to "imf.col_name" as well. Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311520 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311525 Share on other sites More sharing options...
hitman6003 Posted July 31, 2007 Share Posted July 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311526 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 Alright, my mistack on the query issue hopefully last question; how do I get the image name in to the img url thank you Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311528 Share on other sites More sharing options...
hitman6003 Posted July 31, 2007 Share Posted July 31, 2007 I'm assuming you mean in the img tag...change: <img src="../user_images/<?php echo $foundPhoto; ?>" /> to <img src="../user_images/<?php echo $photo['image_name']; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311533 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 thank you for the help and explanations. Would it be hard to change the foreach to loop though the image_ids and just start over when it ends and start back at image one. thank cheers Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311536 Share on other sites More sharing options...
hitman6003 Posted July 31, 2007 Share Posted July 31, 2007 Change the foreach loop...I think this will work... foreach ($ids as $key => $id) { if ($id == $photoId) { $next = ($key + 1 > (count($ids) - 1) ? $ids[0] : $ids[$key + 1]); $prev = ($key - 1 < 0 ? $ids[count($ids)] : $ids[$key - 1]); break; } } Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311538 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 The foreach statement that you wrote for me is not working, when it comes to the end it does not start over. code it be because of the asc in the query thank you Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311544 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 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]); Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311568 Share on other sites More sharing options...
proctk Posted July 31, 2007 Author Share Posted July 31, 2007 the fix was change $ids[0] to $ids[1] Quote Link to comment https://forums.phpfreaks.com/topic/62576-swap-image/#findComment-311576 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.