Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Help displaying many search results using Prev Next Links

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Help displaying many search results using Prev Next Links

Postby buspass » Sun Mar 09, 2003 2:40 am

First, sorry for posting so much here. I have search your previous posts for an existing answer and only found one which came close, but didn't help me. I am now desperate for an answer to this problem.

I,m struggling getting search results to display in blocks of 20 or so. The code below is an example of 2 of my pages. The first page get user input as search criteria, it is then passed by POST to the second page where it is validated, processed into a query and displayed.

The first block of results display fine, and so do the prev 1234...next links. BUT, when i click next or prev links, the page reloads (PHP_SELF) and runs through my own validation again, outputting my own error message telling the user to enter something in a field.

I guess my coding may lead a lot to be desired, but when i wrote the pages, i did not have the need for many search results.. I do now!!

Is there a way to fix things so when i click prev/next the next block of search results are displyed correctly?

I've included some comments to explain what's happening along the way if it helps.

Thank you in advance.



<?
////////////////////// PAGE 1 /////////
// data entry form...page name "data_entry.php"

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

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


<?

// This is the results page...page name "results..php"

// db connection data included here

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

// 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 ("<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=\"javascript:history.back()\">back</a> to the search page.");
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 = " and "; }
else
{ $and = ""; }
check_star($surname, $and, $part, "surname" );// 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 = "Previous";
$Next = "Next";

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

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


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

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

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


echo "</table>";


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

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

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


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


if ($numrows>($offset+$limit)) {
$nextoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$nextoffset\">$Next</a><p>\n";
}
/*
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.

*/


?>
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am

Postby Xerpher » Sun Mar 09, 2003 10:13 am

I was fixing up your code for you, but first thing I'd like to know is what these do: check_length(), check_star(), If they aren't too important then its ok... I'll post your code back with a couple fix shortly.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby Xerpher » Sun Mar 09, 2003 10:42 am

Ok, heres what I got.... btw, I kept the vars outside the strings because I've had many problems with which that was the cause, and you should get used to writting variables in full form (ex: $_SERVER['PHP_SELF'] not $PHP_SELF) since new versions of PHP practically force it.

I can't test it and I'm not any expert so anymore problems just repost here or email me (xerpher@terradesign.ws)

Code: Select all
<?php
// This is the results page...page name "results..php"
// db connection data included here
// start $query off
$query   = "select * from myTable where ";
// 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 (!(isset($_POST['surname']) && isset($_POST['firstname']) && isset($_POST['email']) || isset($_GET['offset']))){
   echo("<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=\"javascript:history.back()\">back</a> to the search page.");
   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 = " and ";
   } else {
      $and = "";
   }
   check_star($surname, $and, $part, "surname" );// 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   = "Previous";
$Next   = "Next";
$numrows= mysql_num_rows($query);
echo("NUM RESULTS $numrows");
if (empty($_GET['offset'])) $dboffset = 0; else $dboffset = $_GET['offset'];
echo "<table width=100% bgcolor=#FFFFFF border=0 cellpadding=2 cellspacing=2>";
$sql = $query;
echo("\$SQL IS $sql");
$sql .= " limit ".$dboffset.", ".$limit;
echo("<br>$SQL IS now $sql");
$result = mysql_query($sql,$conn) or die("Could not execute query.");
while ($row = mysql_fetch_array($result)) {
   print "
   <tr bgcolor=#CCCCCC><td><font color=#000000
   size=1><b>".$row['surname']."\"&&&&\"".$row['firstname']."\"&&&&\"".$row['email']."</b></font></td></tr>";
}
mysql_free_result($result);
echo "</table>";
$pages = intval($numrows/$limit);
if ($numrows%$limit) {
   $pages++;
}
for ($i = 1; $i <= $pages; $i++) {
   $newoffset = $limit * ($i - 1);
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$newoffset.'">'.$i.'</a> \n';
}
if ($offset > 1) {
   $prevoffset = $dboffset - $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$prevoffset.'">'.$Prev.'</a> \n';
}
if ($numrows > ($offset + $limit)) {
   $nextoffset = $dboffset + $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$nextoffset.'">'.$Next.'</a><p>\n';
}
/*
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.
*/
?>
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby buspass » Sun Mar 09, 2003 11:39 am

thank for your efforts. i copied your code in and tested it with the real db connection and table name and it successfully displayed results, but no prev next links were displayed on the page, even though there were many results (It displayed first 20)

I also saw a mysql warning mesage:

mysql_num_rows () supplied argument is not a valid musql result resource.
(However,i was seeing this with my own code)

The two functions you ask about just test the length of the data entered and build the query depending on what fields have been completed.

Any suggestions?
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am

Postby Xerpher » Sun Mar 09, 2003 1:09 pm

Ok, I think I fixed a couple more things, try this:

Code: Select all
<?php
// This is the results page...page name "results..php"
// db connection data included here
// start $query off
// 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.
$query   = "select * from myTable where ";
if (!(isset($_POST['surname']) && isset($_POST['firstname']) && isset($_POST['email']) || isset($_GET['offset']))){
   echo("<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=\"javascript:history.back()\">back</a> to the search page.");
   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 = " and ";
   } else {
      $and = "";
   }
   check_star($surname, $and, $part, "surname" );// 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   = "Previous";
