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

时间:2016-04-21 21:17:18

标签: php arrays codeigniter

我一直致力于将用户数据通过电子邮件发送给自己的方法,而且我在视图中打印数据时遇到问题。我可以批量访问所有数据,但我想格式化它,以便在用户的HTML表格中显示。

型号:

    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;
}

控制器:

    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("\r\n")
        ->set_mailtype('html');

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

查看:

<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>

目前echo $nestdata将数据输出为完整字符串,但我希望访问$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口哨2162016-04-2017:42crampRectum7它也吹口哨

但是我想访问一个特定的值,这样我就可以将每一位数据放入一个表格数据单元中,这样就可以更容易地为用户阅读。

谢谢!

1 个答案:

答案 0 :(得分:0)

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'];
}