Forum Moderators: coopster

Message Too Old, No Replies

pagination help

         

ploppy

6:56 pm on Oct 14, 2007 (gmt 0)

10+ Year Member



hello. i have pagination sorted out, but i need to declare a variable to declare in html page so i can put the pagination where i want it. here is the php code:

<?php
function ShowMyFiles() {

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

// vars messages
global $msg;

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

if ($err) {
$error_msg = $msg['20191'];
}

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
$sql = "SELECT title, description, date FROM idx_reslink where category_id=1 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 idx_reslink where category_id=1"),0);

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

echo "<center>Select a Page<br />";

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

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

$test = '';
for($i = 0;$i < $result->RecordCount(); $i++)
{
$date = $result->Fields("date");
$description = $result->Fields("description");
$title = $result->Fields("title");
$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="16%"><strong>Date</strong></td>
<td width="51%"><strong>Description</strong></td>
<td width="13%"><strong>Date Added</strong></td>
</tr>
<tr class="tbl_caption_mem">
<td valign="top">'.$title.'</td>
<td valign="top">'.$description.'</td>
<td valign="top">'.$date.'</td>
</tr>
</table>';

$result->movenext();
}
$files = $test;
$paginate = $total;
DisplayTemplate($theme_path . "cp/cprm1.html", "\$files,\$paginate,\$error_msg");
}

/*===================================================
main
===================================================*/


include "../application.php";

RunPreFilter(__FILE__);

ShowMyFiles();

RunPostFilter(__FILE__);

?>

this is the area i need to look at:

echo "<center>Select a Page<br />";

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

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

so on my html i can call it like <%$pagination%> or whatever. looked at doing a function, but php not good enough alas :-( many thanks if someone could give me a pointer.

jatar_k

7:03 pm on Oct 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



as it is you have written it as a function. Are you asking how you could use it anywhere in the pagination?

You might want to include some parameters to that function such as

what number of the set your function should start with
how many items per page

those could then be passed into your function and it could be used anywhere.

sorry if I did not understand correctly

ploppy

10:06 pm on Oct 15, 2007 (gmt 0)

10+ Year Member



thanks for reply jatar. i have updated the script to a 99% working dropdown pagination, but although it displays the total pages, when a link is clicked, it says no results. here is the code for the script:

<?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 idx_reslink 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 idx_reslink 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:


<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>

i am stuck as to why it is not showing results although the number of pages is there. many thanks

ploppy

8:42 am on Oct 16, 2007 (gmt 0)

10+ Year Member



bump

jatar_k

12:00 pm on Oct 16, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it's obviously missing something. Does it have access to the search term or criteria?

Habtom

12:10 pm on Oct 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is more likely that it is as jatar_k mentioned above.

On the subsequent pages are you passing this value?
$_POST['select']

You are passing it at the first instance, and that is not enough for the pages following.

ploppy

2:08 pm on Oct 16, 2007 (gmt 0)

10+ Year Member



sorry habtom, do not understand. if i take the dropdown function out altogether, the script works fine. as far as i am aware there is nothing missing from the script and all db queries are correct. Habtom, can you explain a little more about your theory? cheers

Habtom

2:13 pm on Oct 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you need to pass the select somehow like this:

<a href='{$_SERVER['PHP_SELF']}?page=$nextpage&select=$select'>NEXT</a>

Habtom

[edit] Find the relevant part of that code, and check the select can indeed carry your value [/edit]

[edited by: Habtom at 2:14 pm (utc) on Oct. 16, 2007]

ploppy

2:53 pm on Oct 16, 2007 (gmt 0)

10+ Year Member



still the same habtom. this has me totally stumped. where now? cheers

akameng

10:20 pm on Oct 16, 2007 (gmt 0)

10+ Year Member



Can you show up some results for this script? and tell wwhat exactly you need to do?