What iswrong with my pagination code

Hi, can someone advise me as to why I get this error? thanks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head><body><center>
<div class="display print">

<?php
//Open a new connection to the MySQL server
$link = mysqli_connect("localhost", "root", "", "homedb"); 

// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
echo date('m/d/y');

//MySqli Select Query
$sql = "select * FROM paytbl";
$result = mysqli_query($link,$sql);
if (!$result) {
    printf("Error: %s\n", mysqli_error($link));
    exit();
}else{
   while($row = mysqli_fetch_array($result)){

//Set at what record to break at 25 records per page
$BreakAT=25;
$pagenum = 0;
echo date('m/d/y');

?>
<b>
<div class="title"><h3>Payment Report</h3></div><br />
<table align="center" cellspacing=5 cellpadding=5 border=2>
<thead>
<tr>
<th colspan=2> </th>
<th bgcolor="cyan" colspan=2>Rent</th>
<th bgcolor="99FF99">Prev</th>
<th bgcolor="FF99FF">late</th>
<th bgcolor="99FF99">sec</th>
<th></th>
<th bgcolor="99FF99">court</th>
<th></th>
<th bgcolor="99FF99">hud</th>
<th>Date</th>
<th colspan=2></th>
<tr>
<th>Tenant</th>
<th>Unit</th>
<th bgcolor="cyan">Due</th>
<th bgcolor="cyan">Paid</th>
<th bgcolor="99FF99">Bal</th>
<th bgcolor="FF99FF">chg</th>
<th bgcolor="99FF99">dep</th>
<th bgcolor="FF99FF">damage</th>
<th bgcolor="99FF99">cost</th>
<th bgcolor="FF99FF">nsf</th>
<th bgcolor="99FF99">pay</th>
<th>Paid</th>
<th bgcolor="cyan">Paidsum</th>
<th>Comments</th>
</tr>
</thead>

<?php
$slice= array_slice($results,((($pagenum + 1) * $BreakAT) - $BreakAT), (($pagenum + 1)* $BreakAT));
foreach($slice as $row){

echo '
        <tr>
        <td>' . $row['tenant'] . '</td>
        <td>' . $row['unit'] . '</td> 
        <td align=right class="currency">$'.number_format($row['amtdue'],2).'</td>          
        <td align=right class="currency">$'.number_format($row['amtpaid'],2).'</td>
        <td align=right class="currency">$'.number_format($row['prevbal'],2).'</td>
        <td align=right class="currency">$'.number_format($row['latechg'],2).'</td>
        <td align=right class="currency">$'.number_format($row['secdep'],2).'</td>
        <td align=right class="currency">$'.number_format($row['damage'],2).'</td>
        <td align=right class="currency">$'.number_format($row['courtcost'],2).'</td>
        <td align=right class="currency">$'.number_format($row['nsf'],2).'</td>
        <td align=right class="currency">$'.number_format($row['hudpay'],2).'</td>
                <td>' . $row['datepaid'] . '</td>
        <td align=right class="currency">$'.number_format($row['paidsum'],2).'</td>
        <td>' . $row['comments'] . '</td>
        </tr>';
}

echo '
<tr>
<td colspan=2></td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtdue')) . '</td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtpaid')) . '</td>
<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'prevbal')) . '</td>
<td bgcolor="FF99FF" align=right>' . array_sum(array_column($slice, 'latechg')) . '</td>
<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'secdep')) . '</td>
<td bgcolor="FF99FF" align=right>' . array_sum(array_column($slice, 'damage')) . '</td>
<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'courtcost')) . '</td>
<td bgcolor="FF99FF" align=right>' . array_sum(array_column($slice, 'nsf')) . '</td>
<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'hudpay')) . '</td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'paidsum')) . '</td>' 
 } //  ************* line 100  ***********************************
?>
</table class="print">
<div class="breakhere"></div>
</b></center></div></body></html>

result:

Parse error: syntax error, unexpected ‘}’, expecting ‘;’ or ‘,’
in C:\xampp\htdocs\property\paymentlist.php on line 100

So its telling you. On line 100, it sees a }, but it was expecting something else, either a semicolon or a comma.
Look at line 84. Now look at line 99. What’s different?

EDIT: Helps if you can count row numbers, Marc.

I truncated the code and made the suggested changes and this took place?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"[http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd](https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd)">
<html xmlns="[http://www.w3.org/1999/xhtml](https://www.w3.org/1999/xhtml)">
<head></head><body><center>
<div class="display print">

<?php
//Open a new connection to the MySQL server
$link = mysqli_connect("localhost", "root", "", "homedb");

// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
echo date('m/d/y');

//MySqli Select Query
$sql = "select * FROM paytbl";
$result = mysqli_query($link,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($link));
exit();
}else{
while($row = mysqli_fetch_array($result)){

//Set at what record to break at 25 records per page
$BreakAT=25;
$pagenum = 0;
echo date('m/d/y');

?>
<b>
<div class="title"><h3>Payment Report</h3></div><br />
<table align="center" cellspacing=5 cellpadding=5 border=2>
<thead>
<tr>
<th colspan=2> </th>
<th bgcolor="cyan" colspan=2>Rent</th>
<th bgcolor="99FF99">Prev</th>
<th bgcolor="FF99FF">late</th>
<th bgcolor="99FF99">sec</th>
<th></th>
<th bgcolor="99FF99">court</th>
<th></th>
<th bgcolor="99FF99">hud</th>
<th>Date</th>
<th colspan=2></th>
<tr>
<th>Tenant</th>
<th>Unit</th>
<th bgcolor="cyan">Due</th>
<th bgcolor="cyan">Paid</th>
<th bgcolor="99FF99">pay</th>
<th>Paid</th>
<th bgcolor="cyan">Paidsum</th>
<th>Comments</th>
</tr>
</thead>

<?php
$slice= array_slice($results,((($pagenum + 1) * $BreakAT) - $BreakAT), (($pagenum + 1)* $BreakAT));
foreach($slice as $row){

echo '
<tr>
<td>' . $row['tenant'] . '</td>
<td>' . $row['unit'] . '</td>
<td align=right class="currency">$'.number_format($row['amtdue'],2).'</td>
<td align=right class="currency">$'.number_format($row['amtpaid'],2).'</td>
<td align=right class="currency">$'.number_format($row['hudpay'],2).'</td>
<td>' . $row['datepaid'] . '</td>
<td align=right class="currency">$'.number_format($row['paidsum'],2).'</td>
<td>' . $row['comments'] . '</td>
</tr>';
}

echo '
<tr>
<td colspan=2></td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtdue')) . '</td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtpaid')) . '</td>
<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'hudpay')) . '</td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'paidsum')) . '</td>';
} ?>
</table class="print">
<div class="breakhere"></div>
</b></center></div></body></html> // line 86
===============
unexpected EOF on line **86**

gonna need you to put that in a code block for it to make sense.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head><body><center>
<div class="display print">

<?php
//Open a new connection to the MySQL server
$link = mysqli_connect("localhost", "root", "", "homedb"); 

// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
echo date('m/d/y');

//MySqli Select Query
$sql = "select * FROM paytbl";
$result = mysqli_query($link,$sql);
if (!$result) {
    printf("Error: %s\n", mysqli_error($link));
    exit();
}else{
   while($row = mysqli_fetch_array($result)){

//Set at what record to break at 25 records per page
$BreakAT=25;
$pagenum = 0;
echo date('m/d/y');

?>
<b>
<div class="title"><h3>Payment Report</h3></div><br />
<table align="center" cellspacing=5 cellpadding=5 border=2>
<thead>
<tr>
<th colspan=2> </th>
<th bgcolor="cyan" colspan=2>Rent</th>
<th bgcolor="99FF99">Prev</th>
<th bgcolor="FF99FF">late</th>
<th bgcolor="99FF99">sec</th>
<th></th>
<th bgcolor="99FF99">court</th>
<th></th>
<th bgcolor="99FF99">hud</th>
<th>Date</th>
<th colspan=2></th>
<tr>
<th>Tenant</th>
<th>Unit</th>
<th bgcolor="cyan">Due</th>
<th bgcolor="cyan">Paid</th>
<th bgcolor="99FF99">pay</th>
<th>Paid</th>
<th bgcolor="cyan">Paidsum</th>
<th>Comments</th>
</tr>
</thead>

<?php
$slice= array_slice($results,((($pagenum + 1) * $BreakAT) - $BreakAT), (($pagenum + 1)* $BreakAT));
foreach($slice as $row){

echo '
        <tr>
        <td>' . $row['tenant'] . '</td>
        <td>' . $row['unit'] . '</td> 
        <td align=right class="currency">$'.number_format($row['amtdue'],2).'</td>          
        <td align=right class="currency">$'.number_format($row['amtpaid'],2).'</td>      
        <td align=right class="currency">$'.number_format($row['hudpay'],2).'</td>                
        <td>' . $row['datepaid'] . '</td>
        <td align=right class="currency">$'.number_format($row['paidsum'],2).'</td>
        <td>' . $row['comments'] . '</td>
        </tr>';
}

echo '
<tr>
<td colspan=2></td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtdue')) . '</td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtpaid')) . '</td>
<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'hudpay')) . '</td>
<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'paidsum')) . '</td>'; 
} ?>
</table class="print">
<div class="breakhere"></div>
</b></center></div></body></html> // line 86

