如何在不使用循环的情况下根据数组元素从DB中获取数据

时间:2016-02-16 06:24:57

标签: php arrays codeigniter codeigniter-3

我在一个数组中有多个id,我想打印cat_name .. 这是我在CI中的代码......

$business_cat   = $this->input->post('cat_id');

$business_sub_cat= $this->input->post('sub_cat_id');

$msg="<td><b>Business Catagories :</b></td><td>".implode(", ", $business_cat)."</td>
                        </tr><tr>   
                            <td><b>Business Subcatagories :</b></td><td>".implode(", ", $business_sub_cat)."</td>";

但我想打印cat_name而不是id&#39; s,但是当我使用foreach循环时b / w

它打印错误。 谁能帮我吗。 Thanxx提前

1 个答案:

答案 0 :(得分:0)

试试这个...... //标记这只是一个演示代码

$business_cat   = $this->input->post('cat_id');
$cat_name = array();
foreach($business_cat  as $key=>$cat_id )
{
    $cat_name[] = "demo cat name ". $key;//take name from database using $cat_id.... 
}
$cat_name_str = implode(", ", $cat_name);
$business_sub_cat = $this->input->post('sub_cat_id');
$sub_cat = array();
foreach($business_sub_cat  as $key=>$sub_cat_id )
{
    $sub_cat[] = "demo sub cat name ". $key;//take name from database using $sub_cat_id.... 
}
$sub_name_str = implode(", ", $sub_cat);

$msg = "<td><b>Business Catagories :</b></td><td>$cat_name_str</td></tr><tr><td><b>Business Subcatagories :</b></td><td>$sub_name_str</td>";