Search code examples
phpmysqlopencartopencart2.xopencart2.3

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?


Solution

  • 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;