| | |
paging & _POST variables
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
Hi there!
I finally managed to do paging BUT it works only when i manually give 'category' a value.
If i use $_POST[category] a warning occurs and no result is posted:
Warning: Invalid argument supplied for foreach() in
C:...\a.php on line 71
Here is my code in the a.php file:
<?php include("header.php"); php?>
<?php
require_once 'Pager.php';
$db = DB::connect('mysql://root
localhost/db');
$sql = "SELECT title,year
FROM document AS doc
WHERE year between 1995 and 2005
AND category='$_POST[category]'";
// Find our starting Row
$from = $_GET['from'];
if (!is_numeric($from)){
$from = 0; }
// We'll limit the rows to 10 per page
$limit = 2;
// Get the total number of rows and build our pager
$nrows =& $db->getRow('SELECT count(*)
FROM document AS doc
WHERE year between 1995 AND 2005
AND category='$_POST['category']');
$data = DB_Pager::getData($from, $limit, $nrows[0]);
// Grab 2 rows using PEAR:B limitQuery function
$res = $db->limitQuery($sql, $from, $limit);
// Display the rows
print("<p><HR width=100% size=4 noshade><p>");
// Build our pager
echo $data['numrows'] . ' Results found -- ';
echo 'displaying '. $data['limit'] . ' results per page<br><br>';
// Create a header showing which page we're on
echo '<h4> Page '. $data['current'] . ' of ' . $data['numpages'] . '</h4>';
print("<p><TABLE border=1>\n");
print("<TR><TD><center><b>year</b></center></TD>
<TD><b><center>title</center></b>
</TR>");
while ($row = $res->fetchRow()) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
print("<TD>$row[2]</TD>");
print("<TD>$row[1]</TD>");
}
print("</TR></TABLE><p>");
//echo "Jump to page: ";
// Unless we're on the first page, show a link to the previous page
if ($data['current'] != $data['firstpage']){
echo '<a href="'. "$PHP_SELF?from=" . $data['prev'] .'"><< Prev </a>';
}
// Direct link to pages
foreach ($data['pages'] as $page => $start_row) {
echo "[ <a href='$PHP_SELF?from=$start_row'>$page</a> ] ";
}
// Unless we're on the last page, show a link to the next page
if ($data['current'] != $data['lastpage']){
echo '<a href="'. "$PHP_SELF?from=" . $data['next'] . '">Next>> </a>';
}
?>
<?php include("footer.php"); php?>
Thanks for your time.
Cali
I finally managed to do paging BUT it works only when i manually give 'category' a value.
If i use $_POST[category] a warning occurs and no result is posted:
Warning: Invalid argument supplied for foreach() in
C:...\a.php on line 71
Here is my code in the a.php file:
<?php include("header.php"); php?>
<?php
require_once 'Pager.php';
$db = DB::connect('mysql://root
localhost/db');$sql = "SELECT title,year
FROM document AS doc
WHERE year between 1995 and 2005
AND category='$_POST[category]'";
// Find our starting Row
$from = $_GET['from'];
if (!is_numeric($from)){
$from = 0; }
// We'll limit the rows to 10 per page
$limit = 2;
// Get the total number of rows and build our pager
$nrows =& $db->getRow('SELECT count(*)
FROM document AS doc
WHERE year between 1995 AND 2005
AND category='$_POST['category']');
$data = DB_Pager::getData($from, $limit, $nrows[0]);
// Grab 2 rows using PEAR:B limitQuery function
$res = $db->limitQuery($sql, $from, $limit);
// Display the rows
print("<p><HR width=100% size=4 noshade><p>");
// Build our pager
echo $data['numrows'] . ' Results found -- ';
echo 'displaying '. $data['limit'] . ' results per page<br><br>';
// Create a header showing which page we're on
echo '<h4> Page '. $data['current'] . ' of ' . $data['numpages'] . '</h4>';
print("<p><TABLE border=1>\n");
print("<TR><TD><center><b>year</b></center></TD>
<TD><b><center>title</center></b>
</TR>");
while ($row = $res->fetchRow()) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
print("<TD>$row[2]</TD>");
print("<TD>$row[1]</TD>");
}
print("</TR></TABLE><p>");
//echo "Jump to page: ";
// Unless we're on the first page, show a link to the previous page
if ($data['current'] != $data['firstpage']){
echo '<a href="'. "$PHP_SELF?from=" . $data['prev'] .'"><< Prev </a>';
}
// Direct link to pages
foreach ($data['pages'] as $page => $start_row) {
echo "[ <a href='$PHP_SELF?from=$start_row'>$page</a> ] ";
}
// Unless we're on the last page, show a link to the next page
if ($data['current'] != $data['lastpage']){
echo '<a href="'. "$PHP_SELF?from=" . $data['next'] . '">Next>> </a>';
}
?>
<?php include("footer.php"); php?>
Thanks for your time.
Cali
Right, in the first few lines, I have a few comments.
Firstly, you cannot do this:
Second, the reuire_once function was called incorrectly. Here's how to do it:[php]<?php
require_once('Pager.php');
// ^-----------^---Note the parenthesis. =)
?>[/php]
Lastly, your POST variable is called wrong. Try this: [php]$sql = "SELECT title,year
FROM document AS doc
WHERE year between 1995 and 2005
AND category='" . $_POST['category'] . "';";
[/php]Hope my tips helped you. =)
Firstly, you cannot do this:
PHP Syntax (Toggle Plain Text)
<?php -CODE HERE- php?> // The above example is WRONG. You can do either of the following: <?php -CODE HERE- ?> --or-- <? -CODE HERE- ?> // for code snippets, or... <?=$var ?> // to echo a variable.
require_once('Pager.php');
// ^-----------^---Note the parenthesis. =)
?>[/php]
Lastly, your POST variable is called wrong. Try this: [php]$sql = "SELECT title,year
FROM document AS doc
WHERE year between 1995 and 2005
AND category='" . $_POST['category'] . "';";
[/php]Hope my tips helped you. =)
Geek and a Half Blog
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
Thank you for yuor time!
Well the parethensis is not obligatory, that works ok!
And the php?> also works fine. I'm also used to use ?> and not php?> in general.
Concerning the sql query,it is the first time I see using the ';'of the sql command in the query.That didn't cause any warning or problem. Queries can work without it as well. They do to me in other code. I have tried to write $_POST[] in different ways, none of them worked.
I can't understad if there is something else, and it's not the _POSt[] that I'm testing.
Well the parethensis is not obligatory, that works ok!
And the php?> also works fine. I'm also used to use ?> and not php?> in general.
Concerning the sql query,it is the first time I see using the ';'of the sql command in the query.That didn't cause any warning or problem. Queries can work without it as well. They do to me in other code. I have tried to write $_POST[] in different ways, none of them worked.
I can't understad if there is something else, and it's not the _POSt[] that I'm testing.
Probably you use $_POST[category] in your first db query rather than $_POST['category'].
You may want to tidy up your code by listing all the variables before query to db and other actions. eg. $category = $_POST['category']; etc for each variable to be used in the script.
You may want to tidy up your code by listing all the variables before query to db and other actions. eg. $category = $_POST['category']; etc for each variable to be used in the script.
Ecommerce-Web-Store.com Building Your e-Business.
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
Yes you are right, the '' are missing there. It is correct in my code that I'm testing, the problem is with the foreach function as I already said. And I can't find any connection with it.Here is what it posts:
Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Group\Apache2\htdocs\.....php on line....
Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Group\Apache2\htdocs\.....php on line....
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: errors in phpbb
- Next Thread: calling a SQL UDF?
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube





