Jump to content

[SOLVED] Need help wrapping a query in an if/else statement


pmjm1

Recommended Posts

Hi,

I have a form which queries a database and returns the result.

The problem is that in the script are a couple of exit functions which stop the rest of the page loading up when the form is empty.

How do I wrap the whole thing in an if/else statement to stop this happening?  Or am I barking up the wrong tree entirely?

thanks

<form name="form" action="memberstest.php" method="get">

  <input type="text" name="q" />

  <input type="submit" name="Submit" value="Search" />

</form>

 

<?php

 

  // Get the search variable from URL

 

  $var = @$_GET['q'] ;

  $trimmed = trim($var); //trim whitespace from the stored variable

 

// rows to return

$limit=10;

 

// check for an empty string and display a message.

if ($trimmed == "")

  {

  echo "<p>Please enter a search...</p>";

  exit;

  }

 

// check for a search parameter

if (!isset($var))

  {

  echo "<p>We dont seem to have a search term</p>";

  exit;

  }

 

//connect to database

mysql_connect("localhost","fvsrg_fgdata","smill"); //(host, username, password)

 

//specify database

mysql_select_db("fvsrg_fgdata") or die("Unable to select database"); //select which database we're using

 

// Build SQL Query 

$query = "select * from fgdata where rego like \"%$trimmed%\" 

  order by rego"; //

 

$numresults=mysql_query($query);

$numrows=mysql_num_rows($numresults);

 

 

// has s has been passed to script, if not use 0

  if (empty($s)) {

  $s=0;

  }

 

// get results

  $query .= " limit $s,$limit";

  $result = mysql_query($query) or die("Couldn't execute query");

 

// display what the person searched for

echo "<p>You searched for: "" . $var . ""</p>";

 

// begin to show results set

echo "Results";

$count = 1 + $s ;

 

// display the results returned

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

  $title = $row["fg_number"];

 

  echo "$count.) $title" ;

  $count++ ;

  }

 

$currPage = (($s/$limit) + 1);

 

//break before paging

  echo "<br />";

 

  // links to other results

  if ($s>=1) { // bypass PREV link if s is 0

  $prevs=($s-$limit);

  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<

  Prev 10</a>&nbsp ";

  }

 

// calculate number of pages needing links

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

 

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

 

  if ($numrows%$limit) {

  // has remainder so add one page

  $pages++;

  }

 

// check to see if last page

  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

 

  // not last page so give NEXT link

  $news=$s+$limit;

 

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";

  }

 

$a = $s + ($limit) ;

  if ($a > $numrows) { $a = $numrows ; }

  $b = $s + 1 ;

  echo "<p>Showing results $b to $a of $numrows</p>";

 

?>

Hi pmjm1,

 

Give this a go, I've set the script to return errors in a variable, if errors occur they will be echo'd out, otherwise the script will process as it should.

 

Hope this helps.

 

<form name="form" action="memberstest.php" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>

<?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  $error .= "<p>Please enter a search...</p>";
  }

// check for a search parameter
if (!isset($var))
  {
  $error .= "<p>We dont seem to have a search term</p>";
  }

if(!$error)
{
//connect to database
mysql_connect("localhost","fvsrg_fgdata","smill"); //(host, username, password)

//specify database 
mysql_select_db("fvsrg_fgdata") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from fgdata where rego like \"%$trimmed%\"  
  order by rego"; //

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);


// has s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

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

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["fg_number"];

  echo "$count.) $title" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

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

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

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

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

  // not last page so give NEXT link
  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
}
else
{
echo $error;	
}  
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.