Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help displaying search results with PREV NEXT

Status
Not open for further replies.

buspass

Programmer
Feb 2, 2003
29
GB
i am having problems displaying search results from a mysql database in blocks of 20 or so using prev and next links. I have read through old posts and got some sample code which worked in a test file, but not in my code..

I have copied some example code from my pages here with comments in to explain my problems.

I am sorry to include so much code, but i thought it would help you see what i've done so far. If you can copyy this into an editor you'll see the comments.

Any help would be really apppreciated.

code starts here:



<?
////////////////////// PAGE 1 /////////
// data entry form...page name &quot;data_entry.php&quot;

// extract of the data form..there are more fields in reality
$form =&quot;<BR><center><table width = \&quot;600\&quot; <BR> <form name =\&quot;form\&quot; method ='post' action='results.php'>&quot;;
$form .=&quot;<BR>Enter your search criteria in the fields below.&quot;;
$form.=&quot;<p>Surname: &quot;;
$form.=&quot;<br><input type=\&quot;text\&quot; name=\&quot;surname\&quot; size=\&quot;65\&quot; maxlength=\&quot;50\&quot;&quot;;
$form.=&quot;value=\&quot;$surname\&quot;>       &quot;;
$form.=&quot;<br>Firstname: &quot;;
$form.=&quot;<br><input type=\&quot;text\&quot; name=\&quot;firstname\&quot; size=\&quot;65\&quot; maxlength=\&quot;50\&quot;&quot;;
$form.=&quot;value=\&quot;$firstname\&quot;>&quot;;
$form.=&quot;<br>Email Address: &quot;;
$form.=&quot;<br><input type=\&quot;text\&quot; name=\&quot;email\&quot; size=\&quot;65\&quot; maxlength=\&quot;70\&quot;&quot;;
$form.=&quot;value=\&quot;$email\&quot;>&quot;;
$form.=&quot;<br><input type=\&quot;submit\&quot; value=\&quot;Search\&quot; >&quot;;
$form.=&quot;<br></td></tr></table>&quot;;
$form.=&quot;</form>&quot;;

echo($form);
?>
<script language=&quot;JavaScript&quot;>
document.form.surname.focus();
</script>


<?

////////////////////// PAGE 2 /////////

// This is the results page...page name &quot;results..php&quot;

// db connection data included here

// start $query off
$query = &quot;select * from myTable where &quot;;

// there then follow a series of field checking to see what's been filled in etc
// checks to see that more than one field has been completed. here is an example of only 3 fields. In reality there are more fields.

if ((!$surname) && (!$firstname) && (!$email) )
{
echo (&quot;<p><br><br>You have not completed any fields. <p>You can search by E-Mail without entering further criteria if you wish.<p>You cannot search on Surname, First Name without entering more information.<p>Fields must contain at least 2 characters.<p>You can use a wild card search by placing the * character at the end of the text.<p>Please go <a href=\&quot;javascript:history.back()\&quot;>back</a> to the search page.&quot;);
exit();
}

if ($surname) {check_length($surname);}
// this is an example, there are more in reality

// this just checks the number of characters and forces user back if there are too few.


if ($surname)
{ if (($firstname) || ($email))// if there is another field completed...
{ $and = &quot; and &quot;; }
else
{ $and = &quot;&quot;; }
check_star($surname, $and, $part, &quot;surname&quot; );// function to create the query
}


// this is an example of how i build the eventual query. Tests to see what fields have been completed..
// and constantly adds to the query string, eventually producing a valid $query.
// e.g SELECT * FROM myTable WHERE surname = 'testSurname' and firstname = 'testFirstname' and email = 'testEmail' etc

// from here on is where I inserted the sample code to try and display results in blocks using PREV and NEXT, and it's here i struggle
$limit=20;
$Prev = &quot;Previous&quot;;
$Next = &quot;Next&quot;;

$numresults=mysql_query($query);
echo(&quot;NUM RESULT $numresults&quot;);
$numrows=mysql_num_rows($numresults);

if (empty($offset)) {
$offset=0;
}


echo &quot;<table width=100% bgcolor=#FFFFFF border=0 cellpadding=2
cellspacing=2>&quot;;
$sql = $query;
echo(&quot;$SQL IS $sql&quot;);
$sql .= &quot; limit $offset,$limit &quot;;

echo(&quot;<br>$SQL IS now $sql&quot;);
$result = mysql_query($sql,$conn) or die(&quot;Could not execute
query.&quot;);
while ($row = mysql_fetch_array($result)) {

echo &quot;
<tr bgcolor=#CCCCCC><td><font color=#000000
size=1><b>$row[surname]\&quot;    \&quot;$row[firstname]\&quot;    \&quot;$row</b></font></td></tr>
&quot;;
}
mysql_free_result($result);


echo &quot;</table>&quot;;


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

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

for ($i=1;$i<=$pages;$i++) {
$newoffset=$limit*($i-1);
print &quot;<a href=\&quot;$PHP_SELF?offset=$newoffset\&quot;>$i</a> \n&quot;;
}


if ($offset>1) {
$prevoffset=$offset-$limit;
print &quot;<a href=\&quot;$PHP_SELF?offset=$prevoffset\&quot;>$Prev</a> \n&quot;;
}


if ($numrows>($offset+$limit)) {
$nextoffset=$offset+$limit;
print &quot;<a href=\&quot;$PHP_SELF?offset=$nextoffset\&quot;>$Next</a><p>\n&quot;;
}
/*
When you enter a surname on the data_entry form and click search, the records are returned to the limit of 20 as is set above, but when i click on NEXT, I see my customer error merssage promting me to fill in one of the fields. When the form reloads itself, (PHP_SELF) it runs through the validation checks again which in turn spoils the output.

There must be a better way to do this, but I have only been faced with displaying a few search results. Now I am working on something which needs the facility to display many results in blocks of 20 or so.

Question: Is there a way to fix things the way I have done them?

If not, can anyone suggest a better way to handle the search criteria from the user, validating it ensuring more than one field has been completed in certain cases (eg they can only search under email by itself. Other searches require more than 1 field to be completed if email is ommitted) and displaying search results.

I have posted examples of 2 pages. data_entry.php and results.php so you can get an idea. myTable contains surname, firstname and email columns for this excerise.

I am really sorry to post all this but I am totally stuck. I have bought 3 books on PHP/MYSQL and visited various sites for sample code (all of which work during test) but do not work in my site. The problem must be with my coding. PLease help if you can.





?>
 
When you use links for PREV and NEXT you will have to pass also the parameters that were used for the search or they will be lost.
I suggest that instead of doing that - which creates ugly URLs with long GET strings - use hidden fields that have the offset s value and make the links POST the entire form to the script.
For that use javascript in the link. <a href=&quot;javascript:document.form.navto.value='PREV'; document.form.submit();&quot;>PREV</a>
This way your search criteria will be there and you can do your calculations according to what is passed as the $navto value.
Another soultion is SESSION variables.
Read about sessions in the PHP documentation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top