2014-03-12 80 views
0

我有以下看法列出所有產品,並可以刪除或編輯特定的產品,的onclick的刪除或編輯,它會調用控制器,笨:從多個模型到視圖加載數據

<?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」,用戶將被重定向編輯的onclick用於特定項目如下,

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

現在問題是在控制器我需要加載類別(我已評論),以顯示他們在編輯視圖中,但是當我un-comment它,類別加載,但給出錯誤項目說明textarea的話說

Message: Undefined property: stdClass::$itemdesc 

隨着分類模型加載線評論道,它工作得很好,除了沒有類別將在下拉列表加載,請有人建議我的方式來擺脫這一點。

回答

1

請在您的視圖使用此

如果(isset($按行> itemdesc))回聲$按行> itemdesc;

我認爲這將解決您的問題

+0

錯誤沒有顯示出來,而且還有數據,通過註釋這些行來正確加載項目描述,但是當未評論,它加載的類別很好,但沒有物品描述 –

+1

將您的可變名稱$ row更改爲其他某些東西 像$ categroy您已經使用$ row作爲其他數據,這將覆蓋其他變量 foreach($ categories as $ row) –

+0

它工作正常!非常感謝Sohail,這是錯誤的。 –

0

我看到你有一個答案,但我對你的代碼的一些建議:您加載模型3次在你的控制器
1:你應該加載它一旦;你可以做這樣的事情:

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