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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.