php - How to generate table rows based on data in database -
i trying take style of code , generate based on how many machines returned database.
i have this:
<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>
here database code retrieve machines database:
$sql1 = "select m1.machine_id, m2.* userplandetail m1 left join machines m2 on m1.machine_id = m2.machine_id `user_id` = '$user_id1' , `cardio` = 0"; $retval1 = mysql_query( $sql1, $conn ); $array = array(); while ($row = mysql_fetch_array($retval1, mysql_assoc)) { $array[] = $row['machine_name']; }
i want code detect how many machines in database aren't cardio machines , generate number of table rows accordingly.
here visual representation:
updated hard-coded machine attributes (not idea)
$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.* userplandetail m1 left join machines m2 on m1.machine_id = m2.machine_id `user_id` = '$user_id1' , `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 ) $chunk ) echo '<tr>' . implode('', $chunk ) . '</tr>'; echo '</table>';
original answer
assuming number of sets, number of reps, , weight in results set of query:
$sql1 = "select m1.machine_id, m2.* userplandetail m1 left join machines m2 on m1.machine_id = m2.machine_id `user_id` = '$user_id1' , `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>';
Comments
Post a Comment