Jump to content

How do I modify this code?


sonnieboy

Recommended Posts

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

Link to comment
Share on other sites

$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:

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.