Jump to content

[SOLVED] Moving to the previous or next database entry


HokieTracks

Recommended Posts

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?

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.

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.

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?

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.

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.

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.

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.

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'];
?>

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!

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.

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'];
?>

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. :)

I was half way through a completely wrong attempt at a link.  :D 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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.