dsfsd43523
2016-06-24 22:24
浏览 36
已采纳

Codeigniter:传递给视图的解析数组

i've to parse an array and print its reslut in a table , this is how i am passing array to view in
controller

public function history(){
            $history_result= array();
            $history_result = $this->user_model->user_history($this->user_id,0);
            $this->result_set['data'] = $history_result;
            $this->load->view('test',  $this->result_set);  
         }

in view if i var_dump $data i get array (size=1)

  0 => 
    object(stdClass)[19]
      public 'id' => string '1' (length=1)
      public 'user_id' => string '1' (length=1)
      public 'sender_name' => string 'test' (length=14)
      public 'sender_mobile' => string '12323' (length=10)
      public 'receiver_name' => string 'sfsf' (length=4)
      public 'sender_location' => string 'sfsf' (length=4)
      public 'receiver_location' => string 'sfsfs' (length=5)
      public 'receiver_mobile' => string '0' (length=1)
      public 'is_urget' => string '0' (length=1)
      public 'is_assigned' => string '0' (length=1)
      public 'request_type' => string '0' (length=1)
      public 'attachment_id' => string '' (length=0)
      public 'status' => string '0' (length=1)

my question is how do i access each item out of this result so that may print in a html table like this

<tr>id : 1</tr>
<tr>sender name : name..</tr>

图片转代码服务由CSDN问答提供 功能建议

我要解析一个数组并在表中打印它的结果, 这就是我将数组传递给 查看

控制器

 公共功能历史记录(){
 $ history_result = array(); 
 $ history_result = $  this-&gt; user_model-&gt; user_history($ this-&gt; user_id,0); 
 $ this-&gt; result_set ['data'] = $ history_result; 
 $ this-&gt; load-&gt; view(  'test',$ this-&gt; result_set);    
 
 

view 中如果我var_dump $ data我得到 array(size = 1) \ n

  0 =&gt;  
 object(stdClass)[19] 
 public'id'=&gt; 字符串'1'(长度= 1)
 public'user_id'=&gt; 字符串'1'(长度= 1)
公共'sender_name'=&gt; 字符串'test'(长度= 14)
公共'sender_mobile'=&gt; 字符串'12323'(长度= 10)
 public'adminage_name'=&gt; 字符串'sfsf'(长度= 4)
公共'sender_location'=&gt;  string'sfsf'(length = 4)
 public'reader_location'=&gt;  string'sfsfs'(length = 5)
 public'reiver_mobile'=&gt; 字符串'0'(长度= 1)
 public'is_urget'=&gt; 字符串'0'(长度= 1)
 public'is_assigned'=&gt; 字符串'0'(长度= 1)
 public'request_type'=&gt; 字符串'0'(长度= 1)
公共'attachment_id'=&gt;  string''(length = 0)
 public'status'=&gt; 字符串'0'(长度= 1)
   
 
 

我的问题是我如何访问此结果中的每个项目,以便可以在这样的html表中打印< / p>

 &lt; tr&gt; id:1&lt; / tr&gt; 
&lt; tr&gt;发件人姓名:名称..&lt; / tr&gt; 
    
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqkx69935 2016-06-24 22:39
    已采纳

    If you have this in your model

    return = $this->db->get()->row();//one result
    

    In your view you can display like

    $data->id . '<br>' . $data->user_id . '<br' . $data->sender_name ...
    

    if in your model get a multiple rows like

    return = $this->db->get()->result();//as many as you have
    

    In your view

    foreach($data as $item){
    echo '<tr>';
    echo '<td>'.$item->id.'</td>';
    echo '<td>'.$item->user_id.'</td>';
    echo '<td>'.$item->sender_name.'</td>';
    ...
    echo '</tr>';
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题