Jump to content

Foreach() .. Struggleing to get my head around this


Accurax

Recommended Posts

[code]
$name = $_POST['name'];
$pet = $_POST['pet'];
foreach($_POST as $k=>$v) if(!empty($_POST[$k]))$query.=" && `$k`='%$v%'";
if(empty($query)) die("You must enter some fields");

$user_query = "SELECT * FROM mytable WHERE name='$name' $query";
$result = mysql_query($user_query) or die ("no can do");
[/code]

The above code was given to me by Taith in a thread last week, thanks taith.

Basically guys i have a webform, that is posting values to a php script that should build a query to my database, Problem is that if a user leaves one of the fields on my form empty, the query doesnt work.

Taith kindly suggested using a foreach statement for this, ive dont a fair bit of reading, and i know understand how foreach works ... but im having some problems getting my head around how to apply it to this particular situation.

Can anyone help clear things up for me?
you should write a tiny piece of code BEFORE your input fields get thrown into a query, that will only add to the query if the field has input... i wrote something like this for my 5-field search tool...
[code]
<?php
      /*
        *if the "Search" button is clicked
        *check to see if there is anything in the input fields
        */
        if($action == "simplesearch"){
                if(empty($lname) && empty($fname) && empty($event_day) && empty($event_month) && empty($event_year)){
                        echo "<FONT COLOR=\"FF0000\">You must enter at least one input field. Please try again.</FONT>\n";
                        exit;
                }else{
      /*
        *checks to see which input fields had data
        *then depending on the input field
        *enter in the correct sql query for that input field
        *into a new index in the array $query_array
        */
                        $query_array = array();
                        if(!empty($lname)){
                                $query_array[] = "brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'";
                        }
                        if(!empty($fname)){
                                $query_array[] = "brideFname LIKE '%". $fname ."%' OR groomFname LIKE '%". $fname ."%'";
                        }
                        if(!empty($event_day)){
                                $query_array[] = "event_day LIKE '%". $event_day ."%'";
                        }
                        if(!empty($event_month)){
                                $query_array[] = "event_month LIKE '%". $event_month ."%'";
                        }
                        if(!empty($event_year)){
                                $query_array[] = "event_year LIKE '%". $event_year ."%'";
                        }

      /*
        *$query string becomes all the sql queries
        *with 'AND' in between them
        */
                        $query_string = implode(" AND ", $query_array);

                        #echo $query_string ."<br />\n"; /*check sql query*/

                        $result = mysql_query("SELECT * FROM my_search_table WHERE ". $query_string ."") OR die(mysql_error());
                }
        }
?>
[/code]
hope this helps. it should =)
Im still confused im afraid ..... i have 20 different fields in my search form, to do every combination of empty or not empty would require 1000's of if()elseif() statements.... which is why id like to use a foreach() loop.

Im getting lost when i try and understand how i actually go about building the query.
i wrote my 'if' statements the long way... you [i]could[/i] write a small function that would take care of all 20 of your search fields. maybe something to the effect of...
[code]
<?php
$fields_array = ($field1, $field2, $field3, $field4, $field5,
$field6, $field7, $field8, $field9, $field10,
$field11, $field12, $field13, $field14, $field15,
$field16, $field17, $field18, $field19, $field20);
$query_array = array();

foreach($fields_array as $key => $val){
if(!empty($val)){
$query_array[] = "your_column LIKE '%". $val ."%'";
}
}

$query_string = implode(" AND ", $query_array);
$result = mysql_query("SELECT * FROM your_search_table WHERE ". $query_string ."") OR die(mysql_error());
?>
[/code]
that's not too different from what i've already done.
my advice would be to do some testing. start by echoing out the query as you run them to see which combos work and which ones dont. at that pont you should be able to narrow down what the exact problem is. you might find out that the faulty queries have one too many amperands or soemthing

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.