Search engine won't page properly

The website I have been working on (as time permits) reports over
15,000 woods. With that much data, an internal search engine has
become mandatory for users to find what they are looking for. Almost
all the pages are dynamically formed using PHP and data stored in
MySQL files.

Therefore, a search engine that works on hard coded pages would be
totally unusable. I needed one that can search through MySQL tables.
I found a decent template for such a search engine at:
http://www.designplace.org/scripts.php?page=3D1&c_id=3D25
The original template works on searching one column in one table and
reporting only from that one column. The part of the data that by far
most users will wish to search is the species table. I wanted the search
report to show data keyed by one column but displaying multiple
columns --- so I knew I had some additional coding to do..
I
A while back I actually got a copy working for a while, broke it trying to
change it over to working with multiple column choices and then didn't
have a copy left of the code that worked.<live and learn :-[ >.

I have come a fair way to getting it to work but it won't page properly.
It has a parameter ($limit, presently set to 10 records) to control how
many records can be printed per page). It prints one page but gets
stuck. It has "<<prev" and "next>>" links for users to go backward or
forward in pages.

I have put in hours heavily adding internal documentation, studying the
logic till I understood most of it and plastering ECHO statements on
parameters to understand what is happening.

I think I know now what is wrong. $s is the record counter. Once its
value in multiples has a run over for the current page, a new page is
created by incrementing the record counter as the low end record for
the next page for forward travel and decrementing
for going backwards. The next page (or prior
page) is printed using $PHP_SELF statements
to run the same query again but in the prior or next page.

It took a lot of study , but as far as I can tell (..... and prove me wrong=
if
I am), when the program is turned back on itself
(using $PHP_SELF statements, whether for
incrementing or decrementing pages), the
value of $s near the top of the program gets lost. It has no chance to
accumulate (or reduce for backward travel). Therefore the program
gets locked into showing only the first page.

To test this, I hard set $s to a higher value, and ran a query. It
responded as it should, now reporting higher record numbers. As far as
I can see, repairing the paging problem seems to hinge on allowing
$s to carry over to be usable at the top of the program when
$PHP_SELF turns the program on itself.

What I am not familiar with is what the best way is to do that. What do
you suggest? Including code example if possible would be helpful?
If you also happen to spot any other serious bugs, please also let me
know. I am largely confident once $s can carry forward on each
program reiteration, the search engine will work as it should, good
paging included.

Sharing the Near Future
I have already done quite a bit of work on code (that works) that allows
users to choose what column to search on (eg. common names,
botanical names, country of origin, etc.). Once this paging problem is
overcome, It should be fairly easy to expand the search engine to work
on multiple search categories (ie. columns). By that point, it will make
the knowledge base website immensely more powerful and useful
for users to get versatile access to all its stored data. I really look
forward to that!

I will include all the code for this page below. I am also keeping the
large number of debug statements in and active and showing on the
output in a browser when the program is run --- so you can follow what
values parameters have at each step. The search page can be
directly viewed at:
http://www.prowebcanada.com/taxa/commonname_search.php?query string=3DOak&Sub=
mit=3DSearch

Much thanks. Your help makes it possible to go forward where
otherwise I would have a hard or impassible time.

