如何使用codeigniter在视图中使用foreach

时间:2014-08-29 07:09:14

标签: php mysql codeigniter

我对codeigniter的观点有疑问。

我想保留标题。

示例:标题包含视图的主标题,如产品编号,产品种类和产品数量。以下是所有可用产品的列表。

我尝试编码,但会重复顶部标题,即产品编号,产品种类和产品数量。

结果应该是这样的。

Product number: 0011
Product Kind: Beverages
Product Quantity: 1000
--------------------------
Fit n Right
Coke
Pepsi
Lemon Juice
Gatorade
etc...

something like that. But my output code was: 

Product number: 0011
Product Kind: Beverages
Product Quantity: 1000
--------------------------
Fit n Right

Product number: 0011
Product Kind: Beverages
Product Quantity: 1000
--------------------------
Coke

Product number: 0011
Product Kind: Beverages
Product Quantity: 1000
--------------------------
Pepsi

Product number: 0011
Product Kind: Beverages
Product Quantity: 1000
--------------------------
Lemon Juice

依旧......

我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
       <title>Beverages</title>
</head>
<body>
<?php

 $funds=strtoupper($fundagent);

   if ($eid_num >0){
       foreach($eid_num as $row):
            $eid=$row->eid;
            $eseries=$row->becno;
            $amount=$row->amount;
            $rescenter=$row->res_center;

 ?>
 <div id="entrystyle" align="center">
table class="box-table-a" valign="top">
   <thead>
     <tr>
         <th colspan="7">Control System</th>
     </tr>
     </thead>
 <?php 
     echo form_open('/beverage/update-data-entry/'.$eid.'/'.strtolower($eseries)); 
 ?>   
 <tr class="gb">
     <td id="fontstyle">
           <label>Budget Earmarking Control No.</label>
           <input type="text" name="becno" id="controlno" size="15" required="required" value="<?php echo $eseries; ?>" onblur="copy_control_no()" readonly="readonly" />
    </td>
    <td id="fontstyle">
        <label>Amount</label>
        <input type="text" name="amount" id="amount" size="15" required="required" value="<?php echo $amount; ?>" readonly="readonly">
   </td>`

这部分我想重复或循环

    `<td id="fontstyle">
        <label>Responsibility Center</label>
        <input type="text" name="responsible" id="responsible" size="15" required="required" value="<?php echo $rescenter; ?>" readonly="readonly">
    </td>
 </tr>
 </table>

<?php endforeach; ?>

</table>
</div>

</body> 
</html>

2 个答案:

答案 0 :(得分:0)

试试这个

function build_view($flake) {
        $this->data['content'] = $this->load->view($flake, $this->data, TRUE);
        $this->load->view('header', $this->data);
    }   

答案 1 :(得分:-1)

控制器:

public function func(){
    .......
    $data['result'] = $this->model->myfunction();  // return resultset from DB
    $this->load->view('folder/filename', $data);
    ........
}

查看:

<?php 
foreach($result as $value) 
{
     echo $value.'<br/>';
}

?>

我想你会从中得到一些想法。

相关问题