$Next   = "Next";
$rows   = mysql_query($sql,$conn);
$numrows= mysql_num_rows($rows);
echo("NUM RESULTS $numrows");
if (empty($_GET['offset'])) $dboffset = 0; else $dboffset = $_GET['offset'];
echo "<table width=100% bgcolor=#FFFFFF border=0 cellpadding=2 cellspacing=2>";
$sql = $query;
echo("\$SQL IS $sql");
$sql .= " limit ".$dboffset.", ".$limit;
echo("<br>$SQL IS now $sql");
$result = mysql_query($sql,$conn) or die("Could not execute query.");
while ($row = mysql_fetch_array($result)) {
   print "
   <tr bgcolor=#CCCCCC><td><font color=#000000
   size=1><b>".$row['surname']."\"&&&&\"".$row['firstname']."\"&&&&\"".$row['email']."</b></font></td></tr>";
}
mysql_free_result($result);
echo "</table>";
$pages = intval($numrows/$limit);
if ($numrows%$limit) {
   $pages++;
}
for ($i = 1; $i <= $pages; $i++) {
   $newoffset = $limit * ($i - 1);
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$newoffset.'">'.$i.'</a> \n';
}
if ($offset > 1) {
   $prevoffset = $dboffset - $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$prevoffset.'">Prev</a> \n';
}
if ($numrows > ($offset + $limit)) {
   $nextoffset = $dboffset + $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$nextoffset.'">Next</a><p>\n';
}
/*
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.
*/
?>
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby buspass » Sun Mar 09, 2003 1:24 pm

copied your code,and included my dbcionnection and function includes.

20 results displayed, but no prev or next liinks.

Following error displayed at top of page:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp\www\results.php on line 31

is this a 0 error problem with mysql_num_rows, something to do with the internal pointer maybe?
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am

Postby Xerpher » Sun Mar 09, 2003 1:36 pm

I made a couple more mods to the code but I think your problem is in your query. you should maybe check your outputted query. Aside from that.... I'm at a loss at the moment...

Code: Select all
<?php
// This is the results page...page name "results..php"
// db connection data included here
// start $query off
// 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.
$query   = "select * from myTable where ";
if (!(isset($_POST['surname']) && isset($_POST['firstname']) && isset($_POST['email']) || isset($_GET['offset']))){
   echo("<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=\"javascript:history.back()\">back</a> to the search page.");
   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 = " and ";
   } else {
      $and = "";
   }
   check_star($surname, $and, $part, "surname" );// 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;
$result   = mysql_query($sql,$conn);
$numrows= mysql_num_rows($result);
echo("NUM RESULTS $numrows");
if (isset($_GET['offset'])) $dboffset = $_GET['offset']; else $dboffset = 0;
echo "<table width=100% bgcolor=#FFFFFF border=0 cellpadding=2 cellspacing=2>";
$sql = $query;
echo("\$SQL IS $sql");
$sql .= " limit ".$dboffset.", ".$limit;
echo("<br>$SQL IS now $sql");
$result = mysql_query($sql,$conn) or die("Could not execute query.");
while ($row = mysql_fetch_array($result)) {
   print "
   <tr bgcolor=#CCCCCC><td><font color=#000000
   size=1><b>".$row['surname']."\"&&&&\"".$row['firstname']."\"&&&&\"".$row['email']."</b></font></td></tr>";
}
mysql_free_result($result);
echo "</table>";
$pages = intval($numrows/$limit);
if ($numrows%$limit) {
   $pages++;
}
for ($i = 1; $i <= $pages; $i++) {
   $newoffset = $limit * ($i - 1);
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$newoffset.'">'.$i.'</a> \n';
}
if ($dboffset > 0) {
   $prevoffset = $dboffset - $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$prevoffset.'">Previous</a> \n';
}
if ($numrows > ($dboffset + $limit)) {
   $nextoffset = $dboffset + $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$nextoffset.'">Next</a><p>\n';
}
/*
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.
*/
?>
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby buspass » Sun Mar 09, 2003 1:50 pm

i still get the mysql_num_rows error.

But, i substituted $numrows= mysql_num_rows($rows)
with
$numrows = mysql_result(mysql_query("SELECT COUNT(record_id) FROM myTable"),0);
and i get the links visible and no error message.

However, when i clicked the next link, i gat a message saying could not execute query.

I echoed the query and it just said.."select * from myTable where limit 20, 20. It has 'lost' the values that were there the first time around.

Counld it be the way i build the query?

function check_star($name, $and, $part, $field)
{
global $query;
$extra = "%";
$first_char = substr(strrev($name),0,1);
if ($first_char == "*") // yes it is a wild card query
{
$rev_name = strrev(substr(strrev($name), 1, strlen($name)));
$param = "LIKE"; // use LIKE instead of =
$name = $rev_name . $extra; // append % to the $name value
$query .= "$field $param '$name' $and";

}
else // first character is not a * so it is an exact match query
{
$param = "=";
$name = $name;
$query .= "$field $param '$name' $and";

}
}// end of function
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am

