adam291086 Posted October 4, 2007 Share Posted October 4, 2007 How can i search through a database and return the highest id number. As i am new it would be appreciated if you can add example code or point me to the relevent website. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/ Share on other sites More sharing options...
shocker-z Posted October 4, 2007 Share Posted October 4, 2007 it would be a bit like this $query=mysql_query("SELECT id FROM table LIMIT 1 ORDER BY id DESC"); $row=mysql_fetch_array($query); echo $row['id']; Regards Liam Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361586 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 Ahh beat me OK option 2 $query=mysql_query("SELECT MAX(id) as maxID FROM `table` LIMIT 1"); $row=mysql_fetch_array($query); echo $row['maxID']; BUT i think you MAYBE doing something bad here if you want the last ID entered after an insert use mysql_insert_id() mysql_query("INSERT INTO mytable (product) values ('kossu')"); printf("Last inserted record has id %d\n", mysql_insert_id()); (assuming ID is an AUTO_INCREMENT ) as if 2 members insert at the same time your get a problem using the highest Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361587 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 Ok guys i am really really stuck. I have a page displaying a photo albums content. I have a next button and all it does is take the Photo Id current displayed and +1. The problem is that it carrys on even when there is no photo. I wanted to find the max number and create an if statement saying If id exceeds the max ID then redirect to start of album. I have tried all method for days. Below is the basic code to display the gallery. Please can you point me in the right direction. <?php include("config.php"); if( !$_GET['photo_id'] ){ $msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view."; // Display error message displayPage($msg); die(); } // get picture details $con; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = " . addslashes($_GET['photo_id']); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); while($row = @mysql_fetch_array($result)) { $photo_title = $row['photo_title']; $photo_loc = $row['photo_location']; $album = $row['album_id']; } ?> <html> <head> <title>Gallery - <?php echo($photo_title); ?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>Gallery - <?php echo($photo_title); ?></td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td> <img src="<?php echo($photo_loc); ?>" /> </td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><a href="gallery.php">View Gallery</a></td> </tr> <tr> <td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td> </tr> <tr> <td><a href="view_photo.php?photo_id=<?php echo ($_GET['photo_id']+1); ?>">Next</a></td> </tr> <tr> <td><a href="view_photo.php?photo_id=<?php echo ($_GET['photo_id']-1); ?>">Previous</a></td> </tr> </table> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361604 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 maybe <?php include("config.php"); if( !isset($_GET['photo_id']) ){ $msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view."; // Display error message displayPage($msg); die(); } // get picture details $con; $id = (int)$_GET['photo_id']; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows == 0) { $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = 1"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); } while($row = @mysql_fetch_array($result)) { $photo_title = $row['photo_title']; $photo_loc = $row['photo_location']; $album = $row['album_id']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361614 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 I have just insert you code and when i click next on the last image the html code is still displayed with no picture. Any ideas Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361633 Share on other sites More sharing options...
jscix Posted October 4, 2007 Share Posted October 4, 2007 Well Man that's what debugging is for ! Echo $Var; print_r($array); Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361635 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 No the idea is that the html code is supposed to run, therefore nothing is displayed as that photo_id dosen't have a picture attached to it . I think i need to run a check to ensure the Photo_id is in the database and if it isn't then return something else. How can i go about this? Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361638 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 i assumed ID 1 has an image.. quick update.. totally untested <?php include("config.php"); // get picture details $con; $id = (int)$_GET['photo_id']; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows > 0) { $row = @mysql_fetch_array($result); $photo_title = $row['photo_title']; $photo_loc = $row['photo_location']; $album = $row['album_id']; //check Next $nid = $id+1; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $nid"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Next = ($num_rows > 0); //check Previous $pid = $id-1; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $pid"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Previous = ($num_rows > 0); ?> <html> <head> <title>Gallery - <?php echo($photo_title); ?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>Gallery - <?php echo($photo_title); ?></td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td> <img src="<?php echo($photo_loc); ?>" /> </td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><a href="gallery.php">View Gallery</a></td> </tr> <tr> <td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td> </tr> <tr> <td> <?php echo ($Previous)?""<a href='view_photo.php?photo_id=$pid'>Previous</a>:"No more"; ?> </td> </tr> <tr> <td> <?php echo ($Next)?""<a href='view_photo.php?photo_id=$nid'>Next</a>:"No more"; ?> </td> </tr> </table> </td> </tr> </table> </body> </html> <?php }else{ $msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view."; // Display error message displayPage($msg); die(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361642 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 Hey thanks for the help, i correct the bsaic stuff, missing () but now i get the error message Parse error: parse error, unexpected T_STRING in /homepages/12/d214897219/htdocs/adam/view_photo.php on line 62 and i dont know how to fix it Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361647 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 updated what () did i miss ? <?php include("config.php"); // get picture details $con; $id = (int)$_GET['photo_id']; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows > 0) { $row = @mysql_fetch_array($result); $photo_title = $row['photo_title']; $photo_loc = $row['photo_location']; $album = $row['album_id']; //check Next $nid = $id+1; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $nid"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Next = ($num_rows > 0):true:false; //check Previous $pid = $id-1; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $pid"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Previous = ($num_rows > 0):true:false; ?> <html> <head> <title>Gallery - <?php echo($photo_title); ?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>Gallery - <?php echo($photo_title); ?></td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td> <img src="<?php echo($photo_loc); ?>" /> </td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><a href="gallery.php">View Gallery</a></td> </tr> <tr> <td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td> </tr> <tr> <td> <?php echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid'>Previous</a>":"No more"; ?> </td> </tr> <tr> <td> <?php echo ($Next === true)?"<a href='view_photo.php?photo_id=$nid'>Next</a>":"No more"; ?> </td> </tr> </table> </td> </tr> </table> </body> </html> <?php }else{ $msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view."; // Display error message displayPage($msg); die(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361651 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 The () you missed are off the start of selecting something from the database. In the lines beginning $spl. I have updated the code and get the error Parse error: parse error, unexpected ':' in /homepages/12/d214897219/htdocs/adam/view_photo.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361652 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 hey I have solved all the problems by removeing $Previous = ($num_rows > 0):true:false; and replacing it with $Previous = ($num_rows > 0); and it all works now. Thanks a lot.. Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361664 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 LOL $Previous = ($num_rows > 0); was on the first post the 2nd should of been $Previous = ($num_rows > 0)?true:false; oh well all is good Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361671 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 I thought this was finished. The code works and stop the next button when there are no more picture to display. The only problem is that instead of only showing the photos in that album it shows all pictures that the database has. How can i correct this? Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361674 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 you lost me, what (doesn't) happen when you do what ? if theirs no next image then their shouldn't be a next button Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361676 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 ok heres a brief description: When you click on view gallery you can a list of thumbnails representing all the different photo albums. If you click on one a load of thumbnails appear showing all the pictures within that album. Then if you click on an individual picture the full image is displayed. Below the full images is the next button. This button should only take you through the pictures within that album. At present the next button takes you through every single photo in the database regardless of the album. Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361681 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 try this, first <?php $con; $id = (int)$_GET['photo_id']; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos LIMIT $id, 1"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); ?> if thats fine then try this <?php $con; $id = (int)$_GET['photo_id']; $aid = (int)($_GET['album_id']); $where = ($aid>0)?" WHERE album_id = $aid":""; $sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $id, 1"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); ?> Links echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid&album _id=$album'>Previous</a>":"No more"; Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361692 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 That hasn't worked. It now clames No photo selected! Please choose an album you would like to view. Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361698 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 can you post the whole code again, so i can see Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361712 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 <?php include("config.php"); // get picture details $con; $id = (int)$_GET['photo_id']; $sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows > 0) { $row = @mysql_fetch_array($result); $photo_title = $row['photo_title']; $photo_loc = $row['photo_location']; $album = $row['album_id']; //check Next $nid = $id+1; $sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $nid"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Next = ($num_rows > 0); //check Previous $pid = $id-1; $sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $pid"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Previous = ($num_rows > 0); ?> <html> <head> <title>Gallery - <?php echo($photo_title); ?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>Gallery - <?php echo($photo_title); ?></td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td> <img src="<?php echo($photo_loc); ?>" /> </td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><a href="gallery.php">View Gallery</a></td> </tr> <tr> <td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td> </tr> <tr> <td> <?php echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid'>Previous</a>":"No more"; ?> </td> </tr> <tr> <td> <?php echo ($Next === true)?"<a href='view_photo.php?photo_id=$nid'>Next</a>":"No more"; ?> </td> </tr> </table> </td> </tr> </table> </body> </html> <?php }else{ $msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view."; // Display error message displayPage($msg); die(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361717 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 you can see whats happen at www.adamplowman.co.uk/gallery.php Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361732 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 this should work! (kinida hard to check while at work ) <?php include("config.php"); // get picture details $con; $id = (int)$_GET['photo_id']; $aid = (int)($_GET['album_id']); $where = ($aid>0)?" WHERE album_id = $aid":""; $sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $id, 1"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows > 0) { $row = @mysql_fetch_array($result); $photo_title = $row['photo_title']; $photo_loc = $row['photo_location']; $album = $row['album_id']; //check Next $nid = $id+1; $sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $nid, 1"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Next = ($num_rows > 0); //check Previous $pid = $id-1; $sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $pid, 1"); $result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); $num_rows = mysql_num_rows($result); $Previous = ($num_rows > 0); ?> <html> <head> <title>Gallery - <?php echo($photo_title); ?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>Gallery - <?php echo($photo_title); ?></td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td> <img src="<?php echo($photo_loc); ?>" /> </td> </tr> </table> <table width="60%" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><a href="gallery.php">View Gallery</a></td> </tr> <tr> <td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td> </tr> <tr> <td> <?php echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid&album _id=$album'>Previous</a>":"No more"; ?> </td> </tr> <tr> <td> <?php echo ($Next === true)?"<a href='view_photo.php?photo_id=$nid&album _id=$album'>Next</a>":"No more"; ?> </td> </tr> </table> </td> </tr> </table> </body> </html> <?php }else{ $msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view."; // Display error message displayPage($msg); die(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361740 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 It still claims there is no photo selected. ??? ??? ??? ??? I am also at work supposed to be designing some sharepoint strcutures but php is more fun, welll when it works Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361741 Share on other sites More sharing options...
adam291086 Posted October 4, 2007 Author Share Posted October 4, 2007 It appears in the URL that the photo_id is being inputted but the echo message, no photo is being displayed rather than the Pictures. Quote Link to comment https://forums.phpfreaks.com/topic/71798-solved-question/#findComment-361747 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.