PHP while循环动态行跨度

PHP while循环动态行跨度,php,while-loop,html-table,Php,While Loop,Html Table,以一个表为例 使用这些表,我只想通过将rowspan添加到项目ID1002来打印这样的表 这是我的PHP代码 $temp_val = ''; $counter = 1; $sql_sel = mysqli_query($con,"select * from item_table"); while($res_sel = mysqli_fetch_array($sql_sel)){ if($temp_val == $res_sel['item_id']){ $count

以一个表为例

使用这些表,我只想通过将rowspan添加到项目ID1002来打印这样的表

这是我的PHP代码

$temp_val = '';
$counter = 1;
$sql_sel = mysqli_query($con,"select * from item_table");
while($res_sel = mysqli_fetch_array($sql_sel)){

    if($temp_val == $res_sel['item_id']){
        $counter++;
        echo "<tr></tr>";
    }
    else{
       echo "<tr><td rowspan='".$counter."'>".$res_sel['item_id']."</td></tr>"; 
    }

    $temp_val = $res_sel['item_id'];

}
echo "</table>";
$temp_val='';
$counter=1;
$sql_sel=mysqli_query($con,“从项_表中选择*);
而($res\u sel=mysqli\u fetch\u数组($sql\u sel)){
如果($temp\u val=$res\u sel['item\u id'])){
$counter++;
回声“;
}
否则{
回显“$res_sel['item_id']”;
}
$temp_val=$res_sel['item_id'];
}
回声“;

这是不对的,它将rowspan添加到项目id 1003

您应该切换
else
if
语句中的代码您应该切换
else
if
语句中的代码您不能这样做,因为当您创建第一行时,您必须决定此列可以跨越多少,以便必须首先获得相似ID的计数才能实现它。我只是给你们一个想法,告诉你们它如何和你们的数据库提供的数组一起工作

<?php

// Array comming from database
$databaseValues = [
    [
        'item_id'=>'1001',
        'item_color'=>'black',
    ],
    [
        'item_id'=>'1002',
        'item_color'=>'blue',
    ],
    [
        'item_id'=>'1002',
        'item_color'=>'green',
    ],
    [
        'item_id'=>'1003',
        'item_color'=>'red',
    ]
];

// Creating an array as per the need for the table
$arrayForTable = [];
foreach ($databaseValues as $databaseValue) {
    $temp = [];
    $temp['item_color'] = $databaseValue['item_color'];
    if(!isset($arrayForTable[$databaseValue['item_id']])){
        $arrayForTable[$databaseValue['item_id']] = [];
    }
    $arrayForTable[$databaseValue['item_id']][] = $temp;
}

?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<table border="1">
    <?php foreach ($arrayForTable as $id=>$values) :
        foreach ($values as $key=>$value) :?>
    <tr>
        <?php if($key == 0) :?>
        <td rowspan="<?= count($values)?>"><?= $id?></td>
        <?php endif;?>
        <td><?= $value['item_color']?></td>
    </tr>
    <?php endforeach;
    endforeach; ?>
</table>

</body>
</html>

文件

希望这对您有帮助

您不能这样做,因为当您创建第一行时,您必须决定此列可以跨越多少,因此您必须首先获得类似ID的计数才能实现它。我只是给你们一个想法,告诉你们它如何和你们的数据库提供的数组一起工作

<?php

// Array comming from database
$databaseValues = [
    [
        'item_id'=>'1001',
        'item_color'=>'black',
    ],
    [
        'item_id'=>'1002',
        'item_color'=>'blue',
    ],
    [
        'item_id'=>'1002',
        'item_color'=>'green',
    ],
    [
        'item_id'=>'1003',
        'item_color'=>'red',
    ]
];

// Creating an array as per the need for the table
$arrayForTable = [];
foreach ($databaseValues as $databaseValue) {
    $temp = [];
    $temp['item_color'] = $databaseValue['item_color'];
    if(!isset($arrayForTable[$databaseValue['item_id']])){
        $arrayForTable[$databaseValue['item_id']] = [];
    }
    $arrayForTable[$databaseValue['item_id']][] = $temp;
}

?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<table border="1">
    <?php foreach ($arrayForTable as $id=>$values) :
        foreach ($values as $key=>$value) :?>
    <tr>
        <?php if($key == 0) :?>
        <td rowspan="<?= count($values)?>"><?= $id?></td>
        <?php endif;?>
        <td><?= $value['item_color']?></td>
    </tr>
    <?php endforeach;
    endforeach; ?>
</table>

</body>
</html>

文件

希望这将对您有所帮助

您已经在这里问过这个问题。@Jasir,检查我的回答是否有趣?我的回答是-。你已经在这里问过这个问题了。@Jasir,检查我的回答是否有趣?我的答案是-。尝试放置代码
尝试放置代码