Page 1 of 1

Parse and pagination script

Posted: Fri May 11, 2007 12:05 pm
by phpyoungdeveloper
Hello everyone,

I am sorry for asking such a simple question but I am desperate to find the answer as I am still a novice in PHP and I need this to be done. I used search but still it did not help me solve my problem.

So, here is the code

Code: Select all

<?

$base=file("x.txt");
for($i=0;$i<sizeof($base);$i++){
	$base[$i]=rtrim($base[$i]);
	$base[$i]=explode("\t",$base[$i]);
}
echo "<table>";
for($i=0;$i<sizeof($base);$i++){
	echo "<tr>";
	for($j=0;$j<sizeof($base[$i]);$j++){
		echo "<td><font size=1 face=tahoma>".$base[$i][$j]."</td>";
	}
	echo "</tr>";
}
echo "</table>";
?>
I need to add pagination above and below the results, which can be adjusted by a variable in the code. Please help me with that, I will be very grateful, I hope I will be able to learn from that! Thank you very much in advance!

Posted: Fri May 11, 2007 2:54 pm
by tecktalkcm0391
Try adding this before your print out

Code: Select all

if(isset($_GET['page']) && is_numeric($_GET['page']){
$page = $_GET['page'];
} else {
$page = 1;
}

$items_per_page = 5;

if($page!=1){
$display_start = $page*$items_per_page;
} else {
$display_start = $page;
}

$display_end = $display_start+$items_per_page;

//then change:
for($i=0;$i<sizeof($base);$i++){ 
// to:
for($i=$display_start;$i<$display_end;$i++){
Build on that to make the pagination script

Posted: Fri May 11, 2007 3:25 pm
by phpyoungdeveloper
thank you very much Chris,

I did as you said and it seems it still does not want to work, could you please put it inside the script?

the problem is definitely here

Code: Select all

if(isset($_GET['page']) && is_numeric($_GET['page']){
$page = $_GET['page'];
} else {
$page = 1;
}
also how can I add links with page number above and below the results?

Sorry for such a stupid question. THanks in advance!

Posted: Fri May 11, 2007 9:54 pm
by tecktalkcm0391
sorry, i forgot a ) on the first line:

Code: Select all

if(isset($_GET['page']) && is_numeric($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
To add links do something like this.

Code: Select all

$number_of_pages = ceil(sizeof($base)/$items_per_page); //ceil rounds pages up to show all 

//where you want the pageination put
for($p=0; $p<$number_of_pages; $p++){
     print('<a href="?page='.($p+1).'">Page '.($p+1).'</a>&nbsp; &nbsp; &nbsp; &nbsp;');
}

Posted: Sat May 12, 2007 5:05 am
by phpyoungdeveloper
yes, it worked! thank you so much Chris, your expert advice was of real help to me!