dousunle5801 2014-03-12 05:41
浏览 41
已采纳

Codeigniter:将多个模型中的数据加载到视图中

I have the following view to List all products, and in that you can delete or edit a particular product, onclick of delete or edit it will call the controller,

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

So the controller to the above view look like belo

<?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" which the user will be redirected onclick of edit for an particular item is as follows,

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

Now the problem is in the controller i need to load up categories (which I have commented) in order to show them in the edit view, but when I un-commented it, categories are loading, but Gives an Error in Item Description textarea saying

Message:  Undefined property: stdClass::$itemdesc

With the category model loading line commented, it works fine except no categories will load up in the dropdownlist,Please someone suggest me a way to get rid of this.

  • 写回答

2条回答 默认 最新

  • dscuu86620 2014-03-12 05:49
    关注

    Please use this in your view

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

    I think this will solve your problem

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站