1

This is my response from model file through controller

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

But i want to display these in my input placeholder but i am getting error in displaying this is my view file

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

2

You don't show the controller or the model or the exact error you are getting. All would be very useful in providing help. I'm going to make a guess at the problem. It's probably this line.

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

Your model is returning an array of objects so you need to use object notation to access the members

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

Which is frankly a whole lot harder than it needs to be.

Assuming that $wholesale is the response you show from the model you might want to consider this in your view.

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

Using foreach(...) is usually a lot easier to set up than a for(...) loop. It is not only less typing but it will execute faster.

It seems like model's return is maybe more complicated that it needs to be. The model could simply finish with

return $query->result();

Which should return a structure like this

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)

You don't need bury the return another layer deep and you don't need 'wholesale_count'. The 'foreach` call will figure the count for you.

The controller could then passes the model return to view like so

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

Then the first couple lines in the view are reduced to

<?php
foreach($wholesale as $record){ ?>
    //the rest as shown previously
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.