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'];
    } 
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题