gpt4 book ai didi

php - 将数组值传递给 colspan 以在 php 中创建列表

转载 作者:行者123 更新时间:2023-11-29 11:23:17 25 4
gpt4 key购买 nike

我在将值从数组传递到 colspan 时遇到问题,因此我可以拥有动态多列。

$my_values = array();
while ($data=mysqli_fetch_assoc($result)) {
foreach ($data as $key => $value) {
$my_values[] = $value;
}
}
print_r($my_values);

这是我打印 $my_values 得到的输出:

Array ( [0] => 3 [1] => 2 [2] => 1 )

我在这里尝试做的是创建表并将这些数组传递给 colspan:

$query = "SELECT * FROM assignments WHERE course_id=1";
$result1 = mysqli_query($conn, $query);
if (mysqli_num_rows($result1) != 0) {
while ($row=mysqli_fetch_assoc($result1)) {
echo '<th colspan="'.$my_values['0'].'">'.$row['assignment_name'].'</th>';
}
}

其中 $my_values['0'] 只会得到 3。我在这里想做的是我想先得到索引 0,然后是索引 2,然后是索引 3。所以第一个第一个可以创建 3 列,第二个可以创建 2 列,第三个可以创建 1 列。

你们能帮我解决这个问题吗?

最佳答案

使用下面的代码。它可能会帮助你

$query = "SELECT * FROM assignments WHERE course_id=1";
$result1 = mysqli_query($conn, $query);
if (mysqli_num_rows($result1) != 0) {
$i = 0; //-------------->INITIALIZE THE VARIABLE
while ($row=mysqli_fetch_assoc($result1)) {
echo '<th colspan="'.$my_values[$i].'">'.$row['assignment_name'].'</th>';
$i++; //----------------> INCREMENT THE VARIABLE
}
}

关于php - 将数组值传递给 colspan 以在 php 中创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38612277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com