sonnieboy Posted September 10, 2016 Share Posted September 10, 2016 Greetings mates I have this code below: $tsql = "Select COUNT(*) As totalRecords FROM bids b inner join DeptALIAS da on b.AliasID = da.AliasID inner join Dept d on da.DeptCode =d.DeptCode inner join status s on b.BidStatus=s.StatusId " . ( count($where) > 0 ? " WHERE " . implode(' AND ', $where) : " " ); //echo $tsql; I would like to add a WHERE clause where b.aliasID <> 22 Does anyone know how I can modify code above to add this please? Thanks a lot in advance Quote Link to comment https://forums.phpfreaks.com/topic/302129-how-do-i-modify-this-code/ Share on other sites More sharing options...
requinix Posted September 10, 2016 Share Posted September 10, 2016 Either modify $where to include your condition, or write it straight into the query. Whichever makes more sense in the code. Quote Link to comment https://forums.phpfreaks.com/topic/302129-how-do-i-modify-this-code/#findComment-1537269 Share on other sites More sharing options...
sonnieboy Posted September 10, 2016 Author Share Posted September 10, 2016 $fields = array( 'projectTitle' => array('field' => 'b.BidTitle', 'searchType' => 'like'), 'bidType' => array('field' => 'b.BidType', 'searchType' => 'equal'), 'bidStatus' => array('field' => 'b.BidStatus', 'searchType' => 'equal'), 'department' => array('field' => 'b.AliasID', 'searchType' => 'equal'), 'bidId' => array('field' => 'b.BidID', 'searchType' => 'like'), 'txtFromDate' => array('field' => 'b.BidDate', 'searchType' => 'gte'), 'txtToDate' => array('field' => 'b.BidDate', 'searchType' => 'lte'), 'txtFromDueDate' => array('field' => 'b.DueDate', 'searchType' => 'gte'), 'txtToDueDate' => array('field' => 'b.DueDate', 'searchType' => 'lte'), 'bidDate' => array('field' => 'b.BidDate', 'searchType' => 'equal'), 'dueDate' => array('field' => 'b.DueDate', 'searchType' => 'equal'), 'ddlCategory' => array('field' => 'b.CategoryID', 'searchType' => 'equal') ); $where = array(); $searchType = ""; foreach($fields as $fieldPost => $field) { if(isset($_GET[$fieldPost]) && strlen($_GET[$fieldPost]) > 0) { if($field['searchType'] == 'like') { $where[] = "".$field['field']." LIKE '%" . ms_escape_string($_GET[$fieldPost]) . "%'"; } elseif ($field['searchType'] == 'gte') { $where[] = "".$field['field']." >= '" . ms_escape_string($_GET[$fieldPost]) . "'"; } elseif ($field['searchType'] == 'lte') { $where[] = "".$field['field']." <= '" . ms_escape_string($_GET[$fieldPost]) . "'"; } else { $where[] = "".$field['field']." = '" . ms_escape_string($_GET[$fieldPost]) . "'"; } $searchType .= (empty($searchType) ? "" : "&") . $fieldPost . "=" . $_GET[$fieldPost]; // echo $searchType; } } Well, that's why I came here because I am not sure how to make the change. The original intent of the code is to display everything but now they want everything displayed except where b.bstatusid is not equal to 7. Here is the code I didn't post the first time where the Where is generated: Quote Link to comment https://forums.phpfreaks.com/topic/302129-how-do-i-modify-this-code/#findComment-1537271 Share on other sites More sharing options...
requinix Posted September 10, 2016 Share Posted September 10, 2016 I don't know your application. You're going to have to make some decisions on your own. This "status ID" thing isn't in the list of searchable terms so it sounds like you should put that condition straight into the query itself. Are you not familiar enough with SQL to know how to do that? Quote Link to comment https://forums.phpfreaks.com/topic/302129-how-do-i-modify-this-code/#findComment-1537273 Share on other sites More sharing options...
sonnieboy Posted September 10, 2016 Author Share Posted September 10, 2016 inner join status s on b.BidStatus=s.StatusId " . ( count($where) > 0 ? " WHERE " . implode(' AND ', $where) : " " ); First of all, the issue here really has nothing to do with knowing my application. Second, the one field I would like to modify is call bidStatus Third, I am pretty good with sql and writing queries but this is a problem for me because of the php component. The code as you can see is written to dynamically pass parameter based on what parameter is selected by the user. So modifying the code to add WHERE when there is already dynamic where is where I am having problem. Quote Link to comment https://forums.phpfreaks.com/topic/302129-how-do-i-modify-this-code/#findComment-1537279 Share on other sites More sharing options...
Jacques1 Posted September 10, 2016 Share Posted September 10, 2016 You start off with a static WHERE clause, and then you simply append your dynamic conditions: 'SELECT ... WHERE b.aliasID <> 22'.($where ? ' AND '.implode(' AND ', $where) : '') You should also consider using a query builder library or at least writing your own routines, because doing all those SQL gymnastics in the search script makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/302129-how-do-i-modify-this-code/#findComment-1537283 Share on other sites More sharing options...
sonnieboy Posted September 10, 2016 Author Share Posted September 10, 2016 That's it!!! Thank you so very much Jacques. Quote Link to comment https://forums.phpfreaks.com/topic/302129-how-do-i-modify-this-code/#findComment-1537284 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.