php - 如何在两列中显示数据?

标签 php codeigniter

我需要在两个列中显示一些数据,我进行搜索并找到一些东西,然后尝试。我认为我所做的是有效的,但我错了,部分有效。

这是我的数据库

  **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";

注意:这个查询在各自的函数中进行

我的 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

在我看来

<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 |            |

感谢您的帮助。

答案的附加信息 Image of my view

修改后的 View view after modifications

最佳答案

让我们用 join 完成你的任务。

为了您的理解,我首先创建了简单的 MySQL 查询。

select * from Content c left join Type t on c.id_type = t.id_type

您可以在单个查询中轻松获得所需的输出。

现在让我们在 CI 中做同样的事情。 内部 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();

我假设您将此 $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>

关于php - 如何在两列中显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44312833/

相关文章:

PHP - 生成器,发送不遵循产出顺序

php - Codeigniter 选择和(有条件的)更新查询

php - CI 查询结果给出意外结果

javascript - Ajax 请求返回 Jquery 中使用的元素

php - Zend_Search_Lucene 尝试分配 3503812093817007931 字节

php - sendfax php 不工作,给出代码 255

php - 如何在使用 PHP 和 jQuery 成功发布表单时显示警报?

php - 来自 MySQL 的具有最新和唯一值的 HTML 表

php - 使用 CodeIgniter 查询数据库以获取过去一周内提交的条目?

php - 数据未正确来自数据库