First, I would use PDO instead of mysqli in my opinion it’s easier in the long run and supports more databases.

Second this should get you started in the right direction (I believe)


if (isset($_GET['page']) && !empty($_GET['page'])) {

$current_page = urldecode($_GET['page']);

} else {

$current_page = 1;

}

$per_page = 25; // Total number of records to be displayed:

/* Total number of records that NEEDS to be displayed */

$total_count = totalRecords($pdo, 'cms');

/* calculate the offset */

$offset = $per_page * ($current_page - 1);

/* calculate total pages to be displayed */

$total_pages = ceil($total_count / $per_page);

/* Figure out the Pagination Links */

$links = links_function('index.php', $current_page, $total_pages);

/* Finally, call for the data from the database table to display */

$cms = readData($pdo, 'cms', 'blog', $per_page, $offset);
/*
 * Pagination Format
 * Read all the data from the database table in an array format
 */
function readData($pdo, $table, $page, $perPage, $offset) {
    $sql = 'SELECT * FROM ' . $table . ' WHERE page=:page ORDER BY date_added DESC LIMIT :perPage OFFSET :blogOffset';
    $stmt = $pdo->prepare($sql); // Prepare the query:
    $stmt->execute(['perPage' => $perPage, 'blogOffset' => $offset, 'page' => $page]); // Execute the query with the supplied data:
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

unexpected EOF (“End of File”) is PHP telling you you havent closed all your braces. Check your {'s and your }'s.

Indeed, the OP doesn’t close the } for their else clause.

A bit of consistent indenting would make it easier to see. I’m very rusty on HTML, but I think a lot of what’s in the code confusing matters could be split out to a style sheet and make things a lot clearer too. All that “align=” and “bgcolor=” isn’t necessary if the table is styled properly.

1 Like

I notice a number of things besides brackets and nesting issues. First there is an while($row = mysqli_fetch_array($result)){ statement wrapping most of the page. I also see array_slice() is looking for $results which is not defined. To fix both these issues I will define $results as an array and load our query results into this array.

	$results = array();
	while($row = mysqli_fetch_array($result)){
		$results[] = $row;
	}

The variable $pagenum was defined as zero and for pagination this would need to be both defined with a default value and also represent a value passed presumably with a url GET[KEY] e.g. ?page=2 So you would check if this GET[KEY] is set and if so use this value otherwise set a default page number of 1. It would look like this.

$pagenum = (isset($_GET['page']) ? $_GET['page'] : 1);

Now back to array_slice(). Because $pagenum is now being defined with actual page numbers we don’t need to make adjustment math to that value within this statement to get the Offset value. The Offset is simply

($pagenum * $BreakAT) - $BreakAT)

For the Length, no calculations are needed as you have defined records to show as $BreakAT. So now your slice statement looks like this.

$slice= array_slice($results,(($pagenum * $BreakAT) - $BreakAT), $BreakAT);

Now you just need some pagination code with links to send those page values to your page. I threw something together and tested at 2 records per page. It looks like this (minus your row of totals).


This should at least get pagination working for you. My edited version.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<center>
<div class="display print">

<?php
//Open a new connection to the MySQL server
$link = mysqli_connect("localhost", "root", "", "homedb"); 

// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
//echo date('m/d/y');

//MySqli Select Query
$sql = "select * FROM paytbl";
$result = mysqli_query($link,$sql);
if (!$result) {
    printf("Error: %s\n", mysqli_error($link));
    exit();
}else{	  
	$results = array();
	while($row = mysqli_fetch_array($result)){
		$results[] = $row;
	}

	//Set at what record to break at 25 records per page
	$BreakAT=2;
	$pagenum = (isset($_GET['page']) ? $_GET['page'] : 1); 
?>

	<div class="title"><h3>Payment Report</h3></div><br />
	<table align="center" cellspacing=5 cellpadding=5 border=2>
	<thead>
		<tr>
			<th colspan=2> </th>
			<th bgcolor="cyan" colspan=2>Rent</th>
			<th bgcolor="99FF99">Prev</th>
			<th bgcolor="FF99FF">late</th>
			<th bgcolor="99FF99">sec</th>
			<th></th>
			<th bgcolor="99FF99">court</th>
			<th></th>
			<th bgcolor="99FF99">hud</th>
			<th>Date</th>
			<th colspan=2></th>
		</tr>
		<tr>
			<th>Tenant</th>
			<th>Unit</th>
			<th bgcolor="cyan">Due</th>
			<th bgcolor="cyan">Paid</th>
			<th bgcolor="99FF99">Bal</th>
			<th bgcolor="FF99FF">chg</th>
			<th bgcolor="99FF99">dep</th>
			<th bgcolor="FF99FF">damage</th>
			<th bgcolor="99FF99">cost</th>
			<th bgcolor="FF99FF">nsf</th>
			<th bgcolor="99FF99">pay</th>
			<th>Paid</th>
			<th bgcolor="cyan">Paidsum</th>
			<th>Comments</th>
		</tr>
	</thead>
	
	<?php 
	$slice= array_slice($results,(($pagenum * $BreakAT) - $BreakAT), $BreakAT);
	foreach($slice as $row){
	
		echo '<tr>
			<td>' . $row['tenant'] . '</td>
			<td>' . $row['unit'] . '</td> 
			<td align=right class="currency">$'.number_format($row['amtdue'],2).'</td>          
			<td align=right class="currency">$'.number_format($row['amtpaid'],2).'</td>
			<td align=right class="currency">$'.number_format($row['prevbal'],2).'</td>
			<td align=right class="currency">$'.number_format($row['latechg'],2).'</td>
			<td align=right class="currency">$'.number_format($row['secdep'],2).'</td>
			<td align=right class="currency">$'.number_format($row['damage'],2).'</td>
			<td align=right class="currency">$'.number_format($row['courtcost'],2).'</td>
			<td align=right class="currency">$'.number_format($row['nsf'],2).'</td>
			<td align=right class="currency">$'.number_format($row['hudpay'],2).'</td>
			<td>' . $row['datepaid'] . '</td>
			<td align=right class="currency">$'.number_format($row['paidsum'],2).'</td>
			<td>' . $row['comments'] . '</td>
		</tr>';
	}
	
		echo '<tr>
			<td colspan=2></td>
			<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtdue')) . '</td>
			<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'amtpaid')) . '</td>
			<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'prevbal')) . '</td>
			<td bgcolor="FF99FF" align=right>' . array_sum(array_column($slice, 'latechg')) . '</td>
			<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'secdep')) . '</td>
			<td bgcolor="FF99FF" align=right>' . array_sum(array_column($slice, 'damage')) . '</td>
			<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'courtcost')) . '</td>
			<td bgcolor="FF99FF" align=right>' . array_sum(array_column($slice, 'nsf')) . '</td>
			<td bgcolor="99FF99" align=right>' . array_sum(array_column($slice, 'hudpay')) . '</td>
			<td bgcolor="cyan" align=right>' . array_sum(array_column($slice, 'paidsum')) . '</td>
		</tr>';	
	 
	echo '</table>';
} //  if (!$result) { }else{  


