2016-05-14 32 views
1

我有db结构只显示带有foreach限制的值的字段

id | ref_id |美化1 |舒适2 |舒适3 |舒适4 |舒适5

每个舒适性列的值为1或0. 当进行搜索时,我需要显示值不等于零(值!= 0)的字段。 我知道我可以考虑这样做,因为

if($data->amenity1 == 0) echo ''; 

但我需要用它来限制自动化。

我的模型代码是

function select_all_active_amenities($for_id){ 
    foreach($for_id as $id){ 
     $prop_id = $id->vbc_item_id; 
    } 
    $this->db->select('*'); 
    $this->db->from('vbc_property_amenities'); 
    $this->db->where_in(array('v_ref_id'=> $prop_id)); 
    $this->db->limit(5); 
    $query = $this->db->get(); 
    $result = $query->result(); 

    return $result; 
} 

请帮

+0

是有固定的只有6列市容或者动态地改变删除空列? – JYoThI

+0

@ jothi,其中约105人,但未来可能会增加.. – Sebastian

+0

为什么你不喜欢这一个如果($ amenity1 == 0)echo''; ?问题是什么 ? – JYoThI

回答

0

你只是使用自定义数组建立

function select_all_active_amenities($for_id){ 
foreach($for_id as $id){ 
    $prop_id = $id->vbc_item_id; 
} 
$this->db->select('*'); 
$this->db->distinct('v_ref_id'); 
$this->db->from('vbc_property_amenities'); 
$this->db->where_in(array('v_ref_id'=> $prop_id)); 
// $this->db->limit(5); 
$query = $this->db->get(); 
$result1 = $query->result_array(); 

//fetch the value as array 

//and this query for fetch the column name what you have right now an where not equal to id and ref_id 

$this->db->query("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='vbc_property_amenities' AND COLUMN_NAME !='id' AND COLUMN_NAME != 'ref_id AND COLUMN_NAME !='id' ")->result_array(); 
    $query = $this->db->get(); 
    $result2 = $query->result_array(); 

foreach($result as $key1=>$row1) 
{ 

     foreach($result2 as $key2=>$row2) 
     { 

      if($row2 ==$row1[$row2] && $row1 ==0) 
      { 

       unset($result[$key1][$row2]); 
      } 

     } 



} 

//$result only have the where value=1 

return $result; 

//you just limit the five in your view page 
} 
+0

谢谢...任何想法如何限制重复显示的设施..例如..如果hotel1有舒适1而hotel2也有舒适1,则舒适1显示两次。 – Sebastian

+0

你刚才给我看你的表格结构 – JYoThI

+0

上面的代码你明白我做了什么对你有用吗? – JYoThI

1

添加“DISTINCT”关键字查询选择之前,获得唯一的记录

$this->db->distinct(); 

希望它可以帮助你出来了!

+0

对不起。distinct对我而言不适用 – Sebastian

+0

bro,你也可以试试group_by @Sebastian – Kunal

+0

现在我在db中只有2个属性具有相同的设施。当我运行我的查询时,它会回显v_aircondition v_queenbed,v_kingbed, v_aircondition,v_queenbed,v_kingbed ...查看重复内容?我怎么可能做到这一点? – Sebastian

相关问题