problems with empty search fiels and row count

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
nade93
Forum Newbie
Posts: 1
Joined: Wed Sep 09, 2009 4:13 pm

problems with empty search fiels and row count

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: problems with empty search fiels and row count

Post 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
Post Reply