Php 在2上显示自定义标题<;tr>;循环中
我在页面上的表中显示了来自数据库的10个结果。最后两个结果(如第9行和第10行)在表中有自定义标题。显示它们的代码Php 在2上显示自定义标题<;tr>;循环中,php,html-table,Php,Html Table,我在页面上的表中显示了来自数据库的10个结果。最后两个结果(如第9行和第10行)在表中有自定义标题。显示它们的代码 if($result_rates->num_rows>0){ $counter = 1; foreach ($result_rates as $r){ if($counter <9 ) { echo '<tr> <td
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++;
}
}
if($result\u rates->num\u rows>0){
$counter=1;
foreach($r的结果){
如果($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>';
}
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\u rates->num\u rows>0){
$counter=1;
foreach($r的结果){
如果($counter num_rows>0){
$counter=1;
foreach($r的结果){
如果($counter来自您的问题和评论,我认为您希望动态获取最后两行(而不是修复9和10)
$total\u records=$result\u rates->num\u rows;//计算行数
如果($result\u rates->num\u rows>0){
$counter=1;
foreach($r的结果){
如果($counter,您可以这样编写,这样就不需要再次编写相同的代码
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++;
}
}
if($result\u rates->num\u rows>0){
$counter=1;
foreach($r的结果){
如果($counter<9){
$custom_titles=$r[“人数”];
}否则{
$custom_titles=“Number”。$counter;
}
回声'
“.$custom_titles”
编辑
';
$counter++;
}
}
您想要什么样的预期结果?显示我们右侧第二列。为了简单起见,我删除了它。我想要的是手动添加最后两行,而不是第9/10号
来编写第1/2天
。在数据库中,这两行的值是9
和10
,如果重要的话。感谢回答:是的,但我试过这样做,在else块中,我需要显示两行不同的标题,如@Pritamkumar solution。
$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++;
}
}
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++;
}
}