pmjm1 Posted September 24, 2009 Share Posted September 24, 2009 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 Link to comment https://forums.phpfreaks.com/topic/175349-solved-need-help-wrapping-a-query-in-an-ifelse-statement/ Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 Hi pmjm1, Please post your code so we can take a look and give a more definite answer. Thanks Link to comment https://forums.phpfreaks.com/topic/175349-solved-need-help-wrapping-a-query-in-an-ifelse-statement/#findComment-924059 Share on other sites More sharing options...
pmjm1 Posted September 24, 2009 Author Share Posted September 24, 2009 <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>  "; } // 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/175349-solved-need-help-wrapping-a-query-in-an-ifelse-statement/#findComment-924060 Share on other sites More sharing options...
pmjm1 Posted September 24, 2009 Author Share Posted September 24, 2009 problem is after this form I have the include for the footer etc, and the exits are stopping them from loading thanks for your help, I'm v new to this Link to comment https://forums.phpfreaks.com/topic/175349-solved-need-help-wrapping-a-query-in-an-ifelse-statement/#findComment-924061 Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 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>  "; } // 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; } ?> Link to comment https://forums.phpfreaks.com/topic/175349-solved-need-help-wrapping-a-query-in-an-ifelse-statement/#findComment-924067 Share on other sites More sharing options...
pmjm1 Posted September 24, 2009 Author Share Posted September 24, 2009 Thanks very much, that's worked perfectly. I'll go through and try to learn from that. Much appreciated Link to comment https://forums.phpfreaks.com/topic/175349-solved-need-help-wrapping-a-query-in-an-ifelse-statement/#findComment-924074 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.