我正在尝试采用这种代码并根据从数据库返回的机器数生成。
我有这个:
<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">
<tr>
<td><?php echo $array[0]; ?> <span class="blue">#1</span><br>
sets <span class="blue">1</span> reps <span class="blue">50</span><br>
weight <span class="blue">25</span></td>
<td><?php echo $array[1]; ?> <span class="blue">#2</span><br>
sets <span class="blue">1</span> reps <span class="blue">100</span><br>
weight <span class="blue">40</span></td>
</tr>
<tr>
<td><?php echo $array[2]; ?> <span class="blue">#3</span><br>
sets <span class="blue">2</span> reps <span class="blue">25</span><br>
weight <span class="blue">20</span></td>
<td><?php echo $array[3]; ?> <span class="blue">#4</span><br>
sets <span class="blue">3</span> reps <span class="blue">25</span><br>
weight <span class="blue">30</span></td>
</tr>
</table>
以下是从数据库中检索计算机的数据库代码:
$sql1 = "SELECT m1.machine_id, m2.* FROM userPlanDetail AS m1 LEFT JOIN machines AS m2 ON m1.machine_id = m2.machine_id WHERE `user_id` = '$user_id1' AND `cardio` = 0";
$retval1 = mysql_query( $sql1, $conn );
$array = array();
while ($row = mysql_fetch_array($retval1, MYSQL_ASSOC)) {
$array[] = $row['machine_name'];
}
我希望代码检测数据库中有多少台机器不是有氧运动机器,并相应地生成表格行数。
这是一个直观的表示:
答案 0 :(得分:2)
由于您要将所有结果加载到数组中,因此可以使用array_chunk将数组拆分为行,然后相应地输出。
$array = array_chunk($array, 4);
echo '<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">';
foreach($array as $row){
echo "<tr>";
foreach($row as $cell){
// echo out your tds for format each cell here.
}
echo "</tr>";
}
echo '</table>';
答案 1 :(得分:0)
<?php
for ($x=0; $x<sizeOf($array); $x++) {
echo '<tr><td>Array Data Here</td></tr>';
}
?>
答案 2 :(得分:0)
更新了硬编码的机器属性(不是一个好主意)
$machine_atts = array(
'bicep curl' => array( 'sets' => 1, 'reps' => 50, 'weight' => 25 ),
'cable chest press' => array( 'sets' => 1, 'reps' => 100, 'weight' => 40 ),
'lat pulldown' => array( 'sets' => 2, 'reps' => 25, 'weight' => 20 ),
'tricep extension' => array( 'sets' => 3, 'reps' => 25, 'weight' => 30 ),
);
$sql1 = "SELECT m1.machine_id, m2.* FROM userPlanDetail AS m1 LEFT JOIN machines AS m2 ON m1.machine_id = m2.machine_id WHERE `user_id` = '$user_id1' AND `cardio` = 0";
$retval1 = mysql_query( $sql1, $conn );
$array = array();
$i=0;
while ($row = mysql_fetch_array($retval1, MYSQL_ASSOC)) {
$i++;
$sets = $machine_atts[$row['machine_name']]['sets'];
$reps = $machine_atts[$row['machine_name']]['reps'];
$weight = $machine_atts[$row['machine_name']]['weight'];
$array[] = '<td>'.$row['machine_name'].'> <span class="blue">'.$i.'</span><br>sets <span class="blue">'.$sets.'</span> reps <span class="blue">'.$reps.'</span><br>weight <span class="blue">'.$weight.'</span></td>';
}
echo '<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">';
foreach ( array_chunk( $array, 2 ) as $chunk )
echo '<tr>' . implode('', $chunk ) . '</tr>';
echo '</table>';
原始答案
假设查询结果集中的集数,代表数和权重:
$sql1 = "SELECT m1.machine_id, m2.* FROM userPlanDetail AS m1 LEFT JOIN machines AS m2 ON m1.machine_id = m2.machine_id WHERE `user_id` = '$user_id1' AND `cardio` = 0";
$retval1 = mysql_query( $sql1, $conn );
$array = array();
$i=0;
while ($row = mysql_fetch_array($retval1, MYSQL_ASSOC)) {
$i++;
$array[] = '<td>'.$row['machine_name'].'> <span class="blue">'.$i.'</span><br>sets <span class="blue">'.$row['sets'].'</span> reps <span class="blue">'.$row['reps'].'</span><br>weight <span class="blue">'.$row['weight'].'</span></td>';
}
echo '<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">';
echo '<tr>' . implode('</tr><tr>', array_chunk( $array, 2 ) ) . '</tr>';
echo '</table>';