Bill Mudry
Mississauga, Ontario

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D
[CODE]
<HTML>
<HEAD>
<!---
############################################################ ################=
################
This page contains the major search functions for The TAXA Wood Knowledge=
Base.
It is called from the main menu.
Important parameters include:
$searchtype - is the method users choose
to do a search as the English words
$searchfield - Each method translates into
the actual field in the 'species' table
used to complete the query
$querystring - is the actual query string submitted by the user.
$S is the record counter for the last record
of the present page. First appears in line 146
$newquerystring - The new query string
value fom the form by using $_GET[]
$limit - The allowed number of records
reported before starting a new page
connecttotaxa.php - holds the values needed
to connect to the taxa database
$searchfield - The variable to indicate
what field to search on. Right now there is only one,
but coding has already
been worked out for letting users choose from a list of
columns. To be added in the next version
$$numrows - The total number of rows in the present query result
$count - is the lower record number in the
next upcoming page (if one is needed)
It will always be one more than
$s which holds the highest record number for the
cuurent page.
$currPage - Calculates the current page number.
$Page - Like $currPage. I am not sure
yet what the relationship between them is.
$remainder - The query shows in blocks
of $limit records. If the total number of records
to report is divided by the
block size ($limit) and there is a remainder, then
a new page needs to be started to
finish off the last records in the report.
$news - Is the highest record number to use for the next page.
(..... or this is what I interpreted so far).

This search engine was adapted from the template search engine at
http://www.designplace.org/scripts.php?page=3D1&c_id=3D25
############################################################ ################=
################
--->

<TITLE>TAXA: Simple Common Name Search</TITLE>
<META name=3D"author" content=3D"Steve R, http://www.designplace.org/">
<style type=3D"text/css">

body {
background-color:'white'; color: 'navy';
font-family: verdana, arial, sans-serif; font-size: 80%;
};
..smalltext {font-size: 10px};
..largertext {font-size: 1.5em};
TD {font-size:0.8em}

H1 {font-size:2em} /* displayed at 24px */
H2 {font-size:1.5em} /* displayed at 18px */
H3 {font-size:1.25em} /* displayed at 15px */
H4 {font-size:1em} /* displayed at 12px */



</style>

</HEAD>
<!-- =A9 http://www.designplace.org/ -->

<!--
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX END OF
HEADER XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX START OF
BODY XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
-->

<BODY bgcolor=3D'ivory'>

<?php
//$s =3D 14; hard coded as a debug statement
$S=3D [at] $_GET['$s'] ;
Echo "\$s on line 69 is - $s<BR />"; //debug statement
?>

<H2 align=3D"center">Simple Common Name Search</H2>

<div align=3D"center" title=3D"formbox" id=3D"formbox">

<!--
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MAIN FORM STARTS
HERE XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
-->

<FORM name=3D"form" action=3D"commonname_search.php" method=3D"get">
<INPUT type=3D"text" name=3D"querystring" />
<INPUT type=3D"submit" name=3D"Submit" value=3D"Search" />
</FORM>

</div>

<?php

// Get the search variable from URL

$newquerystring =3D [at] $_GET['querystring'] ;
$querystring =3D trim($newquerystring); //trim
whitespace from the stored variable

=
/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXXXXXX SET THE NUMBER OF RECORDS PER PAGE HERE =
XXXXXXXXXXXXXX
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/
$limit=3D'10';

// check for an empty string and display a message.
if ($querystring =3D=3D "")
{
echo "<p align=3D'center'>Please enter a search string...</p>";
exit;
}

// check for a search parameter
if (!isset($newquerystring))
{
echo "<p align=3D'center'>We dont seem to have a search parameter!</p>";
exit;
}

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXXXXXXXXXXXXXXXXXXXX Connect to the
TAXA Database XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/

include ("connecttotaxa.php");
$connection =3D mysql_connect($hostname, $username, $password)
or die("Unable to connect to database server");


