1

So I'm trying to display the data in my view but I can only access it like:

$someArray[index]['value'];

I want to access it like

$someArray['value']

I need to do this so I can pass the array values to a cart insert.

Model:

public function getProductById($id) {
    $this->db->select('*');
    $this->db->from('products');
    $this->db->where('id', $id);
    $this->db->limit(1);
    $query = $this->db-> get();
    $returnArray = $query->result_array();
    return $returnArray;
  }

Controller

public function viewProduct($id){
    $product['productData'] = $this->Products_model->getProductById($id);
    $this->load->view('template/header');
    $this->load->view('products/view_product', $product);
    $this->load->view('template/footer');
  }

View Snippet:

<h4 style="margin-top: 10px;">Product Description:</h4>
      <p><?php echo $productData[0]['full_description']; ?></p>
    </div>
    <div class="col-lg-6">
      <h1><?php echo $productData[0]['product_name']; ?></h1>
      <h3>£ <?php echo $productData[0]['product_price']; ?></h3>
      <br>
      <h5>Main Features:</h5>
      <ul>
        <li><?php echo $productData[0]['feature_1']; ?></li>
        <li><?php echo $productData[0]['feature_2']; ?></li>
        <li><?php echo $productData[0]['feature_3']; ?></li>
      </ul>
...
1
  • if you are retrieving only 1 row should use row_array() and if you are retrieving more than 1 then should use result_array(). And then use foreach loop to handle your data Commented Feb 12, 2018 at 6:18

1 Answer 1

4

Change this line:

$returnArray = $query->result_array();

To this line:

$returnArray = $query->row_array();

By doing that you now longer need to use all those [0]s in your view.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.