在2循环中显示自定义标题

时间:2017-09-08 04:16:48

标签: php html-table

我在页面的表格中显示了10个数据库结果。最后2个结果,例如。第9行和第10行是桌面上的自定义标题。显示它们的代码

if($result_rates->num_rows>0){
    $counter = 1;
    foreach ($result_rates as $r){
         if($counter <9 )
         {    
             echo '<tr>
                      <td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>                           
                      <td>                      
                      <td>
                         <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                      </td>
                   </tr>';
          }
          else {

              echo '<tr>
                      <td id="people-'.$r['number_of_people'].'">Number '.$counter.'</td>                          
                      <td>
                        <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                      </td>
                    </tr>';                                                     
                }
                $counter++;
    }    
}

页面上显示的结果是

enter image description here

是否可以将最后两行Number 9Number 10个不同的标题放在一起?目标是最后一行都是自定义的

我试图在else块中添加2 <tr>但是它们已经翻了一倍。

3 个答案:

答案 0 :(得分:2)

试试这个:

if($result_rates->num_rows>0){
$counter = 1;
foreach ($result_rates as $r){
     if($counter <9 )
     {    
         echo '<tr>
                  <td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>                           
                  <td>                      
                  <td>
                     <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                  </td>
               </tr>';
      }
      elseif($counter == 9){

          echo '<tr>
                  <td id="people-'.$r['number_of_people'].'">Number 9 '.$counter.'</td>                          
                  <td>
                    <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                  </td>
                </tr>';                                                     
            }
        elseif($counter == 10){

          echo '<tr>
                  <td id="people-'.$r['number_of_people'].'">Number 10  '.$counter.'</td>                          
                  <td>
                    <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                  </td>
                </tr>';                                                     
            }
            $counter++;
     }    
   }

如果您想最小化代码,也可以尝试此操作:

if($result_rates->num_rows>0){
$counter = 1;
foreach ($result_rates as $r){
     if($counter <9 )
     {    
         echo '<tr>
                  <td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>                           
                  <td>                      
                  <td>
                     <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                  </td>
               </tr>';
      }
      else{
          if($counter == 9){
             $custom_title= "number 9";
          }elseif($counter == 10){
             $custom_title= "number 10";
          }
          echo '<tr>
                  <td id="people-'.$r['number_of_people'].'">'.$custom_title.' '.$counter.'</td>                          
                  <td>
                    <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                  </td>
                </tr>';                                                     
            }

            $counter++;
     }    
   }

答案 1 :(得分:2)

您可以这样编写,这样您就不需要再次编写相同的代码了。

if ($result_rates->num_rows > 0) {
    $counter = 1;
    foreach ($result_rates as $r) {
        if ($counter < 9) {
            $custom_titles = $r['number_of_people'];
        } else {
            $custom_titles = "Number " . $counter;
        }
        echo '<tr>
                      <td id="people-' . $r['number_of_people'] . '">' . $custom_titles . '</td>                           
                      <td>                      
                      <td>
                         <button id="' . $r['rate_id'] . '" onclick="edit_row(' . $r['rate_id'] . ')" class="btn btn-primary edit_button">Edit</button>
                      </td>
                   </tr>';
        $counter++;
    }
}

答案 2 :(得分:1)

根据您的问题和评论,我认为您希望动态获取最后两行(不是修复9&amp; 10)

$total_records = $result_rates->num_rows; //count number of rows
if($result_rates->num_rows>0){
    $counter = 1;
    foreach ($result_rates as $r){
         if($counter <($total_records-2) ) //(rows - 2) i.e. before last two rows
         {    
             echo '<tr>
                      <td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>                           
                      <td>                      
                      <td>
                         <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                      </td>
                   </tr>';
          }
          else {

              echo '<tr>
                      <td id="people-'.$r['number_of_people'].'">Number '.$counter.'</td>                          
                      <td>
                        <button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
                      </td>
                    </tr>';                                                     
                }
                $counter++;
    }    
}