Page 1 of 1
Complex next prev (not too complex)
Posted: Fri Dec 22, 2006 1:40 pm
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.
Posted: Fri Dec 22, 2006 1:52 pm
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
Posted: Fri Dec 22, 2006 1:57 pm
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>'

input filtering!!
Posted: Fri Dec 22, 2006 2:01 pm
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.
Posted: Fri Dec 22, 2006 2:50 pm
by Kieran Huggins
The Ninja Space Goat wrote:

input filtering!!
I know, I know... I felt a little dirty hitting submit
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