Jump to content

array using for loop HELP!!!!


robcrozier

Recommended Posts

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

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>";
    }
}
?>

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
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.