2013-06-19 14 views
0

我有一個表單要求用戶輸入有關他或她的貓的某些信息,然後用戶可以提交他的信息並將其傳送到數據庫。我希望將這些信息反映到視圖中,以便顯示已在表格中提交的每個貓。我認爲我正確地使用了foreach,但表沒有被填充我想要的東西。如何使用ForEaches回顯陣列陣列

這是我爲我的視圖代碼:

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

我感謝所有幫助。

+0

什麼是在數據數組? HTML或數組的貓? –

+0

5只貓的陣列。在視圖中,所有顯示的是「數組」字樣 以下是輸出圖片:http://tinypic.com/r/33yjddu/5 –

回答

1

您的單元格未被回顯到新行中。

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

確切的語言取決於您的數據陣列的數據進行替換

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

+0

感謝您的回覆,但我收到一個錯誤聲明那'name'是未定義的。 –

+0

不客氣。因此,我的答案的最後一行。我不知道你的$ data數組是什麼樣的。如果你想包含一個var_dump的數據數組,我可以爲你調整它。 –

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

你確定你有權利@Ralph。我仍然可以看到你錯過了''標籤,你只是將數據全部回顯到每個屬性的一個單元格中。 –