$pages = "";
$next = "";
$previous = "";

$currentpage = isset($pagenum) ? (integer)$pagenum : 1; 
$total = (!empty($results) ? count($results) : 0); 
$numofpages = (!empty($total) && !empty($BreakAT) ? ceil($total / $BreakAT) : 1); 


$first = '<a href="?page=1"> First</a> &nbsp; &nbsp; ';
$last = '&nbsp;&nbsp;<a href="?page='.$numofpages.'">Last</a>';
   
if ($currentpage != 1){ 
	$previous_page = $currentpage - 1;  		  
	$previous = '<a href="?page='.$previous_page.'"> Previous</a> &nbsp; &nbsp; ';      
}     
  
for ($a=1; $a<=$numofpages; $a++){ 

  	if($a > $currentpage - $BreakAT && $a < $currentpage + $BreakAT):
		if ($a == $currentpage){
			$pages .= "<b> $a </b>"; 
		}else{   
			$pages .= '<a href="?page='.$a.'" >&nbsp;'.$a.'&nbsp;</a>';  
		}
	endif;
	 
	
	if ($currentpage != $numofpages){
		$next_page = $currentpage + 1;  
		$next = '&nbsp;&nbsp;<a href="?page='.$next_page.'">Next</a>';  
	}else{
		$next = "";
	}
}
		
$plural = ($total > 1 ? 's' : ''); 
echo '<div class="pagingation"><strong>Page: '.$currentpage.' out of '.$numofpages.' pages containing '.$total.' record'.$plural.'</strong>
<br />'.$first.' '.$previous.' '.$pages.' '.$next.' '.$last.'</div>';	 
?>
<div class="breakhere"></div>
</div>
</center>
</body>
</html>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.