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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退