CodeIgniter Forums

Full Version: Get Images of an Article
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
->db->join('gallery''gallery.imageproductid=productid' );
        return $myresult;

My Controller:

PHP Code:
public function products($link)
        $result $this->mydb->singleproduct($link);


My View:
<img src="<?php echo base_url($information->imagepath);?>" class="max"/>
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.
(09-16-2018, 09: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.


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.
The problem is in this line:
PHP Code:

The row() method will only return the first row.
If you want all rows, use $this->db->get()->result().
Dear @php_rocs, I followed your advice and ran the query first in my phpMyAdmin. It helped me to get the idea. However, I couldn't solve my problem at first.

Then I went on watching some other videos instead writing here for a further help. And I did! For example, the video (or even the series) which helped me a lot on YouTube was this Relational Table Joins  as it shows the JOIN method together with controller and view part in a clean way. (Maybe it might help others, that's why I'm sharing.)

I will have another question at the end, after sharing my working JOIN here:

MY MODEL (which has a seperate function following @jreklund's advice)

PHP Code:
function images($productslug){

$this->db->join('products''products.productslug = gallery.imageproductslug');
$bringimages $this->db->get();
               return $bringimages->result();


PHP Code:
public function products($pcategoryslug$productslug)
$data['category']= $this->vt->category($pcategoryslug);
$data['info'] = $this->vt->sinlgeproduct($productslug);
$data['photo'] = $this->vt->images($productslug);


PHP Code:
<?php foreach($photo as $p) { ?>
      <a rel="example_group" href="<?php echo base_url($p->imagepath); ?>" alt="OUR PRODUCTS">
<img itemprop="image" alt="OUR PRODUCTS" src="<?php echo base_url($p->imagepath); ?>" class="max"/></a>

<?php ?>

What I have learnt and what is my question now:

As you can see, I have learnt how to use controller. Collecting each part on $data['...'] or using the methods which effect the URL.. Even the order of it!! This video helped me a lot about this URL issue : URL Routing  .

And my question: As you can see, I did not join my tables using 'id' which is a classical because I got problem with the url. I had to use id within my url like ../product/product-ABC/4 .  4 is the id. Since I couldn't remove it from the url I did all the JOIN methods and rest working on the "slug", which I thought is unique as well. Not unique in DB way, but I have never seen any company having 2 products with the same name Smile There must always be a difference in the name like product-ABC and product-ABC3 .

Is it a rational way? What would you recommend and if you recommend in ID way again, how can I reshape my URL ?

Thank you all for all the answers and
Kind regards,
If you use IDs or slugs that match product, you don't actually have to join it with product table, because you already got product details with singleproduct() method.

Depending how much data you have in product table, with your original query, for every image you also get all the product details.

You could do something like this and only get image data:
PHP Code:
$bringimages $this->db->select('*')