Page 1 of 1

problems with empty search fiels and row count

Posted: Wed Sep 09, 2009 4:17 pm
by nade93
Hi All

I am really stuck! any help would be really appreciated!

basically, trying to create a results page from a form and need two things that am really stuck on.

1. If the form value ="" then i need it to return all the results in that table field.
2. I need to then take those results and count certain values. For example, how many case_open fields have the "yes" value and then return that value as a number

Code for part 1.

Code: Select all

$ethnic = $_REQUEST['ethnic'];
$gender = $_REQUEST['gender'];
$referral = $_REQUEST['referral'];
$age = $_REQUEST['age'];
$month = $_REQUEST['month'];
$year = $_REQUEST['year'];
 
$query  = "SELECT * from tbl WHERE";
 
if ((!empty($_POST['ethnic']))&&($_POST['ethnic'] != 'all')) { $query .= " ethnic_background like '". addslashes($_POST['ethnic'])."%' "; }
 
if ((!empty($_POST['gender']))&&($_POST['gender'] != 'all')) { $query .= " and gender like '". addslashes($_POST['gender'])."%' "; }
 
 
if ((!empty($_POST['age']))&&($_POST['age'] != 'all')) { $query .= " and age_range like '". addslashes($_POST['age'])."%' "; }
 
if ((!empty($_POST['month']))&&($_POST['month'] != 'all')) { $query .= " and date_received_month like '". addslashes($_POST['year'])."%' "; }
 
if ((!empty($_POST['year']))&&($_POST['year'] != 'all')) { $query .= " and date_received_year like '". addslashes($_POST['year'])."%' "; }
 
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
 if ($numrows == 0)
  {
 
  echo "<div class=\"heading\">Sorry, your search returned no results. Please go back and try again!</div>";
 
  }
I then run a loop i.e.

Code: Select all

<? while ($row = mysql_fetch_assoc($result)) {
     
 ?>
Then i need to return the results below in a table i.e.

Code: Select all

<table><tr>
                <td valign="top" height="30" style="border-bottom:1px #ccc solid;" class="bodytext_table"><div align="left"> Number of IMHA cases opened</div></td>
        <td style="border-bottom:1px #ccc solid;"> <div align="right" style="color:#993366; font-size:12px; font-weight:600;">number here</div></td> </tr></table>
can someone shed some light please, its really urgent and need it tonight and have searched high and low for answers!!!

any help appreciated!

thanks

Re: problems with empty search fiels and row count

Posted: Sat Sep 12, 2009 3:41 pm
by Darhazer
Change line 8 to:

Code: Select all

$query  = "SELECT * from tbl WHERE 1=1 "; // this allows easy adding of where conditions
And line 10 to:

Code: Select all

if ((!empty($_POST['ethnic']))&&($_POST['ethnic'] != 'all')) { $query .= " ethnic_background like '". addslashes($_POST['ethnic'])."%' "; }
This should resolve problem #1