Unusual pagination

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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Unusual pagination

Post by evilmonkey »

Hello,

I'm writing a complex shoutbox (hybrid of a shoutbox and a forum, if you will), and I'm having a problem with pagination. Before I post the code, let me explain what the problem is. I want to show 10 "shouts" on one page. This page will reside in an iframe, so the user does not have a choice in how many shouts he'll see. As it stands, there are 12 (0-11) shouts in the databse. The first page displayes 11, the second page displays nothing. When I click the "prev" button on page that should display the last shout, I get a white page. I've been wrestling with a bunch of different problems with pagination for a while now, and I need a pair of fresh eyes to look at this code and point out what I did wrong.

Code: Select all

?php
//this page gets all the shouts and throws them to a template which is an iframe within profile.php
session_start();
include_once("functions.php");
include_once("user_class.php");
require("/home/tmeet/public_html/smarty/Smarty.class.php");
//id will have to be passed (get)
$id=$_GET['id'];
$user_profile = new User($id);

//get shoutbox information, this is basically a mysql_query()/mysql_fetch_array() operation that returns a 2D array
/*$shouts => [0] => [shouter] = evilmonkey
                                 [shout] = test shout
                                 [date] = 1124294982
                       [1] => [shouter] = test_user
                                 [shout] = test shout 2
                                 [date] = 1124295020 
that's what that variable holds */ 
$shouts = $user_profile->sb_get_shouts($id);

//for pagination purposes, find out where to start and where to stop, and whether or not to have next/prev buttons
$info['nextbutton'] = "true";
$info['prevbutton'] = "true";

$start = $_GET['start'];
if (!isset($_GET['start']) || $start == 0) { 
	$start = 0;
	$info['prevbutton'] = "false"; 
}
if ($start + 10 >= sizeof($shouts)){
	$stop = sizeof($shoutbox);
	$info['nextbutton'] = "false";
} else {
	$stop = $start+10;
}
//the template needs to know where we started to know how to link the next/prev buttons
$info['start'] = $start;
//end pagination

//main loop, controls all output
for ($i=$start; $i<=$stop; $i++){
	 $shoutbox[$i]['shouter'] = $shouts[$i]['shouter'];
	 $shoutbox[$i]['shout'] = $shouts[$i]['shout'];
	 $shoutbox[$i]['date'] = date("M d, Y", $shouts[$i]['date']);
	 if ($shouts[$i]['shouter']=="Annonymous"){
	 	$shoutbox[$i]['age']="";
	 	$shoutbox[$i]['gender']="";
	 	$shoutbox[$i]['pic']="";
	 }
	 else {
	 	$userinfo = sql_pull("SELECT `id`, `age`, `gender` FROM `users` WHERE `username`='".$shouts[$i]['shouter']."'", $user_profile->db);
	 	$shoutbox[$i]['age']=$userinfo[0]['age'];
	 	$shoutbox[$i]['gender']=$userinfo[0]['gender'];
	 	//for a picture, 2 options:
	 	//option 1: a smal pic exists in the tmp folder
	 	if (is_file("tmp/S_".$userinfo[0]['id']."_1.jpg")){
	 		$shoutbox[$i]['pic'] = '<img src = "tmp/S_'.$userinfo[0]['id'].'_1.jpg" border = "0">';
	 	}
	 	//option 2: small pic doesn't exist in the tmp folder: two more options open
	 	else {
	 		$userpic = sql_pull("SELECT `pic1` FROM `prefs` WHERE `id`=".$userinfo[0]['id'], $user_profile->db);
	 		//option 1: user doesn't have a main pic
	 		if ($userpic[0]['pic1']==""){
	 			$shoutbox[$i]['pic']="";
	 		}
	 		//option 2: user has a main pic, but for some reason, it hasn't been resized and saved to the tpm/ folder
	 		//this poor user probably never loaded his profile, or just changed his pic
	 		else {
	 			include_once("savepic.php");
	 			savepic(75, 75, $userpic[0]['pic1'],"S_".$userinfo[0]['id']."_1.jpg");
	 			$shoutbox[$i]['pic'] = '<img src = "tmp/S_'.$userinfo[0]['id'].'_1.jpg" border = "0">';
	 		}
	 	}
	}
}
?>
Notes about the code: $shouts array and $info array are assigned to a template. In the template, there's a check that checks whether or not to display the prev/next buttons, and it gets the right value of $start and adds (or subtracts) 10. There is no problem with the link, the problem seems to be with this code.

Thanks in advance. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, I hate pagination - cool to look at, awful to implement.

Anyway, I noticed your default start is 0. Then, you add 10 to that. 0-10 is 11 entries.

Fixing that might solve all your problems, or it might solve none of them. Either way, let us know :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

pickle wrote:Hmm, I hate pagination - cool to look at, awful to implement.
I hear that! Anyway, you suggestion fixed me having 11 rows on the page, but didn't fix anything else...if you have any other suggestions, that'd be good because I'm in trouble if tis doesn't start working soon...

EDIT: Alright, I looked at it again, I managed to fix the back button. Basically, it needs two variables from $_GET: the id, and start. I was passing the start, but not the id (D'oh!). But that still didn't fix the "next" button, that still displays a blank screen with no shouts on it. I hate pagination. :P

Thanks for the help, more help is needed though. :(
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Couple of things:

1) I can't see where you use $info['nextbutton'] and $info['prevbutton'] after you set them.
2) Print out $info['prevbutton'] and $info['nextbutton'] right after you set them in the if, if/else clauses. They're probably different than you're expecting.
2) It's probably too late to change how you're doing it, but using SQL to do pagination is much easier:

Code: Select all

$sql = <<<QUERY
SELECT
   *
FROM
  table
LIMIT
  $start,$entries_per_page
QUERY;
Makes stuff much cleaner - ah well. For you next project at least.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I was going to do another edit, but this requires another post. I played with the start variable directly from the address bar (tried changing it from 0 to 1,2,3,4, all the way to 10). Here are my findings: I have a template (smarty), and I use the {section} command to replicate a table for as many "shouts" as needed ({section} is a loop in smarty). When I change the start from 0 to 1, I get 1 blank table at the top of the page and messages in all other tables. When I change it to 2, I get two blank tables at the top, and messages in the other 8 tables. When I change it to 3, I get 3 blank tables, and messages in the other 7 tables. When I change it to 4, I get a blank screen. Same for 5-infinity. The wierd thing is, the messages that appear after the blank tables are in the same position they would be if start were 0, so basically, if start=3, it skips the first three shouts and outputs blank tables, and shows me shouts 4-10. Very odd indeed...
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

My next button and prevbutton go to an if condition in smarty. Read above please, it's more interesting IMO.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Alright, I ended it. I decided to do a two-line fix in my code and make it SQL pagination. Now, everything works like it was just greased with butter. Thanks pickle. :)
Post Reply