0

我有一个表格,要求用户输入有关他或她的猫的某些信息,然后用户可以提交他的信息并将其传输到数据库。我希望将此信息回显到视图中,因此它将显示已在表格中提交的每只猫。我认为我正确地使用了 foreach,但表格中没有填充我想要的内容。

这是我的观点的代码:

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

这是我的控制器代码:

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

我很感激任何帮助。

4

2 回答 2

1

您的单元格没有被回显到新行中。

代替

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

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

确切的语言取决于您的数据数组是什么。

于 2013-06-19T17:10:22.553 回答
0

啊,我想通了。这是它的样子:

<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 ($cats as $cat):?>
  <?php echo ($cat['name']);?><br/>
<?php endforeach;?></td>
<td><?php foreach ($cats as $cat):?>
  <?php echo ($cat['gender']);?><br/>
<?php endforeach;?></td>
<td><?php foreach ($cats as $cat):?>
  <?php echo ($cat['age']);?><br/>
<?php endforeach;?></td>
<td><?php foreach ($cats as $cat):?>
  <?php echo ($cat['species']);?><br/>
<?php endforeach;?></td>
<td><?php foreach ($cats as $cat):?>
  <?php echo ($cat['eye_color']);?><br/>
<?php endforeach;?></td>
<td><?php foreach ($cats as $cat):?>
  <?php echo ($cat['color']);?><br/>
<?php endforeach;?></td>
<td><?php foreach ($cats as $cat):?>
  <?php echo ($cat['description']);?><br/>
<?php endforeach;?></td>
<td><?php foreach ($cats as $cat):?>
  <?php echo ($cat['marital_status']);?><br/>
<?php endforeach;?></td>
</table> 
于 2013-06-19T17:33:37.837 回答