Foreach looping |
![]() ![]() |
Foreach looping |
Dec 8 2008, 06:06 AM
Post
#1
|
|
|
Advanced Member ![]() ![]() ![]() Group: Members Posts: 8,266 Joined: 23-November 08 Member No.: 6,521 |
Hi,
Ps.. I'm repeating this again becos I was told my facts were not enough for a better understanding of the issue I am facing.... therefore... Issue: 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: $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: 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"; |
|
|
|
![]() ![]() ![]() |
|
Lo-Fi Version | Time is now: 18th March 2010 - 01:36 PM |