Welcome Guest, Not a member yet? Register   Sign In
How to sort a joined table with having the header just once in the output
#1

[eluser]Hantar[/eluser]
I'm new with the codeigniter framework, and learning on how to use the MVC pattern. I'm trying to achieve an output but that isn't really working for me, allow me to illustrate my problem with the code:

I have 3 tables in my DB (take note for explaining myself I made the table a bit more easier, but it really is needed to have 3 tables)

Code:
CREATE TABLE carBrand
(
    pk_brandID int,
    brand varchar(255)
);

CREATE TABLE carModel
(
    pk_modelID int,
    model varchar(255)
)

CREATE TABLE brandModel
(
    pk_brandmodelID int,
    project varchar(255),
    carOWner varchar(255),
    buildyear varchar(255),
    fk_brandID int,
    fk_modelID int
)

Joining the tables like so
Model
Code:
function getCarbyOwner(){
    $this->db->join('carBrand', 'carBrand.pk_brandID = brandModel.fk_brandID');
    $this->db->join('carModel', 'carModel.pk_modelID = brandModel.fk_modelID');
    $this->db->where('carOwner', 'xxx');
    $q = $this->db->get('brandModel');
    if($result = $q->result())
    {
        foreach($result as $key => $value)
        {
            $d[$value->pk_brandID]['brand'] = $value->brand;
            $d[$value->pk_brandID]['data'][$value->pk_modelID] = $value;
        }
        return $d;
    }
    else
        return false;
}

Displaying it in my view like this
Code:
if(isset($x)){
    foreach($x as $r)
    {
        echo $r['brand']."<br />";
        foreach($r['data'] as $v)
        {
            echo $v->model."<br />";
        }
        echo "<br />";
    }
}

And my controller

Code:
function index(){
    if($car= $this->car_model->getCarbyOwner()){
        $data['car'] = $car;
        $this->load->view('carview', $car);
    }
}



The records I have in the table brandModel are for example like this:

Code:
pk_brandmodelID | project | carowner | buildyear | fk_brandID | fk_modelID
1               | proj A  | frank    | 2012      | 1          | 6
2               | proj B  | Jef      | 2002      | 2          | 1
3               | proj C  | jeffrey  | 2013      | 1          | 5
4               | proj X  | frank    | 2010      | 2          | 2
5               | proj Y  | george   | 2008      | 1          | 3


Let's say brand 1 = audi and brand 2 is Mercedes The wanted output is like this

Quote:Audi
model fk_id 6
model fk_id 5
model fk_id 3

Mercedes
model fk_id 1
model fk_id 2

But now I'm only getting

Quote:Audi
model fk_id 6

Mercedes
model fk_id 1

I'm only getting the first model record. Can someone help me with this please?
#2

[eluser]SmokeyJoe[/eluser]
could it be that you're overwriting your array each time?
Code:
$d[$value->pk_brandID]['brand'] = $value->brand;
$d[$value->pk_brandID]['data'][$value->pk_modelID] = $value;
try
Code:
$d[$value->pk_brandmodelID]['brand'] = $value->brand;
$d[$value->pk_brandmodelID]['data'][$value->pk_modelID] = $value;




Theme © iAndrew 2016 - Forum software by © MyBB