【问题标题】:Codeigniter : Loading data from multiple Models to a viewCodeigniter:将数据从多个模型加载到视图
【发布时间】:2014-03-12 05:41:46
【问题描述】:

我有以下视图来列出所有产品,并且您可以删除或编辑特定产品,单击删除或编辑它将调用控制器,

<?php

$this->load->helper('url');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>diluks eCommerce - Home</title>

      <link href="<?php

echo base_url();

?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
   </head>
   <body>
    <form action="<?php echo base_url();?>index.php/productlist_controller" method="post">
      <div class="container">
        <?php

include 'header-adminpanel.php';

?>
         <div class="level3 clearfix">
            <?php

include 'product-sidebar.php';

?>
            <div class="body-content">


             <div class="items">
             <h2>Product List</h2>
             <table class="CSSTable" cellspacing="0px" cellpadding="0px">
                <tr>
                    <td>Item Code</td><td>Item Name</td><td>Item Price</td><td>Edit</td><td>Delete</td>
                </tr>
                <?php foreach($products as $row): ?>
                <form method="POST" action="<?php echo base_url();?>index.php/productlist_controller">
                <tr>
                    <td><?php echo $row->itemcode; ?></td><td><?php echo $row->itemname; ?></td><td>$<?php echo $row->itemprice; ?></td><td><center><button name="btn_edit" class="link-button" value="<?php echo $row->itemcode; ?>" type="submit">Edit</button></center></td><td><center><button name="btn_delete" class="link-button" value="<?php echo $row->itemcode; ?>" type="submit">Delete</button></center></td>
                </tr>
                </form>
                <?php endforeach ?> 
             </table>
              </div>
            </div>
         </div>
         <div style="clear:both"></div>
         <div class="level4">
            <div class="footer-area">
               <div class="lined-space"></div>
               <div class="site-map" align="left">
                  <table>
                     <tr>
                        <td class="footer-text"><a href="#">About Us</a></td>
                        <td class="footer-text"><a href="#">Facebook</a></td>
                     </tr>
                     <tr>
                        <td class="footer-text"><a href="#">Contact Us</a></td>
                        <td class="footer-text"><a href="#">Twitter</a></td>
                     </tr>
                     <tr>
                        <td class="footer-text"><a href="#">FAQs</a></td>
                        <td class="footer-text"><a href="#">Terms & Conditions</a></td>
                     </tr>
                     <tr>
                        <td class="footer-text"><a href="#">Help</a></td>
                     </tr>
                  </table>
               </div>
               <div class="developer-info">
                  <a class="developers-text">Designed & Developed By Diluks Software Solutions.</a>
               </div>
            </div>
         </div>
      </div>
      </form>
   </body>
</html>

所以上面视图的控制器看起来像贝洛

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Productlist_controller extends CI_Controller {

function __construct(){
        parent::__construct();


    }

    public function index()
    {

       if(isset($_POST["btn_delete"])){

         $pid = $_POST["btn_delete"];
         $this->load->model('product_model');
         $result = $this->product_model->deleteProduct($pid);
         if($result==true){

        $data = array();
        $this->load->model('product_model');
        $data['products'] = $this->product_model->availableProductList();
        $this->load->view('admin_product_list_view',$data);



         }
         else{

            echo "Oops! Error occured..!";
         }
       }
       else if(isset($_POST["btn_edit"])){

        $pid = $_POST["btn_edit"];
        $data = array();
        $this->load->model('product_model');
        $data['product'] = $this->product_model->readProduct($pid);
        //$this->load->model('category_model');
        //$data['categories'] = $this->category_model->getCategories();



        $this->load->view('admin_product_edit_view', $data);

       }

    }





}

“admin_product_edit_view”,用户在点击编辑特定项目时将被重定向如下,

<?php

$this->load->helper('url');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>diluks eCommerce - Home</title>
      <link href="<?php

echo base_url();

?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
   </head>
   <body>
    <form enctype="multipart/form-data" action="<?php

echo base_url();

?>index.php/addproduct_controller" method="post">
      <div class="container">
        <?php

include 'header-adminpanel.php';

