如何在视图中显示数据库记录而不重复字段? [英] How to display database records in the view without repeating a field?

查看:323
本文介绍了如何在视图中显示数据库记录而不重复字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有两个表,一个是政治(politics_id,policys,policy_type_id),另一个是topo_politics(politics_type_id,policy_type).
这是表政治

In my database I have two tables one politics (politics_id, politics, politics_type_id) and other tipo_politics (politics_type_id, politics_type).
This is the table politics

politics_id | politics    | politics_type_id
--------    | --------    | -------------
1           | Politic 1   | 1
2           | hello world | 2
3           | magic       | 1
4           | love        |1

这是表 tipo_politics

 politics_type_id | politics_type
  -------------    | --------
  1                | general
  2                | life

我需要的是在视图中显示以政治类型为标题的政治表格的记录,但是标题不会重复.像这样的东西.

What I need is in the view to show the records of the politics table with the type of politics as the header, but the header is not repeated. Something like this.

**General**
- Politic 1
- magic
- love

**Life**
- hello world

我希望对分组依据进行查询,但只显示一个带有policy_type_id 1 的记录.这是视图代码.

I trate to do the query with a group by but only shows one record with politics_type_id 1. This is the view code.

 <?php
   for ($j = 0; $j < $contador; $j++) {
 ?>
   <div class="content-layout">
         <p>
           <span>
                <?php echo $list_politic[$j]['politics_type']; ?> 
           </span> 
         </p>
    </div>
    <div>
      <ul style="text-indent: 0px;">
         <li>
             <span>
                   <?php echo $list_politic[$j]['politics']; ?>   
             </span>
         </li> 
       </ul>
    </div>
 <?php } // for j ?>

使用此表单显示如下记录:

With this form show the records like this:

 **General**
    - Politic 1
 **General**
    - magic
 **General**
    - love

  **Life**
    - hello world

希望您能帮助我,我的问题是一个很好的问题.

I hope you can help me and that my question be a good question.

推荐答案

您似乎希望仅在politics_type发生更改时才显示标题.这将需要在每次迭代中跟踪其值,因此逻辑可以按以下方式构造:

It looks like you want the headers to be displayed only when there's a change in politics_type. This would require keeping a track of its value in every iteration and the logic can thus be constructed as follows:

<?php
$previous_politics_type = "";
for ($j = 0; $j < $contador; $j++) {
    if($previous_politics_type != $list_politic[$j]['politics_type']){  // <-- new if condition
?>

        <div class="content-layout">
            <p>
                <span>
                    <?php echo $list_politic[$j]['politics_type']; ?> 
                </span> 
            </p>
        </div>
<?php
    }       // <-- end if
    $previous_politics_type = $list_politic[$j]['politics_type'];
?> 
    <div>
        <ul style="text-indent: 0px;">
            <li>
                <span>
                    <?php echo $list_politic[$j]['politicas']; ?>   
                </span>
            </li> 
        </ul>
    </div>
<?php 
} // for j 
?>

上述<div>的布局可能与您需要的布局完全不同,但希望它可以使您对如何使用新的$previous_politics_type变量有所了解.

The layout of the <div>s above may or may not be exactly as you need, but hopefully it should give an idea on how to use the new $previous_politics_type variable.

这篇关于如何在视图中显示数据库记录而不重复字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