View Full Version : Paging Problem
calumogg
02-16-2008, 06:01 PM
Hi everyone.
I have just about finished building my photo gallery script (it can be seen in action at Here (http://www.calumogg.co.uk/galleries.php?mode=index))
As you can see there are three main stages of it, the index (where all the different galleries are shown) the individual gallery (where only one specific gallery is shown depending on a $_GET called category, the paging is working on this one) and the final stage where only one picture at a time is shown this is loaded from a $_GET variable called id
The problem I am having is that I cant get the previous and next buttons to work on the final stage. Below is the script I am using if anyone has any ideas of how I can get my previous and next buttons working they would be greatly appreciated.
Thanks in advance for any help :)
alexjewell
02-16-2008, 08:42 PM
I don't believe this is the cause of the problem, but when you're deciding the page, you're doing something a little wrong. Here's what you have:
if ($_GET['mode']=='gallery'){...}
if ($_GET['mode']=='picture'){...}
In this situation, you don't want to use two if's...you want to use if, else if, and maybe an else. For example:
if ($_GET['mode']=='gallery'){...}
else if ($_GET['mode']=='picture'){...}
else{...]
The else isn't necessary, but I noticed that when one takes the ?mode=gallery or picture off, the page is blank. The else is a safeguard against that sort of thing. The else if just says if there is another entry for mode, do this instead for this entry. It's a better organization and structure.
Like I said, though, I don't KNOW if this is the cause of your pagination problem...but it's the first thing I noticed when I skimmed through your script.
calumogg
02-16-2008, 09:18 PM
Hi, thanks for your quick reply, I am not using the else if statement because I am not entirely sure how it works so i dont know its capabilities and limits, this is just a small part of the script I have built, I am also using if($_GET['mode']=='index') and if($_GET['mode']=='search_results') and if($_GET['mode']=='picture_search')
But I do see your point because if anyone types in the address and gets the mode=something bit wrong then nothing will load, I could add in one else if statement that will redirect to mode=index so that if anything else is typed in they are redirected instantly to the index, but I would like to get the paging problem sorted then I will fine tune the script.
alexjewell
02-17-2008, 03:07 AM
Ok, I looked more deeply at the problem area (the next and previous links). Let me visually show you what the problem is. We have two if statements (you say there's more, but these 2 are the only ones that have to do with the problem):
if ($_GET['mode']=='gallery'){...}
if ($_GET['mode']=='picture'){...}
Now, you're making the $pageNum variable in the first if statement, if mode=gallery. Then, you're trying to use $pageNum again in the second if statement, if mode=picture. Now, if anything in mode=picture is being processed, that means that mode does not = gallery, it = picture...so anything done in mode=gallery is ignored. Therefore, the variable $pageNum is being ignored in the second if statement.
To fix this, I'd do the $pageNum thing OUTSIDE of the if statements, before both of them, at the top. If you do this and for some reason there's still a problem, echo the variables being used as a test to make sure they have values. Mess around with stuff until it works. If stuff is still messed up, feel free to post your new code and I'll take a look at it.
Also, I saw some minor issues in the code. In both Next and Previous links, I see you using the < and > symbols. To follow guidelines and stuff, you should use the codes for those: < for < and > for >. So it would look like:
Next >
Also, remember what I said about the if, if else, and else statements: they're better for organization. It's simple logic.
If THIS do THIS
Else if THIS do THIS instead
Else (if the top things do not apply) do THIS
So, simple example:
if($fruitColor == 'red'){ echo '<p>You have an apple!</p>';}
else if($fruitColor == 'yellow'){ echo '<p>You have a banana!</p>';}
else if($fruitColor == 'orange'){ echo '<p>You have an orange!</p>';}
else{ echo '<p>You do not have an apple, a banana, or an orange!</p>';}
Anyway, multiple if's will still work, but the organized if, else if, else statements make more sense...it groups the statements together, really, and makes reading/understanding your code a lot easier. Fix the pagination issue, then worry about organization...it'll improve these types of situations drastically!
calumogg
02-18-2008, 08:26 AM
But the two $pageNum have different values, thats why they are in the separate bits of code. One is referring to the paging in the mode=gallery and the second is for mode=picture
I somehow need to add the ID of the next picture into the paging link but I don't know how to select that from the database it is made a little harder by the fact that the IDs are not continuous.
Is there any chance you could show me some code that would select the next and previous IDs from my database?
Thanks for all your help so far it is really useful
calumogg
02-18-2008, 06:00 PM
I have made a little bit of progress....
Here is what I have now, but the next and previous buttons are both linking to a picture with the same ID, and this is supposed to be the last picture in the paging
calumogg
02-19-2008, 05:12 PM
I finally got it working, I had to add another column into my database that count the number of pictures in each category, and this number if consecutive according to the filename.
Thanks very much for all your help alexjewell it has been really useful, I am just going over my code making the few changes to the structure you suggested. If you want to see my finished code let me know and I will post it up. :)
alexjewell
02-22-2008, 12:51 AM
Hey, sorry for taking a bit to get back to this thread. Feel free to post the restructured code if you'd like and I'll take a look at it.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.