Jump to content

Recommended Posts

I have a php page that's showing the top result in this manner:

$result = mysql_query("SELECT * FROM cities WHERE url LIKE '%$atlanta%' LIMIT 1")

 

Works great.  What I'm wondering... 

 

On the page the result is displaying on..  below the result, can I make a link that says "View Next Result" and on click, have it bring up the next record in the db?  How might I achieve showing that next result... one after another.. until the end? 

If your site gets popular enough, rather than re-query the DB using like (slow) for every page, store the results in a session variable, with a unique token. Pass that token along with your page number, and garb the desired results from the session array, rather than the database.

If your site gets popular enough, rather than re-query the DB using like (slow) for every page, store the results in a session variable, with a unique token. Pass that token along with your page number, and garb the desired results from the session array, rather than the database.

 

I'll look into what you said... not nearly enough experience to understand it right now... but I'm slowly figuring it out.  I definitely want the code to be the most efficient so I'll be trying to dig into this approach.

 

Thanks!

A whole pagination script?  :) 

 

This did the trick.. 

$sql = "SELECT * FROM mytable WHERE id='<?=$_POST['id']?>'";

which let me print..

<a href='?id=<?=db["id"]+1?>'>Next Entry</a>

 

Um, yeah . . . a whole pagination script. The only thing different between what you want and what typically constitutes a pagination script is the number of records per page. Whether you want 100 or 1 records on a page, it is still pagination. Your "trick" is invalid it simply shows the record which has an ID that is 1 greater than the last. There are several problems with that: 1) Such a record may not exist, 2) Your original requirement was to show records that match a specific criteria (i.e the URL value). How do you know the record with the next highest id matches that criteria?

 

Yes, you could do a simpler solution, but why? There is already logic for pagination that is easily implemented. If you wanted to do a different solution, then you need to build it from scratch. Off the top of my head you could always retrieve 2 records - the current record and the next record. You the first result to display on the page and the second to create the next link. But, what if you need a back link. Well, you could pass the current id into the next page to use for a previous, but then you need to account for the first page that won't have a previous link.

A whole pagination script?  :) 

 

This did the trick.. 

$sql = "SELECT * FROM mytable WHERE id='<?=$_POST['id']?>'";

which let me print..

<a href='?id=<?=db["id"]+1?>'>Next Entry</a>

 

Um, yeah . . . a whole pagination script. The only thing different between what you want and what typically constitutes a pagination script is the number of records per page. Whether you want 100 or 1 records on a page, it is still pagination. Your "trick" is invalid it simply shows the record which has an ID that is 1 greater than the last. There are several problems with that: 1) Such a record may not exist, 2) Your original requirement was to show records that match a specific criteria (i.e the URL value). How do you know the record with the next highest id matches that criteria?

 

Yes, you could do a simpler solution, but why? There is already logic for pagination that is easily implemented. If you wanted to do a different solution, then you need to build it from scratch. Off the top of my head you could always retrieve 2 records - the current record and the next record. You the first result to display on the page and the second to create the next link. But, what if you need a back link. Well, you could pass the current id into the next page to use for a previous, but then you need to account for the first page that won't have a previous link.

 

cool... thx dude.  that's the kind of info I was looking for. 

How about this?

 

$items_per_page = 1;
$page = max( 1, (int) $_GET['page'] );
$offset = ($page-1) * $items_per_page;
$limit = $items_per_page;

$sql = "SELECT * FROM 'mytable' LIMIT {$offset}, {$limit}"
http://domain.com/index.php?page=1
http://domain.com/index.php?page=2
http://domain.com/index.php?page=3

Um, yeah . . . a whole pagination script. The only thing different between what you want and what typically constitutes a pagination script is the number of records per page. Whether you want 100 or 1 records on a page, it is still pagination. Your "trick" is invalid it simply shows the record which has an ID that is 1 greater than the last. There are several problems with that: 1) Such a record may not exist, 2) Your original requirement was to show records that match a specific criteria (i.e the URL value). How do you know the record with the next highest id matches that criteria?

 

Yes, you could do a simpler solution, but why? There is already logic for pagination that is easily implemented. If you wanted to do a different solution, then you need to build it from scratch. Off the top of my head you could always retrieve 2 records - the current record and the next record. You the first result to display on the page and the second to create the next link. But, what if you need a back link. Well, you could pass the current id into the next page to use for a previous, but then you need to account for the first page that won't have a previous link.

 

A better way to do this is something like

 

<a href='?id=<?php echo db["id"] ?>&direction=next'>Next Entry</a>

 

Then

 

 $query = 'SELECT `whatever` FROM `table` WHERE `id` > '.sanitize($_POST['id']).' ORDER BY `id` ASC LIMIT 1 

 

Or previous

 

 $query = 'SELECT `whatever` FROM `table` WHERE `id` < '.sanitize($_POST['id']).' ORDER BY `id` DESC LIMIT 1 

<a href='?id=<?=db["id"]+1?>'>Next Entry</a>

 

What happens when a link gets deleted, and the +1 id value doesn't exist?  It is better to limit the query by the rows, than to blindly pull a row that may or may not exist.

 

 

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.