decprog
decprog

Reputation: 187

Codeigniter: Return database row as array

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

Upvotes: 1

Views: 1891

Answers (1)

Brian Gottier
Brian Gottier

Reputation: 4582

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.

Upvotes: 4

Related Questions