Reputation: 67
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.
Upvotes: 2
Views: 150
Reputation: 26902
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 = ' ';
$val2 = ' ';
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>';
Upvotes: 1
Reputation: 32918
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>";
Upvotes: 0
Reputation: 6149
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>";
Upvotes: 1
Reputation: 43547
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>';
}
Upvotes: 0
Reputation: 10377
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>";
Upvotes: 1
Reputation: 7380
Just make one while()
loop and echo the <tr><td>val</td><td>val2</td></tr>
part
Upvotes: 1