[thelist] My Query string not getting results in pages

J Paul Armstrong jpaul at jpauldesign.com
Mon Nov 18 15:51:01 CST 2002


I didn't create the multiple page results code, but got it from evolt.org.
You can see the code here :

http://evolt.org/article/Multiple_Pages_With_PHP/17/19340/index.html

I'm not sure what you're saying exactly, although I do understand the
dilemma of passing the query through my link; but I'm not sure of how to do
it another (since I didn't create this originally). All I saw was a $query
variable which wasn't defined in the original code, so I did what you see.
What ought the $query value/variable be so that the $query in the page link
works AND doesn't show the db query string?


Here's my complete PHP results page, this is long, so I apologize (and I
just edited some of what was already done, but I'm sure there's stuff wrong,
I'm not a PHP expert)...

<form action="resulttest.php" method="post">
<table class="eventContent" cellspacing="0" cellpadding="0" width="360">
<tr>
    <td class="eventContent"><b>Search For:</b><br>
        <select name="searchtype">
        <option value="all">Select</option>
        <option value="last">Last Name</option>
        <option value="first">First Name</option>
        <option value="title">Title</option>
        <option value="all">Search All</option>
        </select></td>
    <td align="left" valign="top" width="15"><img src="../images/spacer.gif"
width="15" height="15"></td>
    <td class="eventContent"><b>Keywords (optional):</b><br><input
name="searchterm" type="text"></td>
    <td><br>&nbsp;&nbsp;<input type="submit" name="Search"
value="Search"></td>
</tr>
<tr>
    <td colspan="3"><hr align="left" noshade size="1" width="100%"></td>
</tr>
</table>
</form>
<?
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

$db_host ="localhost";
$db_name ="mercantilelibrary_com";
$db_pass ="pass";
$db_usr ="user";

@$db = mysql_connect($db_host, $db_usr, $db_pass);

if (!$db)
{
echo "<span class=\"searchHead\">Error : Could not connect to the database.
Please try again later.";
exit;
}

mysql_select_db("mercantilelibrary_com");

if (!($limit))
{
$limit = 10;
}

if (!($pageno))
{
$pageno = 0;
}

if ($searchtype=="all")
{
$query="select * from book_catalog where last like '%".$searchterm."%' or
first like '%".$searchterm."%' or title like '%".$searchterm."%' or genre
like '%".$searchterm."%' order by last";
}

else
{
$query="select * from book_catalog where ".$searchtype." like
'%".$searchterm."%' order by last";
}

$result = mysql_query($query);
$num_results = mysql_num_rows($result);

if (!$result)
{
echo mysql_error();
exit;
}

if ($num_results==0)
{
echo "<span class=\"searchHead\">There are no matching results for your
search. Please try again.";
}

else {

echo "<span class=\"searchHead\">There are <b>".$num_results."</b> match(es)
to your search.<br><br>";
}

$pages = intval($num_results/$limit);


if ($num_results%$limit)
{
$pages++;
}

$current = ($pageno/$limit) + 1;

if (($pages < 1) || ($pages == 0))
{
$total = 1;
}

else
{
$total = $pages;
}

$firstpage = $pageno + 1;

if (!((($pageno + $limit) / $limit) >= $pages) && $pages != 1)
{
$lastpage = $pageno + $limit;
}

else
{
$lastpage = $num_results;
}
?>
<table width="360" border="0">
<tr>
<td class="searchHead" align="left">Results<b><?=$firstpage?></b> -
<b><?=$lastpage?></b> of <b><?=$num_results?></b></td>
<td class="searchHead" align="right">Page <b><?=$current?></b> of
<b><?=$total?></b></td>
</tr>
<tr>
<td class="searchHead" colspan="2" align="right">Results per-page: <a
href="<?=$PHP_SELF?>?query=<?=$query?>&pageno=<?=$pageno?>&limit=5">5</a> |
<a
href="<?=$PHP_SELF?>?query=<?=$query?>&pageno=<?=$pageno?>&limit=10">10</a>
| <a
href="<?=$PHP_SELF?>?query=<?=$query?>&pageno=<?=$pageno?>&limit=20">20</a>
| <a
href="<?=$PHP_SELF?>?query=<?=$query?>&pageno=<?=$pageno?>&limit=50">50</a>
</td>
</tr>
</table>
<?

$results = mysql_query("SELECT * FROM book_catalog WHERE ".$searchtype."
LIKE '%".$searchterm."%' ORDER BY last ASC LIMIT $pageno, $limit");

while ($row = mysql_fetch_array($results))
{

echo "<table cellspacing=\"1\" cellpadding=\"5\" border=\"0\"
width=\"320\">\n";
echo "<tr>\n";
echo "<td class=\"eventDes\" bgcolor=\"#D2CEB8\" width=\"250\">";
echo "<img src=\"images/spacer.gif\" width=250 height=1 border=0><br>";
echo htmlspecialchars(stripslashes($row["title"]));
echo "</td>\n";
echo "<td class=\"eventDes\" bgcolor=\"#D2CEB8\" width=\"70\">";
echo "<img src=\"images/spacer.gif\" width=70 height=1 border=0><br>";
echo htmlspecialchars(stripslashes($row["genre"]));
echo "</td>\n";
echo "<td class=\"text\" bgcolor=\"#eeeeee\" width=\"320\" rowspan=\"2\">";
echo "<a href=editbooks.php?field=";
echo htmlspecialchars(stripslashes($row["aid"]));
echo " target=control>Edit</a>";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class=\"eventContent\" bgcolor=\"#C3C3B3\" width=\"320\"
colspan=\"2\">";
echo "<img src=\"images/spacer.gif\" width=320 height=1 border=0><br>by ";
echo htmlspecialchars(stripslashes($row["first"]));
echo " ";
echo htmlspecialchars(stripslashes($row["last"]));
echo "&nbsp;&nbsp;<i>&copy; ";
echo htmlspecialchars(stripslashes($row["date"]));
echo "</i>";
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<table width=\"320\" height=\"1\" border=\"0\">";
echo "<tr height=\"1\">\n";
echo "<td height=\"1\"><img src=\"../images/spacer.gif\" width=\"360\"
height=\"1\" border=\"0\"><br></td>\n";
echo "</tr>\n";
echo "</table>\n";
}
?>
<p align="left">
<?
if ($pageno != 0)
{
$back_page = $pageno - $limit;
echo "<span class=searchHead";
echo"<a
href=\"$PHP_SELF?query=$query&pageno=$back_pageno&limit=$limit\">back</a>\n"
;
}

for ($i=1; $i <= $pages; $i++)
{
    $ppage = $limit*($i - 1);
    if ($ppage == $pageno)
    {
        echo "<span class=searchHead";
        echo "<b>$i</b> \n";
    }
    else
    {
        echo "<span class=searchHead";
        echo "<a
href=\"$PHP_SELF?query=$query&pageno=$ppage&limit=$limit\">$i</a> \n";
    }
}

if (!((($pageno+$limit) / $limit) >= $pages) && $pages != 1)
{
$next_page = $pageno + $limit;
echo "<span class=searchHead";
echo" <a
href=\"$PHP_SELF?query=$query&pageno=$next_page&limit=$limit\">next</a>\n";
}

?>





> J Paul Armstrong wrote:
>> I have a query string that selects based on the search the user does; which
>> works. When I've implemented the page/limit results, when you click on any
>> of the page numbers, the query string stops working. Here is the query ...
>
> Consider changing the way your paging works. I used 'barn' as my search
> string for all fields. When I click on '10' results per page it uses the
> url of (watch wrap, and I converted %20s to spaces):
>
> http://www.mercantilelibrary.com/catalog/resulttest.php?query=select *
> from book_catalog where last like '%barn%' or first like '%barn%' or
> title like '%barn%' or genre like '%barn%' order by last&pageno=0&limit=10
>
> It seems to me to be a very bad idea to pass a query in the url.  For
> example: if you execute a query that was passed in the url, there is
> nothing to keep me from changing this url to (watch wrap, and I
> converted %20s to spaces)
> http://www.mercantilelibrary.com/catalog/resulttest.php?query=drop
> book_catalog ...and you probably don't want me doing that.
>
>
> My recommendation would be to change the selection method for results
> per page to be a <select> object from those links.  This should allow
> you to give users the option of selecting a number of results in
> searchtest.php as well as in resulttest.php and searchterm searchtype
> and searchlimit will all be easily available to you.
>
> --Steve




++ jpaul design  |  simplify
        [ www.jpauldesign.com ]

        ++ 513.531.4849
           204.209.0219

        ++ 3763 mt vernon ave
           cincinnati, oh
           45209




More information about the thelist mailing list