How to display database records in the view without repeating a field?

后端 未结 2 722
独厮守ぢ
独厮守ぢ 2021-01-22 18:57

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

相关标签:
2条回答
  • 2021-01-22 19:22

    If you make a query like this, then you'll get a row for the type as well as the actual politics:

    select tp.politics_type_id, tp.politics_type politics_text, null politics_id,  1 row_type
    from tipo_politics tp
    union
    select p.politics_type_id, p.politics politics_text, p.politics_id, 2 row_type
    from politics p
    order by politics_type_id, row_type, politics_id;
    

    This gives results like this:

    +------------------+---------------+-------------+----------+
    | politics_type_id | politics_text | politics_id | row_type |
    +------------------+---------------+-------------+----------+
    |                1 | general       |             |        1 |
    |                1 | Politic 1     |           1 |        2 |
    |                1 | magic         |           3 |        2 |
    |                1 | love          |           4 |        2 |
    |                2 | life          |             |        1 |
    |                2 | hello world   |           2 |        2 |
    +------------------+---------------+-------------+----------+
    

    Now you can loop through the results, and only act on the header rows (where row_type = 1). Then within that loop, do another loop and pick out the detail rows (where row_type = 2).

    <?php
       for ($j = 0; $j < $contador; $j++) {
         if($list_politic[$j]['row_type'] == 1){
     ?>
       <div class="content-layout">
             <p>
               <span>
                    <?php echo $list_politic[$j]['politics_text']; ?> 
               </span> 
             </p>
        </div>
        <div>
          <ul style="text-indent: 0px;">
             <li>
              <?php for($i=0; $i < $contador; $i++) {
                    if($list_politic[$i]['row_type'] == 2 && $list_politic[$i]['politics_type_id'] == $list_politic[$j]['politics_type_id']) {
              ?>
                 <span>
                       <?php echo $list_politic[$j]['politics_text']; ?>   
                 </span>
                <?php } // if ?>
               <?php } // for i ?>
             </li> 
           </ul>
        </div>
       <?php } // for if ?>
     <?php } // for j ?>
    
    0 讨论(0)
  • 2021-01-22 19:27

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

    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.

    0 讨论(0)
提交回复
热议问题