简体   繁体   中英

I am trying to compare sport_id in user table but sport_id are store in array how can i compare with php

sports table:

id   name
1    Athletics
2    Aerobics

users table:

id  sports_type 
1     1,2        
2     1          

Model query:

function select_sports($sports) 
{
    $this->db->select('id');
    $this->db->like('name', $sports); 
    $query = $this->db->get('sports');
    //print_r($query);exit();
    return $query->row();
} 

function get_filterde_user() 
{ 
    $sql = "SELECT * FROM users"; 
    $query = $this->db->query($sql); 
    return $query->result();
}

Controller:

 $sports = $this->input->post('sports');
 $query = $this->user_model->select_sports($sports);
 $sport_id = $query->id; 
 $sport_get = explode(',',$sport_id);
 $query = $this->user_model->get_filterde_user(); 
 $arr = array(); 
 foreach ($query as $row=>$value) {
     if ($value->sports_type != 0 && $value->user_type != 1) { 
         $sports_type = explode(',', $value->sports_type);
         foreach ($sport_get as $search) { 
             if (in_array($search, $sports_type)) {
                 echo  $arr[$row]['name'] = $value->user_name; die; 
             }  
         } 
     } 
 }

but it's not give all field related sports_type

from your database structure it seems that it quit difficult but poosible, i recommend the database strucure as below:

  1. sports table:

    id sport_name

  2. users table:

    id user_name user_adress

  3. user_sports:

    id users_id(users table pk) sports_id(sport table pk)

and then try searching operation, it will be very easy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM