我在将值从数组传递到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>';
}
}这里我想做的是先得到索引0,然后是索引2,然后是索引3。所以第一个可以创建3列,第二个可以创建2列,第三个可以创建1列。在这里,我想做的是先得到索引0,然后是索引2,然后是索引3。所以第一个可以创建3列,第二个可以创建2列,第三个可以创建1列。
你能帮我解决这个问题吗?
发布于 2016-07-27 20:09:41
使用下面的代码。它可能会对你有帮助
$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
}
}https://stackoverflow.com/questions/38612277
复制相似问题