我需要在两个列中显示一些数据,进行搜索并找到一些东西,然后尝试.我认为我所做的工作可行,但我做错了,但部分可行.
这是我的数据库
**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
我的模特
$query1 = "select * from type";
$query2 = "select * from content where id_type = $id_type";
注意:此查询进入各自的功能
我的控制器
$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
在我看来
<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>
这个显示这个结果
-------------------------
| First Title |
-------------------------
|Content1 | |
-------------------------
| Second Title |
-------------------------
|Content2 | |
-------------------------
|Content3 | |
-------------------------
| Third Title |
-------------------------
|Content4 | |
-------------------------
|Content5 | Content6 |
我想给我看一些这样的东西
-------------------------
| First Title |
-------------------------
|Content1 | |
-------------------------
| Second Title |
-------------------------
|Content2 | Content3 |
-------------------------
| Third Title |
-------------------------
|Content4 | Content5 |
-------------------------
| Content6 | |
谢谢你的帮助.
解决方法:
让我们通过加入来完成您的任务.
为了您的理解,我首先创建了简单的MySQL查询.
从内容c左连接中选择*从c.id_type = t.id_type上键入t
您可以在单个查询中轻松获得所需的输出.
现在让我们在CI中做同样的事情.
内部控制器
$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();
我假设您通过了此$结果以查看;
视图
<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>