note 84463 added to control-structures.foreach

From: Date: Tue, 15 Jul 2008 13:37:25 +0000
Subject: note 84463 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-141534@lists.php.net to get a copy of this message
Hi, I try to use foreach to print out a part of an mysql query, but always the last one cancels the previous one out. Please advise me on what to do: $searchterm=$_POST['searchterm']; if ($type) { foreach($_POST['type'] as $type) { $search_type = "AND type = '$type' "; } } else { $search_type = " "; } if ($year) { foreach($_POST['year'] as $year) { $search_year = "AND DATE_FORMAT(dateofissue,'%Y') = '$year' "; } } else { $search_year = " "; } if ($theme) { foreach($_POST['theme'] as $theme) { $search_theme = "AND keywords LIKE '%*$theme*%' "; } } else { $search_theme = " "; } if ($geography) { foreach($_POST['geography'] as $geography) { $search_geography = "AND geography = '$geography' "; } } else { $search_geography = " "; } $search_query = "SELECT * FROM library WHERE (title_en LIKE '%$searchterm%' OR fulltext_en LIKE '%$searchterm%') $search_type $search_year $search_theme $search_geography ORDER BY dateofissue DESC, type DESC, title_en DESC"; The original values come from a form which one fills in. So geography[] is parsed to this and it can have one or more values. For example, if geography[]=africa and geography[]=asia, the query will only show "AND geography=asia" as the latter comes later. However the thing would work if I put the variable in the URL, like thisfile.php?geography[]=africa&geography[]=asia Can someone help me? ---- Server IP: 195.207.10.20 Probable Submitter: 195.72.82.137 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/84463 Del: integrated -- https://master.php.net/note/delete/84463/integrated Del: useless -- https://master.php.net/note/delete/84463/useless Del: bad code -- https://master.php.net/note/delete/84463/bad+code Del: spam -- https://master.php.net/note/delete/84463/spam Del: non-english -- https://master.php.net/note/delete/84463/non-english Del: in docs -- https://master.php.net/note/delete/84463/in+docs Del: other reasons-- https://master.php.net/note/delete/84463 Reject -- https://master.php.net/note/reject/84463 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141534) next »