Codeigniter - 查看文件 - 批量从sql中获取数据

时间:2016-11-18 12:56:34

标签: mysql codeigniter

这是我通过控制器模型文件的回复

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 'vat@' => 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 'vat@' => 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 } ?>

1 个答案:

答案 0 :(得分: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 'vat@' => 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 'vat@' => string '3' (length=1)

你不需要埋葬另一层深层的回报,而且你不需要&#39; wholesale_count&#39;。 “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