Forum Moderators: coopster
This is what i'm essentially trying to do.
I'm reading a load of rows from a DB (lets say 97). I want to show 10 rows in 1 page. so that would be 10 pages with 9 having 10 entries and 1 with 7 pages.
I start off with reading all the entries at once. so i get 97 rows and store it in $result.
The first problem i have is how to get the number of pages. 97/10 returns 9.7 - is there any way i can get 9? I've found that the only way to do this in PHP is with
$pages = (( $numberOfEntries - ($numberOfEntries % $numEntryPerPage)) / $numEntryPerPage);
Secondly, how would i go about making the links to the page number on top? like say 1,2,..9,10
and Third, how would i read those from the $result? Like when i click on 2 how would i go about reading rows 11 to 20?
I dont expect code as such, just a pointer toward the pseudo code would help me understand the concept a lot.
Thanks
I've kinda completed the function. Could someone help me fix this error?
this is the read function, which is the function that reads the data from the DB.
function read()
{
global $numPerPage, $table, $page;head();
$result = mysql_query("SELECT COUNT(*) FROM $table");
$entries = mysql_result($result, 0, 0);//pager($entries, $numPerPage, $pageNum)
$pager = pagerData($entries, $numPerPage, $_GET['page']);
$startEntry = $pager->startEntry;
$pageNo = $pager->pageNum;
$numOfPages = $pager->numOfPages;
$numPerPage = $pager->numPerPage;print "<BR><BR>PAGE: " . $pageNo ;
$query = "SELECT * FROM $table ORDDER BY id DESC LIMIT $startEntry, $numPerPage";
$result = mysql_query($query);for ($i = 0; i < mysql_num_rows($result); $i++)
{
$thename = mysql_result($result,$i,"name");
$theemail = mysql_result($result,$i,"email");
print "<p><b>Name:</b> $thename<br><b>E-Mail: </b>$theemail</p>";
$i++;
}foot();
} // END read()
The pager data function is listed below
function pagerData($entries, $numPerPage, $pageNum)
{
$entries = (int) $entries;
$numPerPage = max((int) $numPerPage, 1);
$pageNum = (int) $pageNum;
$pageNum = max($pageNum, 1);
$pageNum = min($pageNum, $numOfPages);$numOfPages = ceil ($entries/$numPerPage);
$startEntry = ($pageNum - 1) * $NumPerPage;$return = new stdClass;
$return->startEntry = $startEntry;
$return->numPerPage = $numPerPage;
$return->numOfPages = $numOfPages;
$return->pageNum = $pageNum;return $return;
}
If i run it, i get the following error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/diabolos/public_html/programs/sandeep/index.php on line 103
I'm not sure what the error is about, but i presume the error is because the $result is invalid (which is why the number of rows cant be counted)
Could someone please help
cheers!
i presume the error is because the $result is invalid
That's correct. Normally, this is because the query is invalid. If the query finds zero rows, it is still valid. You typically get the invalid result resource error because there is a syntax error in your query, you are querying on coumns that don't exist, or something like that. The way to debug is to
1. echo your query. Often the problem is because one of your variables is not declared.
2. if you don't see the error, run the query in the mysql client until you get the right syntax. When you do, then go back to fixing the PHP part.
That's the general case. In your specific situation, at least the immediate problem is here:
$query = "SELECT * FROM $table ORDDER BY id DESC LIMIT $startEntry, $numPerPage";
ORDDER => ORDER
Fix that first and then apply the methods mentioned above if you still have problems.
Tom