将数组值传递给colspan以在php中创建列表
I am having a problem to pass a value from array to colspan, so I can have dynamic multi column.
$my_values = array();
while ($data=mysqli_fetch_assoc($result)) {
foreach ($data as $key => $value) {
$my_values[] = $value;
}
}
print_r($my_values);
and this is the output the I got for printing the $my_values:
Array ( [0] => 3 [1] => 2 [2] => 1 )
What I am trying to do here is creating table and pass those array to 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>';
}
}
where is $my_values['0']
will get just 3. what I am trying to do here is I want to get index 0 first, then index 2 then index 3. so the first one can create 3 columns and the second one 2 columns and the third 1 columns.
can you help me guys with this issue?
我有一个问题是将值从数组传递给colspan,所以我可以使用动态多列。 p>
$ my_values = array();
while($ data = mysqli_fetch_assoc($ result)){
foreach($ data as $ key =&gt; $ value){
$ my_values [] = $ value;
}
}
print_r($ my_values);
code> pre>
这是我打印$的输出 my_values: p>
Array([0] =&gt; 3 [1] =&gt; 2 [2] =&gt; 1)
code> pre>
我在这里要做的是创建表并将这些数组传递给colspan: p>
$ query =“SELECT * FROM assignmentments WHERE course_id = 1 “;
$ result1 = mysqli_query($ conn,$ query);
if(mysqli_num_rows($ result1)!= 0){
while($ row = mysqli_fetch_assoc($ result1)){
echo'&lt; th colspan =“'。$ my_values ['0']。'”&gt;'。$ row ['assignment_name']。'&lt; / th&gt;';
}
}
code> pre>
$ my_values ['0'] code>将在哪里得到3.什么 我想在这里做的是我想首先得到索引0,然后索引2然后索引3.所以第一个可以创建3列,第二个可以创建2列,第三个列。 p>
你可以帮我解决这个问题吗? p>
div>
Use below code. it may help you with
$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
}
}