2016-11-18 27 views
1

SQL視圖文件-retrieve數據這是通過控制器笨 - 從分批

array (size=2) 
    'wholesale_records' => 
array (size=2) 
    0 => 
    object(stdClass)[36] 
     public 'id' => string '117' (length=3) 
     public 'product_id' => string '60' (length=2) 
     public 'usertype' => string 'wholesale' (length=9) 
     public 'range' => string '1' (length=1) 
     public 'uom' => string '3' (length=1) 
     public 'price' => string '1' (length=1) 
     public '[email protected]' => string '1' (length=1) 
     1 => 
    object(stdClass)[37] 
     public 'id' => string '119' (length=3) 
     public 'product_id' => string '60' (length=2) 
     public 'usertype' => string 'wholesale' (length=9) 
     public 'range' => string '3' (length=1) 
     public 'uom' => string '3' (length=1) 
     public 'price' => string '3' (length=1) 
     public '[email protected]' => string '3' (length=1) 
    'wholesale_count' => int 2 

我從模型文件的反應,但我想在我輸入佔位符,以顯示這些,但我在顯示 這是得到錯誤我的查看文件

<?php 
for ($i = 0; $i < $wholesale['wholesale_count']; $i ++) { ?> 
    <div class="section row" id="row1" style="margin-bottom:0px;"> 
     <div class="col-sm-3"> 
      <div class="form-group"> 
       <label class="field"> 
        <input type="text" name="range1" id="amount" 
          class="gui-input" placeholder="<?php echo $wholesale['wholesale_records'][ $i ]['range']; ?>" 
          required> 
       </label> 
      </div> 
     </div> 
    </div> 
<?php } ?> 

回答

2

您不顯示控制器或模型或您正在收到的確切錯誤。所有提供幫助都會非常有用。我要猜測這個問題。這可能是這條線。

placeholder="<?php echo $wholesale['wholesale_records'][ $i ]['range']; ?>" 

,所以你需要使用對象符號來訪問成員

placeholder="<?php echo $wholesale['wholesale_records'][ $i]->range; ?>" 

這簡直是一場困難一大堆比它需要你的模型返回對象的數組。

假設$wholesale是您從模型中顯示的回覆,您可能需要在視圖中考慮這一點。

<?php 
$records = $wholesale['wholesale_records']; 
foreach($records as $record){ ?> 
    <div class="section row" id="row1" style="margin-bottom:0px;"> 
    <div class="col-sm-3"> 
     <div class="form-group"> 
     <label class="field"> 
      <input type="text" name="range1" id="amount" 
       class="gui-input" placeholder="<?php echo $record->range; ?>" required> 
     </label> 
     </div> 
    </div> 
    <?php } ?> 

使用foreach(...)通常更容易設置比for(...)循環了很多。它不僅更少打字,而且執行速度更快。

看起來模型的回報可能更復雜一點。 該模型可以簡單地用

return $query->result(); 

完成它應該會返回一個結構類似這樣的

array (size=2) 
    0 => 
    object(stdClass)[36] 
     public 'id' => string '117' (length=3) 
     public 'product_id' => string '60' (length=2) 
     public 'usertype' => string 'wholesale' (length=9) 
     public 'range' => string '1' (length=1) 
     public 'uom' => string '3' (length=1) 
     public 'price' => string '1' (length=1) 
     public '[email protected]' => string '1' (length=1) 
     1 => 
    object(stdClass)[37] 
     public 'id' => string '119' (length=3) 
     public 'product_id' => string '60' (length=2) 
     public 'usertype' => string 'wholesale' (length=9) 
     public 'range' => string '3' (length=1) 
     public 'uom' => string '3' (length=1) 
     public 'price' => string '3' (length=1) 
     public '[email protected]' => string '3' (length=1) 

你不需要埋回另一層深,你不需要「wholesale_count」。 「foreach」電話會計算你的數量。

該控制器可隨後將模型返回查看,像這樣

$data['wholesale'] = $this->your_model->get_wholesale_records(); 
$this->load->view('your_view', $data); 

然後在視圖中的第一個兩行減少到

<?php 
foreach($wholesale as $record){ ?> 
    //the rest as shown previously 
+0

謝謝你這麼多.. –