Foreach looping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
birrd
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 10:47 am

Foreach looping

Post by birrd »

Hi,

The issue is like this....

For my advanced search function (to search by text - option ['title/director'] AND/OR text - [release date] AND/OR checkboxes - [ratings 1 to 5]), I must do a foreach in order to achieve results according to the selected option, inputs and/or selected checkboxes.

However, I was having problems in retrieving the checkboxes - [ratings] result.. thus I did the foreach loop for the checkboxes separately. Thankfully it works but now I'm not sure how I can put the codes into one.. =( ... Instead of two foreach which will produce results that are separated from one another, I want to have just one foreach which will produce results that will be restricted by the text option AND/OR checkboxes. (Altogether)


This is the first foreach for the text option [title/director] AND/OR text - [release date]

Code: Select all

$sql = "SELECT * FROM movie WHERE publish=1 AND ";
 
$counter = 0; //checks whether an sql statement is a following or a first
 
foreach($_POST as $key => $value){
 
  if($key == "search"){
    if(empty($value)){
    $keyword = "";
    }else{
    $keyword = $value;
    }
  }else if($key == "searchby"){
    if(empty($keyword)){
    }else{
    $sql .= "$value LIKE '%$keyword%'";
    $counter = $counter + 1;
    }
  }else if($key == "Release_date"){
    if(empty($value)){
    }else{
    if($counter > 0){
    $sql .= "AND (Release_date >='$value')";
    $counter = $counter + 1;
    }else{
    $sql .= "(Release_date >='$value')";
    $counter = $counter + 1;
    }
    }
  }else if($key == "Release_date2"){
    if(empty($value)){
    }else{
    if($counter > 0){
    $sql .= " AND (Release_date >='$date1') AND (Release_date <='$value') Order by Release_date ASC";
    $counter = $counter + 1;
    }else{
    $sql .= "(Release_date >='$date1') AND (Release_date <='$value') Order by Release_date ASC";
    }
This is the second foreach for ratings 1 -5 (checkboxes)

Code: Select all

if(!empty($_POST["rating"]))  {
 
    $c=count($_POST["rating"])-1;
    $q=0;
    foreach($_POST["rating"] AS $keys)
    {
        $in_id.="$keys";
        if($q==$c)
        break;
        else
        $in_id.=",";
    
        $q++;
    }
    
    $sql="SELECT * FROM movie WHERE rating IN ($in_id) ORDER BY rating ASC";
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Foreach looping

Post by requinix »

What's wrong with your other thread? Didn't like the title?
birrd
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 10:47 am

Re: Foreach looping

Post by birrd »

Oh nonono.. you mentioned the previous thread lacked info? So i thought to repost another with more details for a better understanding of the issue at hand. =) ... mm does that mean the solution which u provided previously will work if I want results as a whole (meaning: if i input into the text field and/or select the checkboxes) and not from separate entities?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Foreach looping

Post by requinix »

Typically, if someone asks for more information you make another post, not another thread ;)

There are two different types of search terms: text and checkbox. Just start building the WHERE clause using the text boxes, then add on to it according to the checkboxes. Thing is, both of those are really similar.
Umm... something like this:

Code: Select all

// text:
// producer, director, title
// checkbox:
// iscomedy, isdrama, isstupid
 
$conditions = array(); // list of different search conditions
 
// text
if (!empty($_POST["search_producer"]))
    $conditions[] = "`producer` LIKE '" . mysql_real_escape_string($_POST["search_producer"]) . "'";
if (!empty($_POST["search_director"]))
    $conditions[] = "`director` LIKE '" . mysql_real_escape_string($_POST["search_director"]) . "'";
if (!empty($_POST["search_title"]))
    $conditions[] = "`title` LIKE '" . mysql_real_escape_string($_POST["search_title"]) . "'";
 
// checkbox
if (isset($_POST["search_iscomedy"])) $conditions[] = "`iscomedy`";
if (isset($_POST["search_isdrama"])) $conditions[] = "`isdrama`";
if (isset($_POST["search_isstupid"])) $conditions[] = "`isstupid`";
 
if (!$conditions) {
    // no search terms
} else {
    $conditions = implode(" AND ", $conditions); // combine into one string
    $conditions = str_replace(array("*", "?"), array("%", "_"), $conditions); // fix wildcards
 
    $query = "SELECT fields FROM table WHERE $conditions";
    // ...
}
birrd
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 10:47 am

Re: Foreach looping

Post by birrd »

:lol: oops...

Okay, I tried but....

Now, the query only recognizes the release_date inputs; neglecting the value in the first text field with a dropdown list option as well as the checkboxes that are selected... :?:

Thing is... my advanced search function has 3 text fields and [5] checkboxes for ratings 1 - 5; One of the text field is accompanied with a dropdown list option (searchby: title or director or description); The other two text fields are for input of dates, hence it will be blank ( yyyy/mm/dd to yyyy/mm/dd).

Code: Select all

 
    if (!empty($_POST["searchby_title"]))
           $conditions[] = "`title` LIKE '" . mysql_real_escape_string($_POST["searchby_title"]) . "'";
       
    if (!empty($_POST["searchby_Director"]))
           $conditions[] = "`Director` LIKE '" . mysql_real_escape_string($_POST["searchby_Director"]) . "'";
 
    if (!empty($_POST["searchby_Description"]))
           $conditions[] = "`Description` LIKE '" . mysql_real_escape_string($_POST["searchby_Description"]) . "'";
           
    if (!empty($_POST["Release_date"]))
           $conditions[] = "`Release_date` >= '" . mysql_real_escape_string($_POST["Release_date"]) . "'";
           
    if (!empty($_POST["Release_date2"]))
           $conditions[] = "`Release_date` <= '" . mysql_real_escape_string($_POST["Release_date2"]) . "'";
    
// checkbox
    if (isset($_POST["rating"])) $conditions[] = "`rating`";
    if (isset($_POST["rating"])) $conditions[] = "`rating`";
    if (isset($_POST["rating"])) $conditions[] = "`rating`";
    if (isset($_POST["rating"])) $conditions[] = "`rating`";
    if (isset($_POST["rating"])) $conditions[] = "`rating`";
 
Post Reply