robcrozier Posted September 27, 2007 Share Posted September 27, 2007 OK what i have is a bunch of variables defined as follows: $m1 = $row3['machine1']; $m1t = $row3['machine1_taken']; $m1c = $row3['machine1_client']; $m2 = $row3['machine2']; $m2t = $row3['machine2_taken']; $m2c = $row3['machine2_client']; $m3 = $row3['machine3']; $m3t = $row3['machine3_taken']; $m3c = $row3['machine3_client']; $m4 = $row3['machine4']; $m4t = $row3['machine4_taken']; $m4c = $row3['machine4_client']; $m5 = $row3['machine5']; $m5t = $row3['machine5_taken']; $m5c = $row3['machine5_client']; These are just called from a database... no problems here. However... now what i want to do is use a loop in order to generate the number associated with each variable. For example the '3' in this variable '$m3'. This is just so i can first check if there's some data in the variable and then print out some data if there is. Here's the loop i have: for ($i = 1; $i < 6; $i ++) { if ($m[$i] != "") { echo "<td height='10%' align='right' valign='top' class='style11'>Machine [$i]:</td> <td>$m[$i]</td> <td height='10%' align='right' valign='top' class='style11'>Machine [$i] Taken:</td> <td>$m[$i]t</td> <td height='10%' align='right' valign='top' class='style11'>Machine [$i] - My percentage</td> <td>$m[$i]c</td>"; } else { //display nothing } } The problem is i just cant seem to get anything to print. What am i doing wrong? Can anyone help, cheers. Link to comment https://forums.phpfreaks.com/topic/70886-array-using-for-loop-help/ Share on other sites More sharing options...
Barand Posted September 27, 2007 Share Posted September 27, 2007 I'd work with row3 keys. (Well, actually I'd normalize the table) <?php for ($i=1; $i<6; $i++) { if ($row3["machine$i"]) { $m = $row3["machine$i"]; $mt = $row3["machine{$i}_taken"]; $mc = $row3["machine{$i}_client"]; echo "<td height='10%' align='right' valign='top' class='style11'>Machine [$i]:</td> <td>$m</td> <td height='10%' align='right' valign='top' class='style11'>Machine [$i] Taken:</td> <td>$mt</td> <td height='10%' align='right' valign='top' class='style11'>Machine [$i] - My percentage</td> <td>$mc</td>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/70886-array-using-for-loop-help/#findComment-356339 Share on other sites More sharing options...
sasa Posted September 27, 2007 Share Posted September 27, 2007 try for ($i = 1; $i < 6; $i ++) { if (${'m'.$i} != "") { echo "<td height='10%' align='right' valign='top' class='style11'>Machine [$i]:</td> <td>".${'m'.$i},"</td> <td height='10%' align='right' valign='top' class='style11'>Machine [$i] Taken:</td> <td>",${'m'.$i.'t'},"</td> <td height='10%' align='right' valign='top' class='style11'>Machine [$i] - My percentage</td> <td>",${'m'.$i.'c'},"</td>"; } else { //display nothing } } Link to comment https://forums.phpfreaks.com/topic/70886-array-using-for-loop-help/#findComment-356711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.