Complex next prev (not too complex)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phlackee
Forum Newbie
Posts: 2
Joined: Fri Dec 22, 2006 1:38 pm

Complex next prev (not too complex)

Post by phlackee »

Hi All!
I have 3 tables:

Category, Project , Entry .

Category have CatId as PK
Project have ProId as Pk and CatId as FK
Entry have EntId as Pk, ProId as Fk and Catd as FK

How to make a Next Prev links that if you are in

view.php?CatId=11&ProId=22&EntId=111

when you choose next, it goes and check who is next record from same category and same project and display,e.g.
view.php?CatId=11&ProId=22&EntId=112

if there is no entry in that project go to next project , first entry, then dispay entry,

view.php?CatId=11&ProId=23&EntId=113

and if there is no project in that category display next category with first project and first entry.

view.php?CatId=12&ProId=24&EntId=114
thanks.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

echo '<a href="view.php?CatId=11&ProId=22&EntId='.$_GET['EntId']+1.'">next</a>'

Code: Select all

echo '<a href="view.php?CatId=11&ProId=22&EntId='.$_GET['EntId']-1.'">prev</a>'
You'll want to do some testing to make sure you don't try to send them to a record that doesn't exist, but that's the basic idea.

Cheers,
Kieran
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Kieran Huggins wrote:

Code: Select all

echo '<a href="view.php?CatId=11&ProId=22&EntId='.$_GET['EntId']+1.'">next</a>'

Code: Select all

echo '<a href="view.php?CatId=11&ProId=22&EntId='.$_GET['EntId']-1.'">prev</a>'
8O 8O input filtering!!
phlackee
Forum Newbie
Posts: 2
Joined: Fri Dec 22, 2006 1:38 pm

Post by phlackee »

Kieran, thanks for reply!

I know what you mean but ,its not that easy,

how to to know if there is no more entry in that project and there is no more projects in that category

so I should have like this:

<| < entry 3 of 3 > |>

I should count how many entries are in a project then display , if there are no more entries , like 3 of 3 check next project if have entries, if yes, count how many, and display, if next project doesnt exist change category, and if latest category latest project and latest entry go to first.

Its kinda complex. thats why I decide to ask experts.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

The Ninja Space Goat wrote:8O 8O input filtering!!
I know, I know... I felt a little dirty hitting submit :oops:

My *real* solution would be to retrieve all distinct CatId, ProId, and EntId's from the database and drop them in the $_SESSION array. then you can simply link to the next record by appending ?next[entity] to the URL.

Code: Select all

// I assume $EntId is the variable with the current EntId in it.

$EntIdPos = array_search($EntId,$_SESSION['EntId']);

if(isset($_GET['next']['entity']) && isset($_SESSION['EntId'][$EntIdPos+1])) $EntId = $_SESSION['EntId'][$EntIdPos+1];

if(isset($_GET['prev']['entity']) && isset($_SESSION['EntId'][$EntIdPos-1])) $EntId = $_SESSION['EntId'][$EntIdPos-1];

// carry on...
You can repeat this pattern for other variables as well.

Eventually, you'll want to take a look at "clean URLs" and mod_rewrite. But that may be a little while in your future.

Cheers,
Kieran
Post Reply