-3

I want to get product thumbnails on a product category page, each product 1st thumbnail image needs to be shown. right now the last image is showing. here is the screen shot for my db structure

EDIT:

right now the table format as screenshot, there are 4 images with order 1,2,3,4 i want to get the image with img_order 1.

enter image description here

when i am running sql query

 SELECT i.img_order,p.*, i.product_id, i.dirName, i.img FROM products p inner JOIN product_images i ON i.product_id = p.id WHERE p.status = 1 and i.product_id=329 AND i.type = 'Static' GROUP BY i.product_id ORDER BY i.img_order asc

it's giving wrong result see screenshot

enter image description here

but i want to get the image with img_order 1.

Umer bin Siddique
  • 460
  • 1
  • 4
  • 13

3 Answers3

1

Try Something like this:

SELECT pi.img_order,p.*, pi.product_id, pi.dirName, pi.img 
FROM product as p
LEFT JOIN product_image as pi ON pi.product_id = p.id
JOIN
 ( SELECT id,product_id, MIN(img_order) minVal
   FROM product_image GROUP BY product_id
 ) i
ON pi.img_order = i.minVal AND i.id = pi.id;

fiddle is here

Mobasher Fasihy
  • 1,021
  • 2
  • 9
  • 17
0

I found the solution with the following sql query:

SELECT i.img_order,p.*, i.product_id, i.dirName, i.img FROM products p inner JOIN product_images i ON i.product_id = p.id WHERE p.status = 1 and i.product_id=329 AND i.type = 'Static' GROUP BY i.product_id ORDER BY i.img_order asc
miken32
  • 42,008
  • 16
  • 111
  • 154
Umer bin Siddique
  • 460
  • 1
  • 4
  • 13
-1

Have you tried something like this.

Check all the rows

 $query = $this->db->query("YOUR QUERY");

 foreach ($query->result_array() as $row)
 {
    echo $row['title'];
    echo $row['name'];
    echo $row['body'];
  }
Waqas_aamer
  • 220
  • 1
  • 3
  • 18