pagination help needed

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
ploppy
Forum Newbie
Posts: 4
Joined: Tue Oct 16, 2007 4:09 am

pagination help needed

Post by ploppy »

hello. I am having trouble getting results to display from dropdown menu. when a category is selected in the dropdown it shows the first page of results with pagination links and showing 88 pages but when the next link is pressed it says, no results. yet i know they are there. i have pasted the code below and would be grateful if someone could offer some assistance or pointers where i have slipped up.

Code: Select all

<?PHP
function ShowMyFiles() {

// vars global configuration
global $dbConn, $theme_path;

// vars messages
global $msg;

// vars template
global $error_msg, $status, $files, $owner, $test, $paginated;

if ($err) {
$error_msg = 'Custom error message';
}
// get variable after selecting something from the dropdown with name 'chooser'
$select = $_POST['select'];

// if something has been chosen
if (!empty($select)) {

// get the chosen value
$chooser = $_POST['chooser'];

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 10;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

// get file listing
// if everything successful create query
// this selects all rows where the type is the one you chose in the dropdown
// * means that it will select all columns, ie name and type as i said above

$sql = "SELECT title, description, date FROM my_table WHERE category_id='$chooser' LIMIT $from, $max_results";
$result = $dbConn->Execute($sql);

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM my_table WHERE category_id='$chooser'"),0);

// Figure out the total number of pages. Always round up using ceil()
$lastpage = ceil($total_results / $max_results);

global $paginated;
$paginated .= "<center>Select a Page<br />";

if ($page == 1) {
$paginated .= " FIRST PREV ";
} else {
$paginated .= " <a href='{$_SERVER['PHP_SELF']}?page=1'>FIRST</a> ";
$prevpage = $page-1;
$paginated .= " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>PREV</a> ";
}
$paginated .= " ( Page $page of $lastpage ) ";

if ($page == $lastpage) {
$paginated .= " NEXT LAST ";
} else {
$nextpage = $page+1;
$paginated .= " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>NEXT</a> ";
$paginated .= " <a href='{$_SERVER['PHP_SELF']}?page=$lastpage'>LAST</a> ";
}

$test = '';
for($i = 0;$i < $result->RecordCount(); $i++){
$file = $result->Fields("title");
$description = $result->Fields("description");
$date = $result->Fields("date");

$test .= '<table width="100%%" border="0">
<tr>
<td></td>
</tr>
</table>

<table cellpadding="2" cellspacing="1" border="0" align="center" width="100%" class="tbl_border_mem">
<tr class="tbl_caption_mem">
<td width="12%"><strong>Title</strong></td>
<td width="73%"><strong>Description</strong></td>
<td width="15%"><strong>Date Added</strong></td>
</tr>
<tr class="tbl_caption_mem">
<td valign="top">'.$file.'</td>
<td valign="top">'.$description.'</td>
<td valign="top">'.$date.'</td>
</tr>
</table> <br />';

$result->movenext();

}
$files = $test;
DisplayTemplate($theme_path . "cp/data.html", "\$files,\$paginated,\$error_msg");
}
}
/*===================================================
main
===================================================*/

include "../application.php";

RunPreFilter(__FILE__);

ShowMyFiles();

RunPostFilter(__FILE__);

?>
and the html

Code: Select all

<form action="data.php" method="post">

<!-- This creates the dropdown in html -->
<select name="chooser">

<option value="1">List #1</option>
<option value="2">List #2</option>
<option value="3">List #3</option>

</select>

<input type="submit" value="Select" name="select">

</form>
thanks very much
ploppy
Forum Newbie
Posts: 4
Joined: Tue Oct 16, 2007 4:09 am

Post by ploppy »

glad to see i am not the only one that is stumped :-)

also tried this:

Code: Select all

<a href='{$_SERVER['PHP_SELF']}?page=$nextpage&select=$select'>NEXT</a>
still the same. any ideas?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

First suggestion, use

Code: Select all

<?php
$page = basename(__FILE__);
?>
instead of

Code: Select all

<?php
$page = $_SERVER['PHP_SELF'];
?>
PHP_SELF is not always set and is prone to XSS attacks.

Next, remember that pagination is typically driven by querystring, so you have to remember to always carry everything that was entered in the querystring back to itself for each page load when doing pagination.
ploppy
Forum Newbie
Posts: 4
Joined: Tue Oct 16, 2007 4:09 am

Post by ploppy »

hi everah. so how does that fit into this statement

Code: Select all

$paginated .= " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>NEXT</a> ";
also, i thought that the query string was carried forward from the $_GET command? many thanks for guidance. not sure what you mean by basename?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Generally on paging you have a script name (like results.php) which takes several inputs, like $start and $limit and uses them inside the result set or as query params. The thing to remember is that when you pass something to the page itself, it will need to be continually carried after that, so if I click a link to go to page 3, from there I need links to move to other pages, backward and forward.

Basename is a PHP function.
ploppy
Forum Newbie
Posts: 4
Joined: Tue Oct 16, 2007 4:09 am

Post by ploppy »

everah. sorry to be away with the fairies on this, but can you give me practical example of basename in the example posted previously. i haven't been with php too long. i learn better with practical example.many thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would first suggest reading the manual on the basename() function. That should clear it up a bit.

Really the function is used to clean the name of the current page (given by the PHP magic constant __FILE__.
Post Reply