//specify database ** EDIT REQUIRED HERE **
mysql_select_db("taxa") or die("Unable to select
the TAXA database"); //select which database we're using



$searchfield =3D 'species_commonname';
//$searchfield =3D 'species_name';
//$searchfield =3D 'genus_name';
//$searchfield =3D 'species_description';
//$searchfield =3D 'location';

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXXXXXXXX BUILD THE SQL QUERY XXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D*/

// Build SQL Query
$searchquery =3D "SELECT *
FROM species
WHERE $searchfield like \"%$querystring%\"
order by '$searchfield'";

$numresults=3Dmysql_query($searchquery);
$numrows=3Dmysql_num_rows($numresults);

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D
XXXXXXXXXXXXXXX TEST FOR ZERO RESULTS AND
PRINT A MESSAGE OF THAT IF TRUE XXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D*/
if ($numrows =3D=3D 0)
{
echo "<h3>Search Results</h3>";
echo "<p>Sorry, your search: "" .
$querystring. "" returned zero results</p>";
}

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D
// $S is the record counter for the present record
// next determine if s has been passed to script, if not use 0
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D*/

if (empty($s)) {
$s=3D0;
}

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D
XXXXXXXXXXXXXXX QUERY THE DATABASE TO GET THE RESULTS =
XXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D*=
/

// get results
$searchquery .=3D " limit $s,$limit";
$result =3D mysql_query($searchquery) or die("Couldn't execute query");

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXXXXXXXX ECHO BACK TO THE USER WHAT
THEY ASKED TO SEARCH FOR XXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/
echo "<p align=3D'center'>You searched for: ""
.. $querystring . ""</p>";

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXXXXXXXX SET A TITLE FOR THE REPORT XXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D*/
echo "<H3 align=3D'center'>Search Results

</H3>";


// ADD 1 TO THE COUNTER FOR THE LATEST RECORD PRINTED FOR THE PRESENT PAGE
$count =3D 1 + $s ;

// now you can display the results returned

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D
XXXXXXXXXXXXXXX SET UP COLUMN HEADERS FOR THE REPORT =
XXXXXXXXXXXXXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D*/

Echo "<table align=3D'center' border=3D1 cellpadding=3D10 bgcolor=3D'white'>=
";
Echo "<tr colspan=3D3>";
Echo "<td bgcolor=3D'#EFDFCF'>";
Echo "<b>Common Name</b>";
Echo "</td>";

Echo "<td bgcolor=3D'#EFDFCF'>";
Echo "<b>Botanical Name</b>";
Echo "</td>";

Echo "<td bgcolor=3D'#EFDFCF'>";
Echo "<b>Authorities</b>";
Echo "</td>";

Echo "<td bgcolor=3D'#EFDFCF'>";
Echo "<b>Genus Name</b>";
Echo "</td>";

Echo "<td bgcolor=3D'#EFDFCF'>";
Echo "<b>Location</b>";Echo "</td>";

Echo "<td bgcolor=3D'#EFDFCF'>";
Echo "<b>Comments</b>";
Echo "</td>";

Echo "</tr>";
Echo "<tr>";
//Echo "<td>";

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXX PRINT OUT RECORDS TILL END OF QUERY XXXXXXXXX

************** WHILE LOOP STARTS HERE ******************
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D*/
while ($row=3D mysql_fetch_array($result)) {
$commonname =3D $row["species_commonname"];
$botname =3D $row["species_name"];
$authorities =3D $row["authorities_species"];

$genusname =3D $row["genus_name"];
$location =3D $row["location"];
$comments =3D $row["comments"];

ECHO "<td valign=3D'top' class=3Dsmalltext;>";
ECHO "$count.  $commonname
" ;
ECHO "</td>";

ECHO "<td valign=3D'top' class=3Dsmalltext;>";
ECHO "<a href=3D\"displayspecies.php?&species_name=3D$botname \">";
ECHO "$botname
";
ECHO "</a>.<br>\n";

ECHO "</td>";
ECHO "<td valign=3D'top' class=3Dsmalltext;>";
ECHO "$authorities
";
ECHO "</td>";

ECHO "<td valign=3D'top' class=3Dsmalltext;>";
ECHO "$genusname
";
ECHO "</td>";

ECHO "<td valign=3D'top' class=3Dsmalltext;>";
ECHO "$location
";
ECHO "</td>";

ECHO "<td class=3Dsmalltext;>";
ECHO "$comments
";
ECHO "</td>";

Echo "</tr>";
//Echo "<tr>";
//ECHO "</td>";

$count++ ;
$s++ ; // DOESN'T $S HAVE TO BE INCREMEENTED FOR EACH ROW PRINTED ALSO??
// ANOTHER COPY WORKED A FEW MONTHS AGO
WITHOUT IT. iT IS NOT IN THE TEMPLATE.
Echo "\$s on line 265 is - $s<BR />"; //debug statement
}
/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D
XXXXXXXXX END OF WHILE LOOP. XXXXXXXXX
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D*/
Echo "\$s on line 272 is - $s<BR />"; //debug statement



////////////////////////////////////////////////////////////
// END OF WHILE LOOP.
////////////////////////////////////////////////////////////

Echo "</tr>";
Echo "</table>";
////////////////////////////////////////////////////////////
// CLOSE THE REPORT TABLE
////////////////////////////////////////////////////////////

ECHO "<BR />";

Echo "\$s on line 288 is - $s<BR />"; //debug statement

$currPage =3D (($s/$limit) + 1);
//$pages =3D $currPage; //
ECHO "\$currPage on line 292 is - $currPage
"; //debug statement
Echo "\$s on line 286 is - $s<BR />"; //debug statement

//break before paging
echo "
";

// next we need to do the links to other results

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D----------
// If $s is less than 1, there are no more records to view backwards.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/
if ($s>=3D1) { // bypass PREV link if s is 0
ECHO "\$s at line 297 is - $s
"; //debug statement
$prevs=3D($s-$limit);
////////////////////////////////////////////////////////////
// This statement will run the prior page of the report.
// It is user chosen
////////////////////////////////////////////////////////////

//print " <a href=3D\"$PHP_SELF?s=3D$prevs&querystring=3D$var\"><=
<
print " <a href=3D\"$PHP_SELF?s=3D$prevs&querystring=3D$querystring\=
">
<p class=3D'largertext'><b><< Prev $limit</b></p></a>  ";

}



// calculate number of pages needing links
$pages=3Dintval($numrows/$limit);

ECHO "\$pages on line 313 is - $pages
"; //debug statement
ECHO "\$numrows on line 313 is - $numrows
"; //debug statement
ECHO "\$limit on line 313 is - $limit
"; //debug statement

// $pages now contains int of pages needed unless
there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
ECHO "\$pages on line 325 is - $pages
"; //debug statement

$remainder =3D ($numrows%$limit);
ECHO "\$remainder on line 327 is - $remainder
"; //debug statement

ECHO "
";

//$x =3D ($s+$limit)/$limit
//ECHO "\$x on line 296 is - $x
"; //debug statement
ECHO "\$s on line 332 is - $s
"; //debug statement
ECHO "\$limit on line 332 is - $limit
"; //debug statement
ECHO "\$pages on line 332 is - $pages
"; //debug statement

// check to see if last page
if (!((($s+$limit)/$limit)=3D=3D$pages) && $pages!=3D1) {

ECHO "
";

// not last page so give NEXT link
$news=3D$s+$limit;
ECHO "\$s on line 343 is - $s
"; //debug statement
ECHO "\$limit on line 343 is - $limit
"; //debug statement
ECHO "\$news on line 343 is - $news
"; //debug statement
ECHO "<div align=3D'right' style=3D'color:'#FF0000'>";

////////////////////////////////////////////////////////////
// This statement will run the next page of the report.
// It is user chosen
////////////////////////////////////////////////////////////
echo " <a
href=3D\"$PHP_SELF?s=3D$news&querystring=3D$querystring\"><p
class=3D'largertext' aligm=3D'right'><B>Next $limit >></b></p></a>";
ECHO "</div>";
}
Echo "\$s on line 366 is - $s<BR />"; //debug statement

?>

<!-- =A9 http://www.designplace.org/ -->

</BODY>
</HTML>

[/CODE]



--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Bill Mudry [ Fr, 04 Februar 2011 05:33 ] [ ID #2054604 ]

RE: Search engine won't page properly

Bill,

That's a lot of reading ... Anyway, this seems to be a 'general' PHP
question. If I understood you correctly, you're having problem getting PHP
to page the SQL results? If so, look the below code logic and adapt as
necessary:

$numOfResults = 10; // change this to your need
if( !empty($_GET['page']) && intval($_GET['page']) > 1 ) $currentPage =
intval($_GET['page']) - 1; // change the $_GET['page'] accordingly
elseif (!empty($_GET['page']) && strtolower(trim($_GET['page'])) == 'all')
$currentPage = 'all';
else $currentPage = 0;

$sqlQuery = 'SELECT * FROM my_table ';

If( $currentPage != 'all' ) $sqlQuery .= ' LIMIT
'.$currentPage*$numOfResults.', '.$numOfResults; // see [1]

$result = mysql_query($sqlQuery);

Would give you the following:

?page=all returns all results
?page=1 or ?page=0 or ?page= or ? yields 1st batch $numOfResults ( 1 to 10
)
?page=2 yields 2nd batch $numOfResults ( 11 to 20 )
?page=3 yields 3rd batch $numOfResults ( 21 to 30 )
?page=4 yields 4th batch $numOfResults ( 31 to 40 )

Etc....

Regards,
Tommy

[1] http://dev.mysql.com/doc/refman/5.1/en/select.html


--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tommy Pham [ Fr, 04 Februar 2011 06:11 ] [ ID #2054605 ]

RE: Search engine won't page properly

--=====================_1928409572==.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed

At 12:11 AM 04/02/2011, Tommy Pham wrote:
>Bill,
>
>That's a lot of reading ... Anyway, this seems to be a 'general' PHP
>question. If I understood you correctly, you're having problem getting PHP
>to page the SQL results? If so, look the below code logic and adapt as
>necessary:
>
>$numOfResults = 10; // change this to your need
>if( !empty($_GET['page']) && intval($_GET['page']) > 1 ) $currentPage =
>intval($_GET['page']) - 1; // change the $_GET['page'] accordingly
>elseif (!empty($_GET['page']) && strtolower(trim($_GET['page'])) == 'all')
>$currentPage = 'all';
>else $currentPage = 0;
>
>$sqlQuery = 'SELECT * FROM my_table ';
>
>If( $currentPage != 'all' ) $sqlQuery .= ' LIMIT
>'.$currentPage*$numOfResults.', '.$numOfResults; // see [1]
>
>$result = mysql_query($sqlQuery);
>
>Would give you the following:
>
>?page=all returns all results
>?page=1 or ?page=0 or ?page= or ? yields 1st batch $numOfResults ( 1 to 10
>)
>?page=2 yields 2nd batch $numOfResults ( 11 to 20 )
>?page=3 yields 3rd batch $numOfResults ( 21 to 30 )
>?page=4 yields 4th batch $numOfResults ( 31 to 40 )
>
>Etc....
>
>Regards,
>Tommy

Perhaps interesting code but when I said including code would help, I
was referring to
code for the best way of preserving the record counter, $s, when the
program the program
recycles back on itself on PHP_SELF. As mentioned, at present it does
not carry forward
the $s value so it can accumulate with the next page of records and
therefore gets stuck on
page one.

Thank you for your effort.

The general code is already there. In fact, if you were to take a
closer look, it is a fairly
sophisticated algorithm with a lot of checks and validations. It is
also very user friendly,
letting users click on "Next >>" to go ahead one page and "<< Prev"
to go backward one
page.

My testing shows that it should work well IF the value of the record
counter would only
transfer on restarting the page. I will try using SESSION today to
see if it will do the job.

Bill Mudry
MIssissauga


>[1] http://dev.mysql.com/doc/refman/5.1/en/select.html

--=====================_1928409572==.ALT--
Bill Mudry [ Fr, 04 Februar 2011 17:05 ] [ ID #2054606 ]

RE: Search engine won't page properly

Previously,

> [CODE]

<snip>

> <BODY bgcolor='ivory'>
>
> <?php
> //$s = 14; hard coded as a debug statement
> $S= [at] $_GET['$s'] ;

With the above, PHP is looking for the GET parameter name $s, not the value
of $s as the the GET parameter name. If you want the latter, change it to
$_GET[$s]. IIRC, PHP is not case specific, not like Java and C#.

> Echo "\$s on line 69 is - $s<BR />"; //debug statement
> ?>

<snip>

>
> [/CODE]


On Fri, Feb 4, 2011 at 8:05 AM, Bill Mudry <billmudry [at] rogers.com> wrote:
> At 12:11 AM 04/02/2011, Tommy Pham wrote:
>
> Bill,
>
> That's a lot of reading ... Anyway, this seems to be a 'general' PHP
> question. If I understood you correctly, you're having problem getting
PHP
> to page the SQL results? If so, look the below code logic and adapt as
> necessary:
>
> $numOfResults = 10; // change this to your need
> if( !empty($_GET['page']) && intval($_GET['page']) > 1 ) $currentPage =
> intval($_GET['page']) - 1; // change the $_GET['page'] accordingly
> elseif (!empty($_GET['page']) && strtolower(trim($_GET['page'])) == 'all')
> $currentPage = 'all';
> else $currentPage = 0;
>
> $sqlQuery = 'SELECT * FROM my_table ';
>
> If( $currentPage != 'all' ) $sqlQuery .= ' LIMIT
> '.$currentPage*$numOfResults.', '.$numOfResults; // see [1]
>
> $result = mysql_query($sqlQuery);
>
> Would give you the following:
>
> ?page=all returns all results
> ?page=1 or ?page=0 or ?page= or ? yields 1st batch $numOfResults ( 1 to
10
> )
> ?page=2 yields 2nd batch $numOfResults ( 11 to 20 )
> ?page=3 yields 3rd batch $numOfResults ( 21 to 30 )
> ?page=4 yields 4th batch $numOfResults ( 31 to 40 )
>
> Etc....
>
> Regards,
> Tommy
>
> Perhaps interesting code but when I said including code would help, I was
> referring to
> code for the best way of preserving the record counter, $s, when the
program
> the program
> recycles back on itself on PHP_SELF. As mentioned, at present it does not
> carry forward
> the $s value so it can accumulate with the next page of records and
> therefore gets stuck on
> page one.
>
> Thank you for your effort.
>
> The general code is already there. In fact, if you were to take a closer
> look, it is a fairly
> sophisticated algorithm with a lot of checks and validations. It is also
> very user friendly,
> letting users click on "Next >>" to go ahead one page and "<< Prev" to go
> backward one
> page.
>

I think you're over complicating things. Paging doesn't depend complicated
code algorithm but rather simple math deductions. See the below revised
code.

> My testing shows that it should work well IF the value of the record
counter
> would only
> transfer on restarting the page. I will try using SESSION today to see if
it
> will do the job.
>
> Bill Mudry
> Mississauga


Revised code:

$sqlSearchCount = "SELECT COUNT(*) AS Total FROM species WHERE $searchfield
like \"%$querystring%\" order by '$searchfield'"; /* look into [1] */

$result = mysql_query( $sqlSearchCount );
$totalRows = mysql_fetch_assoc('Total');

$numOfResults = 10;

$maxPages = ceil( $totalRows / $numOfResults );

if( !empty($_GET['page']) )
{
if( strtolower(trim($_GET['page'])) == 'all' )
{
$currentPage = 'all';
}
else {
$currentPage = intval($_GET['page']);
if( $currentPage > $maxPages ) $currentPage = 1;
elseif ( $currentPage > 1 ) $currentPage--;
else $currentPage = 1;
}
}
else $currentPage = 0;

$sqlSearch = "SELECT * FROM species WHERE $searchfield like
\"%$querystring%\" order by '$searchfield'";

if( $currentPage != 'all' ) $sqlSearch .= ' LIMIT
'.($currentPage-1)*$numOfResults.', '.$numOfResults;

$result = mysql_query( $sqlSearch );
/* code to display $result */

if( $currentPage > 1 ) echo '<a href="?page='.$currentPage -
1.'">Previous</a>';
if( $currentPage < $maxPages ) echo '<a href="?page='.$currentPage +
1.'">Next</a>';
if( $totalRows > 0 ) echo 'Showing '.($currentPage - 1)*$numOfResults+1.' to
'.( $currentPage < $maxPages ) ? ( $currentPage )*$numOfResults : $totalRows
..' of total: '.$totalRows;

NOTE: The code is not tested, but rather quickly cranked out based on
logic.

Regards,
Tommy

[1] http://php.net/mysql_escape_string



--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tommy Pham [ Sa, 05 Februar 2011 00:49 ] [ ID #2054661 ]

RE: Search engine won't page properly

> -----Original Message-----
> From: Tommy Pham [mailto:tommyhp2 [at] gmail.com]
> Sent: Friday, February 04, 2011 3:49 PM
> To: 'Bill Mudry'
> Cc: 'php-windows [at] lists.php.net'
> Subject: RE: [PHP-WIN] Search engine won't page properly
>
>
> Previously,
>
> > [CODE]
>
> <snip>
>
> > <BODY bgcolor='ivory'>
> >
> > <?php
> > //$s = 14; hard coded as a debug statement
> > $S= [at] $_GET['$s'] ;
>
> With the above, PHP is looking for the GET parameter name $s, not the
> value of $s as the the GET parameter name. If you want the latter, change
it
> to $_GET[$s]. IIRC, PHP is not case specific, not like Java and C#.
>
> > Echo "\$s on line 69 is - $s<BR />"; //debug statement
> > ?>
>
> <snip>
>
> >
> > [/CODE]
>
>
> On Fri, Feb 4, 2011 at 8:05 AM, Bill Mudry <billmudry [at] rogers.com> wrote:
> > At 12:11 AM 04/02/2011, Tommy Pham wrote:
> >
> > Bill,
> >
> > That's a lot of reading ... Anyway, this seems to be a 'general' PHP
> > question. If I understood you correctly, you're having problem
> > getting PHP to page the SQL results? If so, look the below code logic
> > and adapt as
> > necessary:
> >
> > $numOfResults = 10; // change this to your need if(
> > !empty($_GET['page']) && intval($_GET['page']) > 1 ) $currentPage =
> > intval($_GET['page']) - 1; // change the $_GET['page'] accordingly
> > elseif (!empty($_GET['page']) && strtolower(trim($_GET['page'])) ==
> > 'all') $currentPage = 'all'; else $currentPage = 0;
> >
> > $sqlQuery = 'SELECT * FROM my_table ';
> >
> > If( $currentPage != 'all' ) $sqlQuery .= ' LIMIT
> > '.$currentPage*$numOfResults.', '.$numOfResults; // see [1]
> >
> > $result = mysql_query($sqlQuery);
> >
> > Would give you the following:
> >
> > ?page=all returns all results
> > ?page=1 or ?page=0 or ?page= or ? yields 1st batch $numOfResults ( 1
> > to 10
> > )
> > ?page=2 yields 2nd batch $numOfResults ( 11 to 20 )
> > ?page=3 yields 3rd batch $numOfResults ( 21 to 30 )
> > ?page=4 yields 4th batch $numOfResults ( 31 to 40 )
> >
> > Etc....
> >
> > Regards,
> > Tommy
> >
> > Perhaps interesting code but when I said including code would help, I
> > was referring to code for the best way of preserving the record
> > counter, $s, when the program the program recycles back on itself on
> > PHP_SELF. As mentioned, at present it does not carry forward the $s
> > value so it can accumulate with the next page of records and therefore
> > gets stuck on page one.
> >
> > Thank you for your effort.
> >
> > The general code is already there. In fact, if you were to take a
> > closer look, it is a fairly sophisticated algorithm with a lot of
> > checks and validations. It is also very user friendly, letting users
> > click on "Next >>" to go ahead one page and "<< Prev" to go backward
> > one page.
> >
>
> I think you're over complicating things. Paging doesn't depend
complicated
> code algorithm but rather simple math deductions. See the below revised
> code.
>
> > My testing shows that it should work well IF the value of the record
> > counter would only transfer on restarting the page. I will try using
> > SESSION today to see if it will do the job.
> >
> > Bill Mudry
> > Mississauga
>
>
> Revised code:
>
> $sqlSearchCount = "SELECT COUNT(*) AS Total FROM species WHERE
> $searchfield like \"%$querystring%\" order by '$searchfield'"; /* look
into
> [1] */
>
> $result = mysql_query( $sqlSearchCount ); $totalRows =
> mysql_fetch_assoc('Total');
>
> $numOfResults = 10;
>
> $maxPages = ceil( $totalRows / $numOfResults );
>
> if( !empty($_GET['page']) )
> {
> if( strtolower(trim($_GET['page'])) == 'all' )
> {
> $currentPage = 'all';
> }
> else {
> $currentPage = intval($_GET['page']);
> if( $currentPage > $maxPages ) $currentPage = 1;
> elseif ( $currentPage > 1 ) $currentPage--;
> else $currentPage = 1;
> }
> }
> else $currentPage = 0;

Sorry, was in hurry to go run an errand didn't have time to review my
e-mail. That should be else $currentPage = 1;

>
> $sqlSearch = "SELECT * FROM species WHERE $searchfield like
> \"%$querystring%\" order by '$searchfield'";
>
> if( $currentPage != 'all' ) $sqlSearch .= ' LIMIT '.($currentPage-
> 1)*$numOfResults.', '.$numOfResults;
>
> $result = mysql_query( $sqlSearch );
> /* code to display $result */
>
> if( $currentPage > 1 ) echo '<a href="?page='.$currentPage -
> 1.'">Previous</a>'; if( $currentPage < $maxPages ) echo '<a
> href="?page='.$currentPage + 1.'">Next</a>'; if( $totalRows > 0 ) echo
> 'Showing '.($currentPage - 1)*$numOfResults+1.' to '.( $currentPage <
> $maxPages ) ? ( $currentPage )*$numOfResults : $totalRows .' of total:
> '.$totalRows;
>
> NOTE: The code is not tested, but rather quickly cranked out based on
logic.
>
> Regards,
> Tommy
>
> [1] http://php.net/mysql_escape_string



--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tommy Pham [ Sa, 05 Februar 2011 01:21 ] [ ID #2054662 ]

RE: Search engine won't page properly

> -----Original Message-----
> From: Tommy Pham [mailto:tommyhp2 [at] gmail.com]
> Sent: Friday, February 04, 2011 4:21 PM
> To: 'Bill Mudry'
> Cc: 'php-windows [at] lists.php.net'
> Subject: RE: [PHP-WIN] Search engine won't page properly
>


<snip>

> > $result = mysql_query( $sqlSearchCount ); $totalRows =
> > mysql_fetch_assoc('Total');

Argh... interruptions...

$row = mysql_fetch_assoc($result);
$totalRows = $row['Total'];

Anyway, you get the idea.

Happy coding,
Tommy


--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tommy Pham [ Sa, 05 Februar 2011 01:43 ] [ ID #2054663 ]
PHP » gmane.comp.php.windows » Search engine won't page properly

Vorheriges Thema: Problem in sending email from localhost
Nächstes Thema: xsl:include fails after upgrading PHP version; is libxml/libxsltversion mismatch the issue?