【问题标题】:Not able to get show coma separated values in codeigniter view无法在 codeigniter 视图中显示逗号分隔值
【发布时间】:2016-07-12 22:18:17
【问题描述】:

我有这个数组我没有得到 area_tutor 因为它有逗号的值

Array
(
    [0] => stdClass Object
        (
            [tuitor_id] => 288
            [mobile_tuitor] => 0123456789
            [alt_mobile_tuitor] => 
            [fullname_tuitor] => 
            [gender_tuitor] => male
            [city_tutor] => 2
            [area_tutor] => 7,8,9
        )

    [1] => stdClass Object
        (
            [tuitor_id] => 287
            [mobile_tuitor] => 2568798456
            [alt_mobile_tuitor] => 
            [fullname_tuitor] => sumit test new filed
            [gender_tuitor] => male
            [city_tutor] => 1
            [area_tutor] => 3
        )

    [2] => stdClass Object
        (
            [tuitor_id] => 285
            [mobile_tuitor] => 1452587658
            [alt_mobile_tuitor] => 0789578469
            [fullname_tuitor] => Sumit Nair
            [gender_tuitor] => male
            [city_tutor] => 1
            [area_tutor] => 1,2,3,4
        )

    [3] => stdClass Object
        (
            [tuitor_id] => 273
            [mobile_tuitor] => 0954652478
            [alt_mobile_tuitor] => 
            [fullname_tuitor] => vbvbbb
            [gender_tuitor] => female
            [city_tutor] => 1
            [area_tutor] => 3
        )

    [4] => stdClass Object
        (
            [tuitor_id] => 225
            [profile_title] => 
            [mobile_tuitor] => 4557821212
            [alt_mobile_tuitor] => 5445587112
            [fullname_tuitor] => xyz
            [gender_tuitor] => male
            [city_tutor] => 4
            [area_tutor] => 14
        )

)

其中 area_tutor 的值带有逗号,我在从 foeach 循环中检索时没有得到这些值,我只得到 area_tutor 的第一个元素,这就是我使用 foreach 循环进行循环的方式

$this->db->select('*')->from('tuitor');
$this->db->order_by("tuitor_id", "desc");
$this->db->join('city_tuitor', 'tuitor.city_tutor = city_tuitor.city_id', 'left');
$this->db->join('area_tuitor', 'tuitor.area_tutor = area_tuitor.area_id', 'left');

$query=$this->db->get();
return $result = $query->result();

现在看来,我只是在 foreach 循环中循环

如果我像这样打印,我会得到上面的 aray print_r($结果);

但是当我当时试图回显值时,我只得到 area_tutor 的第一个值

 <?php  foreach($result as $r) { ?>
    echo $r->tuitor_id; 
    echo $r->mobile_tuitor;
    echo $r->city_tutor;
    echo $r->area_tutor;
    <?php } ?>

请帮助我如何获得 area_tutor 的所有值

【问题讨论】:

    标签: php mysql codeigniter foreach multipleselection


    【解决方案1】:

    尝试打印值的简单方法..

    <?php 
    $result = $query->result_array();
    foreach($result as $key=>$val)
    {
        echo $val['tuitor_id'];
        echo $val['mobile_tuitor'];
        echo $val['city_tutor'];
        echo $val['area_tutor'];
    }
    ?>
    

    【讨论】:

    • 消息:不能使用 stdClass 类型的对象作为数组 .... 致命错误:不能使用 stdClass 类型的对象作为数组 in
    • 我需要更改模型中的任何内容吗?
    • 无需更改模型中的任何内容,只需在 foreach 循环上方添加 $result= json_decode($result,true); 这一行
    • json_decode() 期望参数 1 是字符串,对象鉴于此我现在得到了对不起,我给你带来了麻烦,但我仍然可以得到:(
    • 将此行$result = $query-&gt;result(); 替换为$result = $query-&gt;result_array(); 并删除此行$result= json_decode($result,true);
    【解决方案2】:

    尝试使用where_in()

    $this->db->where_in();
    
    Generates a WHERE field IN ('item', 'item') SQL query joined with AND if appropriate
    
    $area_tutor= array('1', '2', '3');
    $this->db->where_in('area_tutor', $area_tutor);
    // Produces: WHERE area_tutor IN ('1', '2', '3')
    

    visit here for more information

    【讨论】:

    • yaar 它不工作你能详细说明我该怎么办吗?
    • 当我在做 print_r($result);我得到了区域,但是当我去 echo $r->area_tutor;它只显示一个值
    • 我得到了相同的输出,你可以在整个数组上方看到
    • 这个数组我只通过 print_r 得到的
    • bhai 请帮助 kar de :( pata he nae chal paara kaise values lu 多维数组 ka use karna hoga ky ?
    猜你喜欢
    • 2020-05-31
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    相关资源
    最近更新 更多