CodeIgniter Forums
Selecting category details and products linked with the same category. - 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: Selecting category details and products linked with the same category. (/showthread.php?tid=38295)

Pages: 1 2


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Bigil Michael[/eluser]
i want to list car category name and car details in a single page

but the result is car details are same for every category....
can anyone help me .....

thanks in advance


controller
Code:
function index()
    {
        
    $fleets = $this->Fleets_model->select_fleets();
    $this->data['fleets'] = $fleets;
        foreach($fleets as $row)
        {
         $cid = $row->category_id;
         $this->data['listefleets'] = $this->Fleets_model->list_fleets($cid);
        }
    $this->load->view('fleets',$this->data);    
    }

model
Code:
function select_fleets()
   {
            
    $this->db->select('car_category.category_name, car_category.category_id, car_category.details');
    $this->db->from('car_category');
    $this->db->join('cars', 'cars.category_id = car_category.category_id');
    $result_product = $this->db->get();
    return $result_product->result();
   }
function list_fleets($cid)
   {
   $this->db->where('category_id',$cid);
   $result_product = $this->db->get('cars');
   return $result_product->result();
   }
view
Code:
<?php foreach($fleets as $row)
{
?>
   <?php echo $row->category_name;?>
      
     <?php foreach($listefleets as $row)
     {
     ?>
         <?php echo $row->car_name;?>
     <?php
      }
     ?>
}
?>



Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]cideveloper[/eluser]
First thing. Please use code blocks for your code. Makes it much easier to read. like this

Controller
Code:
code here...
Model
Code:
code here...
View
Code:
code here...

Now on to your question. From the looks of you code am I correct in saying that for each category displayed the result is all the car names from the last category.

Category 1
------------
Car_name_1_from_Category_3
Car_name_2_from_Category_3
Car_name_3_from_Category_3

Category 2
------------
Car_name_1_from_Category_3
Car_name_2_from_Category_3
Car_name_3_from_Category_3

Category 3
------------
Car_name_1_from_Category_3
Car_name_2_from_Category_3
Car_name_3_from_Category_3

If I am correct, it is because of this

Code:
foreach($fleets as $row)
{
$cid = $row->category_id;
$this->data['listefleets'] = $this->Fleets_model->list_fleets($cid);
}

from this code it looks like every time you loop you are overwriting $this->data['listefleets'] so by the time you call the view your $this->data['listefleets'] only has the info from the last loop


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Bigil Michael[/eluser]
how do i solve this problem???


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Isern Palaus[/eluser]
Try:

Code:
foreach($fleets as $row)
{
     $cid = $row->category_id;
     $listefleets[] = $this->Fleets_model->list_fleets($cid);
}

$this->data["listefleets"] = $listefleets;



Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Bigil Michael[/eluser]
but it shows error in the view page

Message: Trying to get property of non-object

is there any change in the view page????


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Isern Palaus[/eluser]
Use <pre>&lt;?php print_r($variable);?&gt;</pre> to see what you've to render (an array).


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Bigil Michael[/eluser]
it shows the same car details in every categories


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]InsiteFX[/eluser]
Show your databse table structure, that could be the problem!

When dealing with categories any sub-categories need to have
a parent_id pointing to the main category.

InsiteFX


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Bigil Michael[/eluser]
can u help me?? i didn't used parent id till this time

table car_category
Code:
category_id, category_name, details, photo

table cars
Code:
id, car_name, category_id, description
i dont know for why we use parent id...
if i use parentid how i write query???

thanks in advance....


Selecting category details and products linked with the same category. - El Forum - 02-04-2011

[eluser]Bigil Michael[/eluser]
Group D

Daily Rate (AED) Weekly Rate (AED) Monthly Rate (AED) View Details
YARIS - 2011 100 630 2100 Click here
Lancer - 2010 100 600 1850 Click here

Group C

Daily Rate (AED) Weekly Rate (AED) Monthly Rate (AED) View Details
YARIS - 2011 100 630 2100 Click here
Lancer - 2010 100 600 1850 Click here


Group A

Daily Rate (AED) Weekly Rate (AED) Monthly Rate (AED) View Details
YARIS - 2011 100 630 2100 Click here
Lancer - 2010 100 600 1850 Click here


this is the result iam getting.

car group(category) name is different but car details are repeating
can any one help me???
urgent....