Postby Xerpher » Sun Mar 09, 2003 2:01 pm

try this:
Code: Select all
<?php
// This is the results page...page name "results..php"
// db connection data included here
// start $query off
// 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.
$query   = "select * from myTable where ";
if (!(isset($_GET['surname']) && isset($_GET['firstname']) && isset($_GET['email']) || isset($_GET['offset']))){
   echo("<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=\"javascript:history.back()\">back</a> to the search page.");
   exit();
}
if ($_GET['surname']) check_length($_GET['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 ($_GET['surname']){
   if (($_GET['firstname']) || ($_GET['email'])){// if there is another field completed...
      $and = " and ";
   } else {
      $and = "";
   }
   check_star($_GET['surname'], $and, $part, "surname" );// 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;
$numrows = mysql_result(mysql_query("SELECT COUNT(record_id) FROM myTable"),0);
echo("NUM RESULTS $numrows");
if (isset($_GET['offset'])) $dboffset = $_GET['offset']; else $dboffset = 0;
echo "<table width=100% bgcolor=#FFFFFF border=0 cellpadding=2 cellspacing=2>";
$sql = $query;
echo("\$SQL IS $sql");
$sql .= " limit ".$dboffset.", ".$limit;
echo("<br>$SQL IS now $sql");
$result = mysql_query($sql,$conn) or die("Could not execute query.");
while ($row = mysql_fetch_array($result)) {
   print "
   <tr bgcolor=#CCCCCC><td><font color=#000000
   size=1><b>".$row['surname']."\"&&&&\"".$row['firstname']."\"&&&&\"".$row['email']."</b></font></td></tr>";
}
mysql_free_result($result);
echo "</table>";
$pages = intval($numrows/$limit);
if ($numrows%$limit) {
   $pages++;
}
for ($i = 1; $i <= $pages; $i++) {
   $newoffset = $limit * ($i - 1);
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$newoffset.'">'.$i.'</a> \n';
}
if ($dboffset > 0) {
   $prevoffset = $dboffset - $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$prevoffset.'">Previous</a> \n';
}
if ($numrows > ($dboffset + $limit)) {
   $nextoffset = $dboffset + $limit;
   print '<a href="'.$_SERVER['PHP_SELF'].'?offset='.$nextoffset.'">Next</a><p>\n';
}
/*
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.
*/
?>

And replace data_entry.php with this:
Code: Select all
<?
////////////////////// PAGE 1 /////////
// data entry form...page name "data_entry.php"
// extract of the data form..there are more fields in reality
$form ="<BR><center><table width = \"600\" <BR> <form name =\"form\" method ='get' action='results.php'>";
$form .="<BR>Enter your search criteria in the fields below.";
$form.="<p>Surname: ";
$form.="<br><input type=\"text\" name=\"surname\" size=\"65\" maxlength=\"50\"";
$form.="value=\"$surname\">&&&&&&&";
$form.="<br>Firstname: ";
$form.="<br><input type=\"text\" name=\"firstname\" size=\"65\" maxlength=\"50\"";
$form.="value=\"$firstname\">";
$form.="<br>Email Address: ";
$form.="<br><input type=\"text\" name=\"email\" size=\"65\" maxlength=\"70\"";
$form.="value=\"$email\">";
$form.="<br><input type=\"submit\" value=\"Search\" >";
$form.="<br></td></tr></table>";
$form.="</form>";
echo($form);
};?>
<script language="JavaScript">
document.form.surname.focus();
</script>


Hopefully, that might help :?
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby buspass » Sun Mar 09, 2003 2:16 pm

The search criteria is passed via GET to results.php correctly.

The first 20 results are displayed, but this error occurs, and the prev/next links do not appear.

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp\www\results.php on line 27

Is it worth changing the check_star () function which is building up the query so the vvariables are the same as in the results.php page?

(btw i am checking for the presence of a '*' at the end of the text field to indicate if the user want a wild cardf search as well as building up the query)
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am

Postby Xerpher » Sun Mar 09, 2003 2:31 pm

To be honest, I think a portion of your code might be in need of a complete re-write, but I might be wrong, I need to out for the rest of the day so hopefully somebody can help you in the mean time. I'l try to help you later tonight / tomorow if you still need it.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby buspass » Sun Mar 09, 2003 2:34 pm

Thanks for your help

I reckon you could be right about the query.

I need to revisit the way I construct it

Thanks again
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am

Postby buspass » Mon Mar 10, 2003 8:14 am

i have coded an actual query (ie "select * from table where surname = 'smith' and email= 'a@b.com'") and thte results page displayes correctly.

The problem MUST lie in the way i pass the variables from the data_entry page to the results page.

Has anyone any ideas of the best way to do this. It must be a common issue?
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am

Postby buspass » Mon Mar 10, 2003 11:40 am

i have rewritten the results page and have managed to get something close to what i am after. Its still not ideal as i want more error checking included, but that will come in time.

If anyone is interested I will post my revised code.

Otherwise thanks xerpher for your help. I'll email you separately.
buspass
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Mar 09, 2003 2:20 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.