Page 1 of 1

Need Help with the Logic

Posted: Wed Jun 26, 2002 6:57 pm
by virgil
Hi PHP's (Peep's? MyPHP's YO,YO,YO Wud up LOL)

Boy I could use some advice. I'm getting lost in the logic. There must be an easier way. I'm puting together an event list site. I have the database up and running. Events posting to the DB fine. My logic trouble is in the Search/query pages. The Search page consists of four mutiple select(dropdown) menus. The filter selections are (year[], month[], event type[], and event size[] (which are columns in the DB) with the word "ALL" as a selection in each menu. Follow?

I have gotten as far as getting a correct result back for one column. The query uses three cases. "ALL" , "SOME" , or "ONE of the selections.
If I start to think of nesting "IF" statements for every combination of 4 sets of "ALL" , "SOME" , or "ONE, I just get the 8O look on my face.

I know the SWITCH statement might help, but it dosen't seem like it would much. There must be another way of approching this.

Can you do this with IF?:

Code: Select all

$count_month=count($date_filter);
$count_type=count($event_type_filter);
$count_size=count($size_filter);

if ($count_month == 1 and $count_date == 1 and $count_size == 1)
        {
        reset($date_filter);
            reset($event_type_filter);
               reset($size_filter);
        $date_filter=pos($date_filter);
            $event_type_filter=pos($event_type_filter);
               $size_filter=pos($size_filter);
        }
$ALL="ALL";
echo "$date_filter "." $event_type_filter "." $size_filter "."
It dosen't cause an error but the IF( AND ) seems to ignored..thought it was worth a try.




Anyway... This is my approch,
but I dont think it can work with multiple multiples.

Code: Select all

//This returns events in all months
//date_filter is month filter

$count=count($date_filter);

if ($count==1)
	{
	reset($date_filter);
	$date_filter=pos($date_filter);
	}

$ALL="ALL";
if ($date_filter == "$ALL")
{
	$query = "SELECT eventnum, date_year, date_month, date_day,  event_size, event_type, FROM db"; 

  



//This returns multiple months (i.e JAN FEB MAR only)

if ($date_filter!=$ALL)
	
	{
	$count = count($date_filter);
 	}

if ( $count > 1 )
 
{
	$date_filter = implode( "','", $date_filter);

$query = "SELECT eventnum, date_year, date_month, date_day,  event_size, event_type,

FROM eventpost WHERE date_month IN ('$date_filter')";
   

    
//This returns single (not ALL) selections only
else
{
$query = "SELECT eventnum, date_year, date_month, date_day,  event_size, event_type,
	
 FROM eventpost WHERE date_year='$year_filter' AND date_month='$date_filter' AND event_size='$size_filter' AND event_type='$event_type_filter'";


I would very much appreicate any advice and would love maybe some sample code from another query page that deals with multiples.

Thanks PHP's ........Virgil

Posted: Wed Jun 26, 2002 7:07 pm
by jason
Okay, let me run an example with 2 options, and of course, this idea can be expanded.

Code: Select all

<?php

$sql = "SELECT * FROM table WHERE ";

if ( $_POST&#1111;'searchByDate'] ) &#123;
	$sql .= "date = '".$_POST&#1111;'date']."'";
&#125;

if ( $_POST&#1111;'searchByLength'] ) &#123;
	$sql .= " AND length = '".$_POST&#1111;'length']."'";
&#125;

$result = mysql_query($sql);

?>
Of course, this is under the assumption that you allow the person two choices for searching by, but it can be expanded easily enough.

Another option if you have a lot of possibilities, is to name the form elements the same thing as the field name and do something like this:

Code: Select all

<?php

$sql = "SELECT * FROM table WHERE 1";

foreach ( $_POST as $fieldName => $fieldSearchString ) &#123;
	if ( !empty($fieldSearchString) ) &#123;
		$sql .= " AND $fieldName = '$fieldSearchString'";
	&#125;
&#125;

$result = mysql_query($sql);

?>
Both of these should work (as in, I haven't tested them, but the logic is there. :D