Welcome Guest, Not a member yet? Register   Sign In
Check if a value from array exists in database not working
#3

(10-28-2015, 11:08 AM)Taylor Wrote:
PHP Code:
   //use foreach to use $this->$db->like with every sub_team that the user is assigned to
   foreach ($sub_teams as $sub_team) {
       $this->db->like('sub_teams'$sub_team);
       echo $sub_team;
   

This code works but only works in an order. For instance, if the user is assigned "Programming,Construction" then he will see all assigned that are shared with either Programming or Construction. This is good so far. The problem happens when the values of sub_teams in the user's table is not in order. It won't show me announcements that are either Programming or Construction if the order is "Construction,Programming".

Please help guys. Thanks.

When you build the LIKE as above it will be joined together with AND, for example:

Code:
SELECT * FROM `announcements`
WHERE `active` = 1
 AND `date_created` < '2015-10-28 20:10:28'
 AND `sub_teams` LIKE '%Construction%'
 AND `sub_teams` LIKE '%Programming%'
JOIN ...;

So only when the announcements table has rows with sub_teams containing both Construction and Programming will be returned.
You could give following example a try:
   
PHP Code:
    if (!empty($sub_teams)) {
            $this->db->group_start();
            for ($i 0$i count($sub_teams); ++$i) {
                if ($i 1) {
                    $this->db->like('sub_teams'$sub_teams[$i]);
                } else {
                    $this->db->or_like('sub_teams'$sub_teams[$i]);
                }
            }
            $this->db->group_end();
        

Hope I can help you.
Reply


Messages In This Thread
RE: Check if a value from array exists in database not working - by pdthinh - 10-28-2015, 01:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB