•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,547 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,353 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1477 | Replies: 16 | Solved
![]() |
•
•
Join Date: Jul 2005
Posts: 20
Reputation:
Rep Power: 4
Solved Threads: 0
hi. i wonder if someone could check this script for me and tell why it isn't producing any results. here is the code:
it produces the number of pages, but when user clicks on next, it goes to next page but blank screen. this is part of a dropdown menu and the form is passing select into this script. many thanks.
php Syntax (Toggle Plain Text)
<?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'; } if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } // 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']; // 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) or die(mysql_error()); // 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__); ?>
it produces the number of pages, but when user clicks on next, it goes to next page but blank screen. this is part of a dropdown menu and the form is passing select into this script. many thanks.
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation:
Rep Power: 3
Solved Threads: 72
this might help, i never could test it.
Just tell me any errors and i can fix them
<?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';
}
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// 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'];
// 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 = mysql_query($sql) or die(mysql_error());
// Figure out the total number of results in DB:
$total_results = mysql_num_rows($result);
// Figure out the total number of pages. Always round up using ceil()
$lastpage = $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 = '';
while ($row = mysql_fetch_assoc($result)) {
$file = $row['title'];
$description = $row['description'];
$date = $row['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__);
?>Just tell me any errors and i can fix them
•
•
Join Date: Jul 2005
Posts: 20
Reputation:
Rep Power: 4
Solved Threads: 0
thank you ever so much for your help kkeith. however, i get this error:
this line refers to this statement
If i remove that statement, then the pagination only shows page 1of1 and no links. there should be links. there are over 700 entries that should be coming back.
Thanks for continuing help kkeith.
Fatal error: Call to a member function movenext() on a non-object in C:\wamp\www\public_html\cp\data.php on line 102
$result->movenext();
Thanks for continuing help kkeith.
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation:
Rep Power: 3
Solved Threads: 72
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation:
Rep Power: 3
Solved Threads: 72
i think i have solved your problem. i also remade the code without all the globals and template stuff.
Original Code Fixed (I think):
Here is the remake without all the extras:
All you have to do is fill in the mysql database connect stuff at the top of the script to make it work i hope
Original Code Fixed (I think):
<?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';
}
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// 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'];
// 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
$sql1 = "SELECT * FROM idx_reslink WHERE category_id = '" . $chooser . "'";
$result1 = mysql_query($sql1);
$sql = "SELECT title, description, date FROM idx_reslink WHERE category_id='$chooser' LIMIT $from, $max_results";
$result = mysql_query($sql) or die(mysql_error());
// Figure out the total number of results in DB:
$total_results = mysql_num_rows($result1);
// Figure out the total number of pages. Always round up using ceil()
$lastpage = $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 = '';
while ($row = mysql_fetch_assoc($result)) {
$file = $row['title'];
$description = $row['description'];
$date = $row['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 />';
}
}
$files = $test;
DisplayTemplate($theme_path . "cp/data.html", "\$files,\$paginated,\$error_msg");
}
/*===================================================
main
===================================================*/
include "../application.php";
RunPreFilter(__FILE__);
ShowMyFiles();
RunPostFilter(__FILE__);
?>Here is the remake without all the extras:
All you have to do is fill in the mysql database connect stuff at the top of the script to make it work i hope
<?php
function showFiles() {
$con = mysql_connect("localhost","username","password") or die('Couldn\'t connect to mysql');
mysql_select_db('db_name', $con) or die('Couldn\'t select database');
$page = $_GET['page'];
if (!isset($page) || $page == NULL) {
$page = 1;
}
else {
$page = $_GET['page'];
}
if ($_REQUEST['select'] !== NULL) {
$chooser = $_REQUEST['chooser'];
$num = 10;
$lim = $page * $num;
$lim = $lim - $num;
$sql = "SELECT * FROM idx_reslink WHERE category_id = '" . $chooser . "'";
$query = mysql_query($sql);
$tot = mysql_num_rows($query);
$sql = "SELECT title,description,date FROM idx_reslink WHERE category_id = '" . $chooser . "' LIMIT " . $lim . ", " . $num;
$query = mysql_query($sql);
$lpage = ($tot/$num);
$thispage = $_SERVER['PHP_SELF'];
if($page > 1) {
$prev = $page - 1;
$last = '<a href="' . $thispage . '?page=1">First</a> <a href="' . $thispage . '?page=' . $prev . '">PREV</a>';
}
if ($lpage > $page) {
$go = $page + 1;
$next = '<a href="' . $thispage . '?page=' . $go . '">NEXT</a> <a href="' . $thispage . '?page=' . $lpage . '">Last</a>';
}
echo '<table cellpadding="2" cellspacing="1" border="0" align="center" width="100%" class="tbl_border_mem">';
echo '<tr><td width="12%"><strong>Title</strong></td><td width="73%"><strong>Description</strong></td><td width="15%"><strong>Date Added</strong></td></tr>';
while ($row = mysql_fetch_assoc($query)) {
$title = $row['title'];
$description = $row['description'];
$date = $row['date'];
echo '<tr><td valign="top">' . $title . '</td><td valign="top">' . $description . '</td><td valign="top">' . $date . '</td></tr>';
}
echo '<tr><td>' . $last . '</td><td colspan="2" align="right">' . $next . '</td></tr>';
echo '</table>';
}
mysql_close($con);
}
showFiles();
?>•
•
Join Date: Jul 2005
Posts: 20
Reputation:
Rep Power: 4
Solved Threads: 0
kkeith. neither version work. both display pagination but when next is clicked, blank page. the function that the command $result->movenext(); is this:
i need to work within the template system of the first script because it is an integral part of the site in terms layout etc. if it would help, i can post the whole of the db.class file. thank you ever so much in trying to get this script to work and for your valuable time and effort. cheers
php Syntax (Toggle Plain Text)
function MoveNext() { if ($row = mysql_fetch_assoc($this->resultset)) { $this->current_row = $row; $this->EOF = false; return true; } else { $this->EOF = true; return false; } }]
i need to work within the template system of the first script because it is an integral part of the site in terms layout etc. if it would help, i can post the whole of the db.class file. thank you ever so much in trying to get this script to work and for your valuable time and effort. cheers
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation:
Rep Power: 3
Solved Threads: 72
•
•
Join Date: Jul 2005
Posts: 20
Reputation:
Rep Power: 4
Solved Threads: 0
db.class.php
php Syntax (Toggle Plain Text)
<? class DB_Connection { var $conn = null; var $debug = false; var $num_query = 0; var $error_msg = ""; var $affected_rows = 0; function DB_Connection($host, $username, $pwd, $db) { $this->Connect($host, $username, $pwd, $db); } function Connect($host, $username, $pwd, $db) { global $db_use_persistent; if ($db_use_persistent) { $this->conn = mysql_pconnect($host, $username, $pwd); } else { $this->conn = mysql_connect($host, $username, $pwd); } mysql_select_db($db, $this->conn); } function Execute($query) { $query = trim($query); $this->num_query++; if ($this->debug) { static $mysql_query_number; static $mysql_query_time; $mysql_query_number++; $time_start = (float) array_sum(explode(' ', microtime())); } $result = @mysql_query($query, $this->conn); if ($this->debug) { $time_end = (float) array_sum(explode(' ', microtime())); $time = $time_end - $time_start; $time = sprintf("%01.4f", $time); $mysql_query_time += $time; $mysql_query_time = sprintf("%01.3f", $mysql_query_time); if ($time > 1) { $time = "<b>$time [slow query]</b>"; } if (preg_match('/^select/i', $query)) { $num_rows = @mysql_num_rows($result); echo "<table width='100%' border='1' cellspacing='1'><tr valign='top'><td width='70'>QUERY #$mysql_query_number: </td><td>$query (result : $num_rows, time : $time)</td><td align='right' width='30'>$mysql_query_time</td></tr></table>"; } else { echo "<table width='100%' border='1' cellspacing='1><tr valign='top'><td width='70'>QUERY #$mysql_query_number: </td><td>$query (time : $time)</td></td><td align='right' width='30'>$mysql_query_time</td></tr></table>"; } if (mysql_error()) { echo "<table width='100%' border='1' cellspacing='1><tr valign='top'><td width='70'><font color='red'><b>ERROR : </b></font></td><td><font color='red'><b>".mysql_error()."</b></font></td><td align='right' width='30'>$mysql_query_time</td></tr></table>"; } } $this->error_msg = mysql_error(); if ($this->error_msg) { return false; } else { if (preg_match("/^update|insert|delete/msi", $query)) { $this->affected_rows = @mysql_affected_rows($this->conn); return new DB_Resultset_empty(); } else { return new DB_Resultset($result, $query); } } } function PageExecute($query, $pg_which, $pg_size) { if (!$pg_which) { $pg_which = 1; } $query_total = $query; if (!preg_match('/group by/msi', $query_total)) { $query_total = preg_replace("|select(.*?)from|ms", "select count(*) as c from", $query_total); } $result = $this->Execute($query_total); if (preg_match('/group by/msi', $query_total)) { $num_rows = $result->RecordCount(); } else { $num_rows = ($result->Fields('c')) ? $result->Fields('c') : 0; } $start = ($pg_which - 1) * $pg_size; $query = $query . " limit $start, $pg_size"; $result->Close(); $result = $this->Execute($query); $result->num_rows = $num_rows; return $result; } function InsertID() { return @mysql_insert_id($this->conn); } function Close() { return true; } function FetchArray($query) { $result = $this->Execute($query); $arr = array (); while ($row = $result->FetchRow()) { $arr[] = $row; } $result->Close(); return $arr; } function FetchOne($query) { $result = $this->Execute($query . ' limit 1'); $row = $result->FetchRow(); $result->Close(); return $row; } function Lookup($field, $table, $where) { $result = $this->Execute("select $field from $table where $where limit 1"); $value = $result->Fields($field); $result->Close(); return $value; } function CountQuery() { return $this->num_query; } function ErrorMsg() { return $this->error_msg; } function AffectedRows() { return $this->affected_rows; } } class DB_Resultset { var $resultset = null; var $num_rows = null; var $num_field = null; var $current_row = null; var $EOF = true; var $query = null; function DB_Resultset(&$resultset, $query = '') { $this->resultset = $resultset; $this->query = $query; if (preg_match('/^select|show|describe|explain/msi', $query)) { $this->MoveNext(); } } function RecordCount() { if (is_null($this->num_rows)) { $this->num_rows = @mysql_num_rows($this->resultset); } return $this->num_rows; } function FieldCount() { if (is_null($this->num_field)) { $this->num_field = mysql_num_fields($this->resultset); } return $this->num_field; } function FetchField($offset) { $field = mysql_fetch_field($this->resultset, $offset); $field->max_length = mysql_field_len($this->resultset, $offset); return $field; } function MetaType($field_type) { switch($field_type) { case preg_match('/char/i', $field_type): return 'C'; case preg_match('/int|float|double/i', $field_type): return 'I'; case preg_match('/text/i', $field_type): return 'X'; case preg_match('/blob/', $field_type): return 'B'; default: return ''; } } function Move($offset) { if (@mysql_data_seek($this->resultset, $offset)) { return $this->MoveNext(); } else { return false; } } function MoveFirst() { return $this->Move(0); } function MoveLast() { return $this->Move($this->num_rows - 1); } function MoveNext() { if ($row = mysql_fetch_assoc($this->resultset)) { $this->current_row = $row; $this->EOF = false; return true; } else { $this->EOF = true; return false; } } function Fields($name) { return $this->current_row[$name]; } function FetchRow() { if (!$this->EOF) { $row = $this->current_row; $this->MoveNext(); return $row; } else { return false; } } function Close() { if ($this->resultset) { mysql_free_result($this->resultset); } } } class DB_Resultset_empty { var $resultset = null; var $num_rows = null; var $num_field = null; var $current_row = null; var $EOF = true; var $query = null; function DB_Resultset() { return true; } function RecordCount() { return 0; } function FieldCount() { return 0; } function FetchField($offset) { return false; } function MetaType($field_type) { return false; } function Move($offset) { return false; } function MoveFirst() { return false; } function MoveLast() { return false; } function MoveNext() { return false; } function Fields($name) { return false; } function FetchRow() { return false; } function Close() { return true; } }

