CodeIgniter Forums
simple data passing query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: simple data passing query (/showthread.php?tid=49226)



simple data passing query - El Forum - 02-11-2012

[eluser]junaids[/eluser]
Hi,
I am trying to pass two things through the data variable. But I cant figure out how to use them in the view. Here is the code..
Code:
function show_products()
  {
    $this->load->model('Gallery_model');
    $data['products'] = $this->Gallery_model->show_products();
    $data['images'] = $this->Gallery_model->get_images();
    //$this->load->view('gallery', $data);
    $this->load->view('show_products', $data);
  }
and the view..
Code:
<div id="blank_gallery">Product Description</div>
    
<div id="gallery">
  &lt;?php foreach($images as $image): ?&gt;
   <div class="thumb">
    <a href="&lt;?php echo $image['url']; ?&gt;">
     <img src="&lt;?php echo $image['thumb_url']; ?&gt;" />
    </a>    
   </div>
              &lt;?php endforeach;?&gt;
            <div class="thumb">

            &lt;?php foreach($products as $product): ?&gt;
            &lt;?php echo 'Product Name' ?&gt;
            &lt;?php echo $product->name; ?&gt;
            &lt;?php echo 'Product Price' ?&gt;
            &lt;?php echo $product->price; ?&gt;
   &lt;?php echo $product->option_color; ?&gt;
   &lt;?php echo $product->option_size; ?&gt;
            &lt;?php endforeach; ?&gt;
            </div>
            
</div>

Now there are two foreach in the view.. I want to dispaly the product image and its description in one div(foreach). Suggestions please..


simple data passing query - El Forum - 02-12-2012

[eluser]Bhashkar Yadav[/eluser]
you can fetch all these info from one model function and can pass into view.


simple data passing query - El Forum - 02-12-2012

[eluser]junaids[/eluser]
The images are returned from the directory and the data is returned from db..so i dont think they can be returned in one query..
Code:
function show_products()
    {
  $this->load->database();

        $query = $this->db->get('products');
        return $query->result();
    }
function get_images() {
  
  $files = scandir($this->gallery_path);
  $files = array_diff($files, array('.', '..', 'thumbs'));
  
  $images = array();
  
  foreach ($files as $file) {
   $images []= array (
    'url' => $this->gallery_path_url . $file,
    'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
   );
  }
  return $images;
}
Can this be done in one query?
or I can store images in the db. Can someone show me how to display images returned from db?


simple data passing query - El Forum - 02-12-2012

[eluser]vitoco[/eluser]
I think that if you use product_id (int) as name for the images, you can put it as index in the images array and then call it inside the products foreach

Code:
// product_id = 100001 => image => 100001.png/gif/jpeg
In get_images() function =============================
Code:
foreach ($files as $file) {
   // GET product_id FROM $file ( only digits )
   $product_id = preg_replace("/[^0-9]/", "", $file );
   // MAKE THE INDEX EXPLICIT
   $images [ $product_id ]= array (
    'url' => $this->gallery_path_url . $file,
    'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
   );
}
In products foreach =============================
Code:
&lt;?
// ASSUMING THAT $product->product_id have the product_id
?&gt;
&lt;?php foreach($products as $product): ?&gt;
    &lt;?php echo 'Product Name' ?&gt; :
    &lt;?php echo $product->name; ?&gt;<br>
    &lt;?php echo 'Product Price' ?&gt; :
    &lt;?php echo $product->price; ?&gt;<br>
    COLOR :&lt;?php echo $product->option_color; ?&gt;<br>
    SIZE :&lt;?php echo $product->option_size; ?&gt;<br>
    IMG: <img src="&lt;?php echo $images[ $product-&gt;product_id ]['url'] ?&gt;"/><br>
    THUMB: <img src="&lt;?php echo $images[ $product-&gt;product_id ]['thumb_url'] ?&gt;"/><br>
&lt;?php endforeach; ?&gt;

Saludos


simple data passing query - El Forum - 02-12-2012

[eluser]junaids[/eluser]
thanx vitoco..
i have defined 'id' in the table. But I dont get how the images can be retrieved from the directory based on the id..I used ur code and get this error.
Code:
Product Name : camera
Product Price : 2000
COLOR :red
SIZE :large
IMG:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: 1

Filename: views/show_products.php

Line Number: 41
"/>
THUMB:



simple data passing query - El Forum - 02-12-2012

[eluser]vitoco[/eluser]
as i wrote, the code works only if the name of the image contains de id of the product, so how are you creating the name of the images?, please give an example


simple data passing query - El Forum - 02-12-2012

[eluser]junaids[/eluser]
@vitoco..need your advice


simple data passing query - El Forum - 02-12-2012

[eluser]vitoco[/eluser]
very simple...if the product id is "1" , the image must be "1.png" ( or any other image extension ), so the id and the name match and you can have a "reference" from one the other, and also use the id as index in every array of "objects" related to the product.


simple data passing query - El Forum - 02-12-2012

[eluser]junaids[/eluser]
thanx vitoco..it helped