Reputation: 197
I need to show some data in two colums, I make a search and find some things and I try. I think that what I do It works but I was rong, works partially.
This is my database
**Type**
--------
id_type | type
------------|------------
1 | first Title
2 | second Title
3 | Third Title
**Content**
---------
id_content | content | id_type
-----------|-----------|---------
1 | Content1 | 1
2 | Content2 | 2
3 | Content3 | 2
4 | Content4 | 3
5 | Content5 | 3
6 | Content6 | 3
my model
$query1 = "select * from type";
$query2 = "select * from content where id_type = $id_type";
Note: this query goes in ther respective function
my controller
$list_content = array(array());
$contador = 0;
$list_type = array(array());
$cont_type = 0;
$query1 = $obj->Search_Type();
for ($j = 0; $j < $query1->num_rows; $j++) {
$id_type = $list_type[$cont_type]['id_type'] = $query1->row($j)->id_type;
$query2 = $obj->Search_Content($id_type);
for ($i = 0; $i < $query2->num_rows; $i++) {
id_content = $list_content[$contador]['id_content'] = $query2->row($i)->id_content;
$list_content[$contador]['id_type'] = $query2->row($i)->id_type;
$contador++;
} //end for i
$list_type[$cont_type]['id_receptor'] = $id_content;
$cont_type++;
}//del for j
in my view
<table border='1'>
<?php $columna = 1;
for ($j = 0; $j < $cont_type; $j++) {
if ($list_type[$j]['id_type'] != '') {
?>
<tr>
<td colspan = '2'>
<?php echo $list_tipo_receptor[$j]['tipo_receptor_TVD']; ?>
</td>
</tr>
<?php
}// if type no empty
for ($i = 0; $i < $contador; $i++) {
if ($list_type[$j]['id_type'] == $list_content[$i]['id_type']) {
if ($columna == 1) {
?>
<tr>
<?php } ?>
td> <?php echo $list_content[$i]['id_content']; ?></td>
<?php if ($columna != 1) { ?>
</tr>
<?php
$columna = 1;
}// if columna != 1
else {
$columna++;
}// else
}//if
} //for i
}// for j
?>
</table>
this show this result
-------------------------
| First Title |
-------------------------
|Content1 | |
-------------------------
| Second Title |
-------------------------
|Content2 | |
-------------------------
|Content3 | |
-------------------------
| Third Title |
-------------------------
|Content4 | |
-------------------------
|Content5 | Content6 |
and I want to show me some like this
-------------------------
| First Title |
-------------------------
|Content1 | |
-------------------------
| Second Title |
-------------------------
|Content2 | Content3 |
-------------------------
| Third Title |
-------------------------
|Content4 | Content5 |
-------------------------
| Content6 | |
Thanks for your help.
Additional Information of the answer
Upvotes: 3
Views: 85
Reputation: 2643
Let's do your task with join
.
For your understanding i am creating simple MySQL query first.
select * from Content c left join Type t on c.id_type = t.id_type
you can easily get your desired output in single query.
Let's do same thing in CI now. inside controller
$result = $this->db->select('c.content, c.id_type, t.type')
->from('Content c')
->join('Type t','c.id_type = t.id_type','left')
->order_by('id_type')
->get()
->result();
i assume you pass this $result
to view;
view
<table>
<?php
$previous_id_type = 0;
$col_count =0;
foreach($result as $row){
if($row->id_type != $previous_id_type){
$previous_id_type = $row->id_type;
$col_count = 0;
?>
<tr><td colspan="2"><?php echo $row->type; ?> </td></tr><tr>
<?php
}?>
<td> <?php echo $row->content; ?> </td>
<?php
if( $col_count == 1){
echo '</tr>';
}
$col_count++;
}
?>
} ?>
</tr>
</table>
Upvotes: 2
Reputation: 431
hei you can use this join query
$this->db->select('*');
$this->db->from('content');
$this->db->join('type', 'content.id_type = type.id');
$query = $this->db->get();
if you want too loop you can return with
return $query->result();
if not
return $query->row();
hope solve your problem
Upvotes: 1