简体   繁体   中英

Codeigniter : Loading data from multiple Models to a view

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.

Please use this in your view

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

I think this will solve your problem

I saw that you got an answer but i have a few suggestions for your code:
1. in your controller you load the model 3 times: you should load it once; you could do something like this:

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


2. in your views you load a helper: this should be loaded into the controller, right after the first verification from point 1.
3. in views you should check your variable for content, especially the ones that you are going to use in a loop, like $products

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM