HokieTracks Posted May 5, 2009 Share Posted May 5, 2009 I have some code that displays the latest entry in a database full of photos: <?php include "layout.php"; ?> <body> <html> <div id="contnt"> <div id="h1"> Photo of The Week </div> <?php require('connect.php'); //Defines $query_photos as getting everything from photo where the id equals the id in the browser $query_photos = "SELECT * FROM photo ORDER BY id DESC LIMIT 1"; //defines photos as running the above $photos = mysql_query($query_photos) or die(mysql_error()); $row_photos = mysql_fetch_assoc($photos); //numbers rows as variable $totaphotos $totalphotos = mysql_num_rows($photos); //if there are no rows, then there is no photos under that ID. if ($totalphotos == '0') { echo '<script>alert("No photos");</script>'; echo '<script>history.back(1);</script>'; } //otherwise write the photos title and underneath the photos content else { echo $row_photos['title'];?> <br/><br/> <?php echo $row_photos['content']; ?> <?php } ?> </div> </body> </html> <footer> <?php include "footer.php"; ?> </footer> This works great but my problem is that I want my users to have access to all of the photos not just the latest one. So is there a way that I can add previous and next links that take the user to the previous and next database entrys. The next link would also need to only appear when the user is on any entry except for the latest one and same for the previous except for when it is on the first entry. Any ideas? Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/ Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 You probably want to add a parameter like photoid in the URL to that page. Then use $_GET['photoid'] for the query instead of that ORDER BY id DESC LIMIT 1. Also, make sure that the param photoid is a number before running a SQL. If it's not a valid number, then get the last photo. If the number is too big, get the last photo. I hope your table photo has a unique ID field. Then when creating the prev and next links, just use the same URL as that page is on, but use photoid - 1 and photoid + 1 to go prev and next. Of course, make sure you can go prev and next. There is no prev is it's the first photo and no next if it's the last. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826885 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 But I can't add that because every time a new photo is added that latest id goes up by 1. So if I were to add something like photoid into the url then every time I added a photo I would have to change every single link to the page. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826911 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 No, the photoid is the ID field of the photo in the DB. Say you have 10 photos, ids 1 - 10. Say I go to the url - http://site.com/gallery.php?photoid=3 You go and query for the 3rd photo. If it exists, get it. In the URL, you would generate http://site.com/gallery.php?photoid=2 and http://site.com/gallery.php?photoid=4. Of course, make sure photoid isn't 10 because it would have no 11. Same for if the photoid is 1 - there is no prev. Get it now? Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826916 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 I understand but this page is used to display the latest photo so when the user accesses the page the id is not necessarily going to be the same tomorrow. So if I were to go the route you suggested I would have to change all of the links that link to the latest photo page every time I add a new one. Now, I understand that your method works great if the link is to a specific photo in the database with an id that will never change. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826924 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Why would you have a photoid that changes? When you add a new images, you get another photoid. By the way, when I say photoid, I'm referring to a column in the DB. Preferably a PRIMARY_KEY AUTO_INCREMENT column. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826934 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 Yeah I know what you mean by photoid. But, the reason the database photo id changes is because the page displays the newest id in the database. So one day the photo id that the page displays may be 20 but tomorrow it may be 21. Because this page displays the newest photo every time a newer one is entered into the database. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826940 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Well yeah, you can just say that if $_GET['photoid'] is not set or if it's empty, then fetch the latest one. The photoid is just there for navigating, not to display the latest entry. Then go back and read my first post in this topic more carefully. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826941 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 Oh my bad you said it all along. But there is still one problem isn't there? Because if there is no valid number defined the page will display the latest photo. But, that means when the previous button is clicked (or next) how will it know what to +1 or -1 to. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826949 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Well if you're displaying the latest photo, you know it'll always be a -1. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826956 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 Yeah, but the code doesn't know what to subtract that 1 from. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826959 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 The photoid. You have the SQL right there. Use it to get the photoid from the DB. o.O Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826982 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 Ok, I get it now. This what I have so far: <?php require('connect.php'); //Defines $query_photos as getting everything from photo where the id equals the id in the browser $query_photos = "SELECT * FROM photo ORDER BY id DESC LIMIT 1"; //defines photos as running the above if (is_numeric($_GET['photoid']){ $photos = $_GET['photoid']; } else{ $photos = mysql_query($query_photos) or die(mysql_error()); } $row_photos = mysql_fetch_assoc($photos); //numbers rows as variable $totaphotos $totalphotos = mysql_num_rows($photos); //if there are no rows, then there is no photos under that ID. if ($totalphotos == '0') { echo '<script>alert("No photos");</script>'; echo '<script>history.back(1);</script>'; } //otherwise write the photos title and underneath the photos content else { echo $row_photos['title'];?> <br/><br/> <?php echo $row_photos['content']; ?> <?php } ?> But I am not sure what should come next as far as defining what photoid is for the previous and next links and after that how to set up the links to have -1 and +1. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-826993 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 I updated the code: <?php require('connect.php'); //Defines $query_photos as getting everything from photo where the id equals the id in the browser $query_photos = "SELECT * FROM photo ORDER BY id DESC LIMIT 1"; //defines photos as running the above if (is_numeric($_GET['photoid'])){ $photos = $_GET['photoid']; $row_photos = "SELECT * FROM photo WHERE id = $photos"; } else{ $photos = mysql_query($query_photos) or die(mysql_error()); $row_photos = mysql_fetch_assoc($photos); } echo $row_photos['title'];?> <br/><br/> <?php echo $row_photos['content']; ?> Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827005 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 SQL INJECTION ALERT!! 1. Don't set the variable $photos to be two different data types. That's confusing. Do something like this: $photoid = $_GET['photoid']; $query = "SELECT * FROM photo "; if (is_numeric($photoid)) $query .= "WHERE id = '" . mysql_real_escape_string($photoid) . "' "; else $query .= "ORDER BY id DESC "; $query .= "LIMIT 1"; 2. In your SQL, you have selected all the fields, so one of the fields is id. $row_photos['id'] should do it. Gotta think! Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827007 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 Ok, this is what I have now: <?php require('connect.php'); $photoid = $_GET['photoid']; $query = "SELECT * FROM photo "; if (is_numeric($photoid)) $query .= "WHERE id = '" . mysql_real_escape_string($photoid) . "' "; else $query .= " ORDER BY id DESC "; $query .= "LIMIT 1"; echo $query['title'];?> <br/><br/> <?php echo $query['content']; ?> But all I end up with for title and content is "S". Which is not what should be showing up. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827027 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Hello, That's one reason I don't like posting codes for people. They just take it, slap it down and think it'll run just like that. Do you know PHP? You can see that I never ran the query. Ken Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827047 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 Well I am definatley a beginner but I messed with it a bit and got this to work: <?php require('connect.php'); $photoid = $_GET['photoid']; if (is_numeric($photoid)) $query = "SELECT * FROM photo WHERE id = '" . mysql_real_escape_string($photoid) . "' LIMIT 1" else $query = "SELECT * FROM photo ORDER BY id DESC LIMIT 1"; if ($photoid == '-1'){ $photos = mysql_query($query) or die(mysql_error()); $row_photos = mysql_fetch_assoc($photos); echo $row_photos['title'];?> <br/><br/> <?php echo $row_photos['content']; ?> Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827052 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Yeah, I knew you were a beginner when you asked a few of the questions earlier. It's cool. We all start somewhere asking clueless simple questions where the answers are so obvious that we just can't think for ourselves because the new language is causing a burden in our head. Anyways, please know from this point on, when people post codes, don't expect them to be full code you can slap into your code. Read it, analyze it and read the message. When most people post codes, they're giving you a heads up or a hint on how things should work. Now back to the subject. 1. May I ask why you have that if statement to check if $photoid is -1? Also, you didn't close the curly brace for the if statement. Syntax error! 2. From your query, you know how to pull certain fields out to display right? Well, you can pull out the ID as well, can't you? You can use that to create a link. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827054 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 I was half way through a completely wrong attempt at a link. But, yeah I know how to pull out the photo id. I am just not sure how to create the link so that it subtracts 1 from it. Do I do something like http://hokietracks.com/home/photo.php?$row_photos['id']-1? Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827068 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Almost, you forgot to add the photoid parameter. Now it's up to you to learn some PHP syntax and get it to display right. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827077 Share on other sites More sharing options...
HokieTracks Posted May 5, 2009 Author Share Posted May 5, 2009 Ok, so let me make sure that this url is correct before I spend to long trying to figure it out with the wrong url. http://hokietracks.com/home/photo.php?photoid=$row_photos['id']-1 Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827083 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Yup, that would be it. Make sure you re-read my first post in this topic for special conditions. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827090 Share on other sites More sharing options...
HokieTracks Posted May 6, 2009 Author Share Posted May 6, 2009 Ignore this post, I think I am on to something. Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827932 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 What do you mean? Link to comment https://forums.phpfreaks.com/topic/156974-solved-moving-to-the-previous-or-next-database-entry/#findComment-827942 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.