2

echo ""; while (list ($key, $val) = each ($users)) { echo "$val\n\n"; } echo ""; while (list ($key2, $val2) = each ($enable)) { echo "$val2\n\n"; } echo "";

I want to format this into table with side by side where should be

$val $val2

Currently its in

$val
$val2

What should i amend to achieve this?

Thank you for your kind assistance.

2
  • Do you want all the users on one row and then all the enable on the next row? Or User / Enable on one row and then User / Enable on the next row? Commented Jan 14, 2010 at 15:40
  • My question to you is why would you have two separate arrays for $users and $enable? It seems as though you should just have a multi-dimensional array with the user and whether or not they are enabled... Commented Jan 14, 2010 at 15:53

6 Answers 6

1

I think I see what you're trying to do now. Something to this effect?:

echo "<table>";
while (list ($key, $val) = each ($users)) {
list ($key2, $val2) = each ($enable);
echo "<tr><td>$val</td>";
echo "<td>$val2</td></tr>";
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

1 Comment

This is a little safer: echo "<table>"; while (list ($key, $val) = each ($users)) { echo "<tr><td>$val</td>"; if (list ($key2, $val2) = each ($enable)) echo "<td>$val2</td>"; echo "</tr>"; } echo "</table>";
1

Just make one while() loop and echo the <tr><td>val</td><td>val2</td></tr> part

Comments

1

If you want the format user/ enable on one line and then user/enable on the next line, you'll need to do some array work first so you can get them in the same loop.

echo "<table>";
foreach(array_combine($users, $enable) as $u => $e){
   echo "<tr><td>$u</td><td>$e</td></tr>\n\n";
}
echo "</table>";

Comments

1

You need to do some trickery to loop through both arrays at the same time since they need to be on the same row:

echo '<table>';
$user_count = count($users);
$enable_count = count($enable);
$max = max($user_count, $enable_count );
for ($i = 0; $i < $max; $i ++) {
    $val = '&nbsp;';
    $val2 = '&nbsp;';
    if ($i < $user_count) $val = $users[$i];
    if ($i < $enable_count) $val2 = $enable[$enable];
    echo "<tr><td>$val</td><td>$val2</td></tr>";
}
echo '</table>';

Comments

0

You can condense this into a single loop for optimal performance assuming your keys are integers starting at 0:

$len = min(count($users), count($enable));
if ($len > 0) {
    echo '<table>';
    for ($i = 0; $i < $len; ++$i) {
        echo '<tr><td>' . $users[$i] . '</td><td>' . $enable[$i] . '</td></tr>';
    }
    echo '</table>';
}

1 Comment

this will not work except that the $users and $enable has the same index or keys which usualy arent, have a look at my for loop ist that cool?
0

hmm you could do that in a for loop this is smart and is readable

echo "<table><tr>";
for($u=0;$e=0;$u<count($users),$e<count($enable);$i++,$u++)
{
echo "<td>$users[$u]['idntknowthekeysethere']</td></tr>\n\n";
echo "<td>$enable[$e]['idntknowthekeysethere']</td></tr>\n\n";
}

echo "</tr></table>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.