?>
         <div class="level3 clearfix">
            <?php

include 'product-sidebar.php';

?>
            <div class="body-content">


             <div class="items">
             <h2>Edit Product</h2>
        <?php foreach($product as $row){ ?>         
        <table>
            <tr>
                <td class="captions">Product Code</td>
                <td><input name="txt_pcode" type="text" readonly="true" value="<?php echo $row->itemcode; ?>"/></td>
            </tr>
            <tr>
                <td class="captions">Product Name</td>
                <td><input name="txt_pname" type="text" size="40" value="<?php echo $row->itemname; ?>" /></td>
            </tr>
            <tr>
                <td class="captions">Product Price</td>
                <td><input name="txt_pprice" type="text" value="<?php echo $row->itemprice; ?>" /></td>
            </tr>
            <tr>
                <td class="captions">Product Category</td>
                <td><select name="txt_pcategory">

<?php 

            foreach($categories as $row)
            { 
              echo '<option value="'.$row->catname.'">'.$row->catname.'</option>';
            }
            ?>


                </select></td>
            </tr>
            <tr>
                <td class="captions">Product Description</td>
                <td><textarea name="txt_pdesc" style="width:300px;height:100px;"><?php echo $row->itemdesc; ?></textarea></td>
            </tr>
            <tr>
                <td class="captions">Product Image</td>
                <td><input type="file" name="userfile" size="20" /></td>
            </tr>
            <tr>
                <td class="captions">Product Options</td>
                <td><input name="txt_poptions" size="40" type="text" /><a class="hint"> (Separate by a "," comma)</a></td>
            </tr>
            <tr><td><input name="btn_add" class="grey-button" type="submit" value="Update" /></td></tr>
        </table>
        <?php } ?> 
        <br />
              </div>
            </div>
         </div>
         <div style="clear:both"></div>
         <div class="level4">
            <div class="footer-area">
               <div class="lined-space"></div>
               <div class="site-map" align="left">
                  <table>
                     <tr>
                        <td class="footer-text"><a href="#">About Us</a></td>
                        <td class="footer-text"><a href="#">Facebook</a></td>
                     </tr>
                     <tr>
                        <td class="footer-text"><a href="#">Contact Us</a></td>
                        <td class="footer-text"><a href="#">Twitter</a></td>
                     </tr>
                     <tr>
                        <td class="footer-text"><a href="#">FAQs</a></td>
                        <td class="footer-text"><a href="#">Terms & Conditions</a></td>
                     </tr>
                     <tr>
                        <td class="footer-text"><a href="#">Help</a></td>
                     </tr>
                  </table>
               </div>
               <div class="developer-info">
                  <a class="developers-text">Designed & Developed By Diluks Software Solutions.</a>
               </div>
            </div>
         </div>
      </div>
      </form>
   </body>
</html>

现在问题出在控制器中,我需要加载类别(我已评论)以便在编辑视图中显示它们,但是当我取消评论时,类别正在加载,但在项目中出现错误描述 textarea 说

Message:  Undefined property: stdClass::$itemdesc

注释了类别模型加载行后,它工作正常,除了下拉列表中不会加载任何类别,请有人建议我一种摆脱这种情况的方法。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    请在您的视图中使用它

    if(isset($row->itemdesc)) echo $row->itemdesc;

    我想这会解决你的问题

    【讨论】:

    • 将您的变量名称 $row 更改为其他名称,例如 $categroy 您已经将 $row 用于其他数据,这将覆盖其他变量 foreach($categories as $row)
    【解决方案2】:

    我看到你得到了答案,但我对你的代码有一些建议:
    1. 在您的控制器中,您加载模型 3 次:您应该加载一次;你可以这样做:

    if(!empty($_POST)){
       $this->load->model('product_model');
       if(isset($_POST["btn_delete"])){
           //some code
       }elseif(isset($_POST["btn_edit"])){
           //some other code
       }
    }
    


    2. 在您的视图中,您加载了一个助手:这应该在第 1 点的第一次验证之后立即加载到控制器中。
    3. 在视图中你应该检查你的变量的内容,尤其是那些你要在循环中使用的,比如$products

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多