dsrw29618 2016-04-21 21:17
浏览 49
已采纳

如何在视图中使用它的Key访问数组值

I've been working on a method to email the users data to themselves and I'm having trouble printing data in the view. I can access all the data in bulk but I would like to format it so it displays within a HTML table for the user.

Model:

    function getEmailData($usersid){
    $this->db->select('*');
    $this->db->from('symptom');
    $this->db->where('userid', $usersid);
    $symptomState = $this->db->get();
    $result = $symptomState->result_array();

    return $result;
}

function getEmailBMData($usersid){
    $this->db->select('*');
    $this->db->from('bowelmovement');
    $this->db->where('userid', $usersid);
    $bmState = $this->db->get();
    $result = $bmState->result_array();

    return $result;
}

Controller:

    function sendDataAsEmail(){
    $uid = $this->session->userdata('id');
    $uname = $this->session->userdata('username');
    $uemail = $this->input->post('email_data');

    $data = array(
         'userName'=> $uname,
        );

    $data['symptomData'] = $this->poomonitormodel->getEmailData($uid);
    $data['bmData'] = $this->poomonitormodel->getEmailBMData($uid);
    $data['symptomCount'] = $this->poomonitormodel->getTotalSymptomCount($uid);
    $data['bmCount'] = $this->poomonitormodel->getTotalBMCount($uid);

    var_dump($data);

    $contents = $this->load->view('pooemail.php',$data,TRUE);

    $this->email
        ->from('#', '#')
        ->to($uemail)
        ->subject('#')
        ->message($contents)
        ->set_newline("
")
        ->set_mailtype('html');

    $this->email->send();
}

View:

<p>
    <?php foreach($symptomData as $data){
                        foreach($data as $nestdata){
                            echo $nestdata;
                        };
                    };?>
                </p>
                <p>
                <?php foreach($bmData as $bmdata){
                        foreach($bmdata as $nestbmdata){
                            echo $nestbmdata;
                        };
                 };?>
</p>

Currently echo $nestdata outputs the data as a complete string like this, but I would like to access a single attribute like $nestdata['symptomdate']:

462016-04-1402:00burningOesophagus7vomitting 562016-04-1405:00tinglingGallBladder3vomitting 662016-04-1410:00shootingSmallIntenstine8vomitting 1362016-04-2016:47crampGallBladder1Gurgling Noise 1462016-04-2016:58tinglingRectum1Strange tingling sensation in the raer 1962016-04-2017:41crampIleum2Cramping 2062016-04-2017:42crampAnus7It whistles 2162016-04-2017:42crampRectum7It also whistles

But I would like to access a specific value so that I can put each bit of data into a table data cell so it's easier to read for the user.

Thanks!

  • 写回答

1条回答 默认 最新

  • doudao1369 2016-04-21 21:51
    关注

    The keys in $data will be the name of the var in the view. So, $data['symptomCount'] in the controller will be accessed as $symptomCount in the view.

    What you do with it depends on its type. If $symptomCount is an object (class) then $x = $symptomCount->some_property. If it is an array - $symptomCount['some_index']

    Iterate them like so

    foreach($symptomData as $symptom){
       echo $symptom['symptomdate'];
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)
  • ¥15 三向应力状态求剪应力
  • ¥15 jupyter notebook如何添加libGL.so.1库
  • ¥20 easyPoi能否实现下拉多选或者复选框
  • ¥15 网桥在转发帧时,会变帧的源地址和目的地址吗?
  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码