RIRedinPA Posted December 19, 2007 Share Posted December 19, 2007 Hi Folks I'm building a MySQL query string based on selections from a form... Here are the field names and values passed (via POST): search:::advanced logNum::: pubName:::AA photoName:::FirstName_LastName issueDate::: keywords::: linkage:::and usage:::null in the end I should get a string something like this "SELECT * FROM photorecord WHERE key = value AND otherkey = othervalue ORDER BY logNum DESC" instead I am getting this...I've bolded the problem... SELECT * FROM photorecord WHERE AND pubName LIKE '%AA%' AND photoName LIKE '%FirstName_LastName%' ORDER BY logNum DESC I've been staring at this too long and can't see where I am making the mistake. Can someone look at it with a pair of fresh eyes and see if they can pick it out. It seems that it is not registering the proper $keyCount - the first field/value pair should be written without "AND" at it's start. What is throwing it off is the field "search" which is not a field in the db, but a hidden form field passed to let me know what type of search the user is performing. I thought I trapped that out with this code: if ($key != "search") { $keyCount = $keyCount + 1; } but it doesn't seem to be working. Any help would be appreciated. ================== Code Follows ================== <?php $linkage = $_POST['linkage']; $query = "SELECT * FROM photorecord WHERE "; $keyCount = 0; foreach($_POST as $key => $value) { if ($key != "search") { $keyCount = $keyCount + 1; } //build logNum if ($key == "logNum") { if ($value != "") { if ($keyCount == 1) { $query .= "logNum = " . $value; } else { $query .= " AND logNum = " . $value; } } } else if ($key == "pubName") { if ($value != "" && $value != "novalue") { if ($keyCount == 1) { $query .= "pubName LIKE '%" . $value . "%'"; } else { $query .= " AND pubName LIKE '%" . $value . "%'"; } } } else if ($key == "photoName") { if ($value != "" && $value != "novalue") { if ($keyCount == 1) { $query .= "photoName LIKE '%" . $value . "%'"; } else { $query .= " AND photoName LIKE '%" . $value . "%'"; } } } else if ($key == "issueDate") { if ($value != "") { if ($keyCount == 1) { $query .= "issueDate LIKE '%" . $value . "%'"; } else { $query .= " AND issueDate LIKE '%" . $value . "%'"; } } } else if ($key == "restrictions") { if ($value != "") { if ($keyCount == 1) { $query .= "restrictions LIKE '%" . $value . "%'"; } else { $query .= " AND restrictions LIKE '%" . $value . "%'"; } } } else if ($key == "keywords") { if ($value != "") { //see if we're looking for an exact match if ($linkage == "exact") { if ($keyCount == 1) { $query .= "keywords LIKE '" . $value . "'"; } else { $query .= " AND keywords LIKE '" . $value . "'"; } } else { //get count of keywords $keyList = split($value, chr(32)); if ($keyCount == 1) { if (count($keyList) == 1) { $query .= "keywords LIKE '%" . $value . "%'"; } else { $query .= " AND keywords LIKE '%" . $value . "%'"; } } else { //loop through the keywords for ($i=0; $i<=count($keyList); $i++) { //first search item if ($keyCount == 1) { //which keyword (in list) if ($i < count($keyList)) { $query .= "keywords LIKE '%" . $value . "%' " . $linkage; } else { $query .= "keywords LIKE '%" . $value . "%' "; } } else { if ($i == 1) { $query .= " AND keywords LIKE '%" . $value . "%' " . $linkage; } else if ($i < count($keyList)) { $query .= "keywords LIKE '%" . $value . "%' " . $linkage; } else { $query .= "keywords LIKE '%" . $value . "%' "; } } } }//end loop through keywords }//end key count check }//end key check } }//end post loop $query .= " ORDER BY logNum DESC"; echo $query; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82381-solved-problem-with-_post-array-loop/ Share on other sites More sharing options...
papaface Posted December 19, 2007 Share Posted December 19, 2007 Change $keycount = 0 to $keycount = 1. See if that makes a difference. Quote Link to comment https://forums.phpfreaks.com/topic/82381-solved-problem-with-_post-array-loop/#findComment-418807 Share on other sites More sharing options...
RIRedinPA Posted December 20, 2007 Author Share Posted December 20, 2007 You got me partly there. I needed to only increase $keyCount when this condition is true - $value != "", and start the $keycount at 1. Thanks for the nudge. Quote Link to comment https://forums.phpfreaks.com/topic/82381-solved-problem-with-_post-array-loop/#findComment-419373 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.