douqiang1851 2013-06-19 17:05
浏览 49
已采纳

如何使用ForEaches回显数组数组

I have a form that asks the user to enter certain information about his or her cat, then the user may submit his information and it will be transferred to the database. I want this information to be echoed out into the view, so it will show each cat that has been submitted in a table. I think I am using the foreach correctly, but the table is not being populated with what I want.

Here is my code for my view:

<form action="/Anish/create" method="POST">
  <div>
    <label>Name</label><input type="text" name="name">
  </div>
  <div>
    <label>Age</label><input type="text" name="age">
  </div>
  <div>
    <label>Gender</label><input type="text" name="gender">
  </div>
  <div>
    <label>Species</label><input type="text" name="species">
  </div>
  <div>
    <label>Eye Color</label><input type="text" name="eye_color">
  </div>
    <div>
    <label>Color</label><input type="text" name="color">
  </div>
    <div>
    <label>Description</label><input type="text" name="description">
  </div>
    <div>
    <label>marital status</label><input type="text" name="marital_status">
  </div>
  <br>
  <button type="submit" class="btn btn-block btn-primary span1">Add cat</button>
</form>
<br/>
<br/>
<br/><br/>
<table border="1">
<tr>  
<td>Name</td>
<td>Gender</td>
<td>Age</td>
<td>Species</td>
<td>Eye Color</td>
<td>Color</td>
<td>Description</td>
<td>Marital Status</td>
</tr>
<td>
<?php 
  foreach ($data as $cats){
    echo ($cats);
  }
?>
</td>
</table>

And here is my code for the controller:

class Anish extends Public_Controller
{
  public function __construct()
  {
   parent::__construct();
    // Load the required classes
    $this->load->model('Anish_m');
    // $this->lang->load('anish');
  }
  public function index()
  {
$data=array( 
'cats'=>$this->Anish_m->get(),
  );
// var_dump($data);die;
    $this->template
      ->set('data', $data)
      ->build('index', $data);
  }

  public function create() {
    $Anish = array(
      'name' => $this->input->post('name'),
      'age' => $this->input->post('age'),
      'gender' => $this->input->post('gender'),
      'species' => $this->input->post('species'),
      'eye_color' => $this->input->post('eye_color'),
      'color' => $this->input->post('color'),
      'description' => $this->input->post('description'),
      'marital_status' => $this->input->post('marital_status')
    );
    $createCat=$this->Anish_m->create($Anish);
      redirect('/Anish');
  }
}

I appreciate any help.

  • 写回答

2条回答 默认 最新

  • douzinei6926 2013-06-19 17:10
    关注

    Your cells aren't being echoed into new rows.

    replace

    <td>
    <?php 
      foreach ($data as $cats){
        echo ($cats);
      }
    ?>
    </td>
    

    with

    <?php 
      foreach ($cats as $cat){
        echo '<tr>'
         . '<td>' . $cat['name'] . '</td>'
         // etc...
         . '</tr>';
      }
    ?>
    

    The exact language depends on what your data array is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