Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorry, but really do need help with my pagination script

Status
Not open for further replies.

jasonindus

Programmer
Dec 7, 2006
27
GB
for the life of me i just can't figure out what is going wrong. the pagination seems to work a i get the next and previous links, but my results (looped table) doesn't appear, could be my query or something?


Code:
//db settings
$records_per_page = 4;


//find out how many records are in the table
$count_query = "SELECT count(*)
FROM  07_articles
WHERE supplier_id='1008'
AND deleted <> 'remove'";
$rh = mysql_query($count_query);
list ($record_count) = mysql_fetch_array($rh);

//calculate the maximum "page" that can be displayed.
$max_pages = floor($record_count / $records_per_page);

//This logic takes care of reacting to input.
if (isset($_GET['page']))
{
if ($_GET['page'] > 1)
{
if ($_GET['page'] > $max_pages)
{
$current_page = $max_pages;
}
else
{
$current_page = $_GET['page'];
}
}
else
{
$limit_start = 0;
$current_page = 1;
}

$limit_start = $current_page * $records_per_page;
}
else
{
$limit_start = 0;
$current_page = 1;
}


//echo 'max_pages'; echo ''.$max_pages.''; echo '<br>';



// Get data



//query the database for the required records
$data_query = "SELECT * 
FROM  07_articles
WHERE supplier_id='1008'
AND deleted <> 'remove'
ORDER by item_id DESC
DESC 
LIMIT " . $limit_start . ", " . $records_per_page;

$rh = mysql_query ($data_query);



// test start




echo '<table border="0" cellspacing="0" cellpadding="0">';

echo '<tr>';
echo '<td valign="top">';


echo '<table class="table_border_red" width="295" border="0" cellspacing="0" cellpadding="0">';

echo '<tr>';
echo '<td><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($current_page - 1) .' ">previous</a></td>';
echo '<td width="10"></td>';
echo '<td><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($current_page + 1) .' ">next</a></td>';
echo '</tr>';

echo '</table>';


echo '</td>';	 
echo '</tr>';


while ($content_row = mysql_fetch_array($rh)) {
//while($profile_row = mysql_fetch_array( $result )) {




echo '<tr>';
echo '<td valign="top"><img src="images/bullet_arrow_down.png"></td>';	 
echo '<td valign="top"><a href="?article='.$content_row['unique_id0'].'">'.$content_row['item_name'].'</a></td>';	 
echo '</tr>';

echo '<tr>';
echo '<td valign="top" height="10"></td>';	 
echo '</tr>';

}

echo '</table>';
 
Hi

That $max_pages will count only the full pages. If on the last page there will be less than $records_per_page items, it will not be included in $max_pages value. I suggest to change the function to round up instead of down.
Code:
$max_pages = [red]ceil[/red]($record_count / $records_per_page);
And please indent your code.

Feherke.
 
try this
Code:
<?
//db settings
$records_per_page = 4;


//find out how many records are in the table
$count_query = "SELECT count(*)
				FROM  07_articles
				WHERE supplier_id='1008'
				AND deleted <> 'remove'";
				
$result = mysql_query($count_query);
$record_count = mysql_result($result, 0, 0); //changed

//calculate the maximum "page" that can be displayed.
$max_pages = ceil($record_count / $records_per_page); //this needs to be ceiling not floor

//This logic takes care of reacting to input.
//in order to derive the desired page
$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
if ($current_page > $max_pages){
	$current_page = $max_pages;
} elseif($current_page < 1){
	$current_page = 1;
}

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

// Get data



//query the database for the required records
$data_query = "SELECT * 
				FROM  07_articles
				WHERE supplier_id='1008'
				AND deleted <> 'remove'
				ORDER by item_id DESC
				LIMIT $records_per_page
				offset $offset";

$rh = mysql_query ($data_query);



// test start




echo '<table border="0" cellspacing="0" cellpadding="0">';

echo '<tr>';
echo '<td valign="top">';


echo '<table class="table_border_red" width="295" border="0" cellspacing="0" cellpadding="0">';

echo '<tr>';
echo '<td><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($current_page - 1) .' ">previous</a></td>';
echo '<td width="10"></td>';
echo '<td><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($current_page + 1) .' ">next</a></td>';
echo '</tr>';

echo '</table>';


echo '</td>';     
echo '</tr>';


while ($content_row = mysql_fetch_assoc($rh)) {
//while($profile_row = mysql_fetch_array( $result )) {
echo '<tr>';
echo '<td valign="top"><img src="images/bullet_arrow_down.png"></td>';     
echo '<td valign="top"><a href="?article='.$content_row['unique_id0'].'">'.$content_row['item_name'].'</a></td>';    
echo '</tr>';

echo '<tr>';
echo '<td valign="top" height="10"></td>';     
echo '</tr>';

}

echo '</table>';
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top