Pedram
Pedram

Reputation: 16575

opencart get all downloadable files

I trying to get and display all downloadable files in product page. here is my code:

model:

public function download_m($product_id) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
    if ($query->num_rows) {
      return array(
        'product_id'       => $query->row['product_id'],        
        'download_id'      => $query->row['download_id']
      );
    } 
}

controller:

$download_m = $this->model_catalog_product->download_m($product_id);

view:

print_r($download_m)

database:

enter image description here

as you see there are two downloadable items with product_id 95, but it just return 3 not 3,4. what did i wrong?

Upvotes: 0

Views: 117

Answers (2)

try this code to get result as you want

$return_result=array();
if ($query->num_rows) {
  foreach ($query->rows as $result) {
    $return_result[]=array(
      'product_id'       => $result['product_id'],        
      'download_id'      => $result['download_id']
    );
 }
} 
return $return_result;

Upvotes: 3

Nimish
Nimish

Reputation: 1016

Use loop for each results like this.

if ($query->num_rows) {
    foreach ($query->rows as $result) {
        return array(
          'product_id'       => $result['product_id'],        
          'download_id'      => $result['download_id']
        );
     }
} 

You can check this type of code everywhere in Opencart files.

Upvotes: 0

Related Questions