简体   繁体   English

Codeigniter:将数据库行返回为数组

[英]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>
...

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. 通过这样做,您现在不再需要使用视图中的所有[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM