• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Images of an Article

#1
Hello there,

I'm new to Codeigniter and to this forum as well.

I'm trying to publish my articles (products). On any product page there will be a gallery showing the related images about the product. I've tried the JOIN method below upon a tutorial, however I cannot fetch all the photos. How can I get all the images and show them like a photo gallery?

My Gallery table has a "imageproductid" column on which I make the join for "product"

My DB :


PHP Code:
function singleproduct($a)
    {
        $this->db->select('*');
        $this->db->from('products');
        $this->db->where('productslug'$a);
    
        $this
->db->join('gallery''gallery.imageproductid=productid' );
        $myresult=$this->db->get()->row();
        return $myresult;
    



My Controller:


PHP Code:
public function products($link)
    {
        $this->load->model('mydb');
        $result $this->mydb->singleproduct($link);
        $data['information']=$result;

        $this->load->view('product'$data); 


My View:
 
Code:
<img src="<?php echo base_url($information->imagepath);?>" class="max"/>
Reply

#2
You never specified the table "products" in your join.
PHP Code:
$this->db->join('gallery''gallery.imageproductid=products.productid' ); 

But if you have multiple images I would make a separate SQL query instead. Or you need to ignore the duplicated data.
Reply

#3
(09-16-2018, 10:46 AM)jreklund Wrote: You never specified the table "products" in your join.
PHP Code:
$this->db->join('gallery''gallery.imageproductid=products.productid' ); 

But if you have multiple images I would make a separate SQL query instead. Or you need to ignore the duplicated data.

Thanks for your answer jreklund, but I guess pointing to my "products" in my JOIN is not the issue as I might have typed my codes here wrong Smile

My problem is, imagine that, 7 of my images on my database have 'gallery.imageproductid' = 4. It means, my product whose ID is 4 will show 7 images.
But with my codes, I can only fetch 1 result. I cannot fetch all the images for this join (article and images match).

Plus, I tried but couldn't figure out how to make a seperate SQL. How can I match the products with articles.. How can I join them on model-controller ?

As you can see, up to now, I have tried it ona a slug (link) based way.

Regards
Reply

#4
@demyr,

I recommend that you try creating the query and running it in phpMyAdmin/or some other MySQL tool. This will give you a much better idea if your query will work the way you need it to work. For me to assist it would be great if you could provide table field names plus test data. This appears to be a problem that can be soloved.
Reply

#5
The problem is in this line:
PHP Code:
$myresult=$this->db->get()->row(); 

The row() method will only return the first row.
If you want all rows, use $this->db->get()->result().
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.