Hi,
I have a question some of you may find simple enough to answer. The code below works perfectly (hacked a bit from the PHPBuilder article on pagination) when on a "default" display, in which the SELECT simply lists the tables's records. However, when I submit more than two keywords the script for a search, mysql_num_rows doesn't report the number of resultset rows correctly (usually 1 or 0), so the whole pagination thing breaks down, among other small problems...even though the resulting display will grab the relevant records.
I've tried a number of permutations with the SELECTS, the mysql_num_rows and other calls, but the effect is always the same; upon submitting keywords, the script doesn't use the "prev -- next" pagination or $limit.
Thanks in advance for any help!
Mark
ps. I'm running:
(webserver) Red Hat 6.2, PHP 4.4pl1
(data server) Red Hat 6.2, MySQL 3.23.30-1
== code == appologies if tabs are off ==
require ("connect.php");
$keyword = trim($keywords);
$words = explode(" ",$keyword);
$num_words = count($words);
$limit = 2; // small number for testing with a short list
$search = implode($words, "+");
if (empty($offset) || $offset < 0) $offset = 0;
if (empty($index)) $index = 0;
while (list ($key, $val) = each ($words))
{
$numresults = mysql_query("SELECT count(contact_id) FROM contacts WHERE first LIKE '$val%' OR middle LIKE '$val%' OR last LIKE '$val%' OR company LIKE '$val%' GROUP BY contact_id");
$numrows = mysql_num_rows($numresults);
$results = mysql_query("SELECT contact_id,first,middle,last,company FROM contacts WHERE first LIKE '$val%' OR middle LIKE '$val%' OR last LIKE '$val%' OR company LIKE '$val%' GROUP BY contact_id ORDER BY last LIMIT $offset,$limit");
$num_row = mysql_num_rows($results);
$index++;
if ($row = mysql_fetch_array($results))
{
do
{
// this will show the results, if any
printf("<a href=\"%s?id=%s\"> %s, %s %s </a>( %s )<br />\n", $PHP_SELF, $row["contact_id"], $row["last"], $row["first"], $row["middle"], $row["company"]);
}
while ($row = mysql_fetch_array($results));
mysql_free_result($results);
}
else printf("no records found<br/>\n");
}
if ($offset != 0)
{
$prevoffset = $offset - $limit;
echo "<a onMouseOver=\"window.status='previous $limit results'; return true\"; href=\"$PHP_SELF?keywords=$search&offset=$prevoffset&index=$prevoffset\">[previous]</a> ";
}
else echo "[previous] ";
$pages = intval($numrows / $limit);
if ($numrows % $limit) $pages++;
for ($i = 1; $i <= $pages; $i++)
{
if (($offset / $limit) == ($i - 1)) echo "$i";
else
{
$newoffset = $limit * ($i - 1);
echo " <a onMouseOver=\"window.status='page $i results'; return true\"; href=\"$PHP_SELF?keywords=$search&offset=$newoffset&index=$newoffset\">$i</a>\n";
}
}
mysql_free_result($numresults);
if (!((($offset / $limit) + 1) == $pages) && $pages != 1)
{
$newoffset = $offset + $limit;
echo " <a onMouseOver=\"window.status='next $limit results'; return true\"; href=\"$PHP_SELF?keywords=$search&offset=$newoffset&index=$newoffset\">[next]</a><p>\n";
}
else echo " [next]";
printf("<p><form method=\"get\" action=\"$PHP_SELF\"><input type=\"text\" name=\"keywords\" value=\"$keywords\"> <input type=\"submit\" name=\"submit\" value=\"search\"></p>");
echo "$numrows matching records<br />"; // for testing
echo "$num_row records on page"; // for testing
==