循环以生成表格行

时间:2013-09-16 22:15:37

标签: php html-table

我有一个从数据库中提取的健身房机器的动态列表。

我想实现这个目标:

enter image description here

现在我想要组合2个不同的代码片段。

这会从数据库中提取机器:

$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();

此代码创建图片:

<?php
    $machine_id = 1;
    $count = mysql_result(mysql_query("SELECT COUNT(*) FROM `userPlanDetail` WHERE `user_id` = '$user_id1'"), 0);
    $sql = "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'";
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
    ?>
        $('<div/>', {
                'id': 'div _<?php echo $row["machine_id"]; ?>',
                'class': 'some_class_name',
                'html': '<img src="client workout page/round_<?php echo $machine_id++ ?>.jpg" style="position:absolute;top:50%;left:0;">'+
                        '<img src="<?php echo $row["picture"]; ?>" style="width:200px;height:200px;display:block;margin:20px auto;"/> '+
                       ' <a class="demo" href="<?php echo $row["link"]; ?>" rel="group1"><img src="client workout page/click to play video.jpg" style="width:186px;height: 14px;position:absolute;bottom:0;left:0;right:0;margin-left:auto;margin-right:auto;"></a>'+
                       ' <img src="" style="position:absolute;top:0;right:-15px;bottom:0;margin-top:auto;margin-bottom:auto;">'
            }).appendTo('.body1');
<?php   }

 ?>

此代码生成集合和代表:

$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 ),
);

$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, 1 ) as $chunk )
    echo '<tr>' . implode('', $chunk ) . '</tr>';
  echo '</table>';

我想以某种方式组合这些单独的代码片段以产生图片中的输出。现在图片很好但是套装和代表是在一个单独的表格中生成的。

1 个答案:

答案 0 :(得分:0)

在图片代码片段中:创建一个关联数组,其中包含将机器与集合/代表匹配所需的任何密钥,并将其发送到集合/代表代码片段。在那里你可以访问图片。