Welcome Guest, Not a member yet? Register   Sign In
2 foreach loops
#1

(This post was last modified: 06-07-2021, 12:32 AM by Lipe_Bogaci.)

Hello
How can i add 2 foreach loops in same table? One loop for users and another for user roles

<?php foreach($users as $row): ?>
 <tr>
  <td><?=$row['first_name']?></td>
  <td><?=$row['last_name']?></td>
  <td><?=$row['username']?></td>
  <td><?=$row['email']?></td>
  <td><?=$row['register_date']?></td>
  <td class="text-center"><?=$row['role_id']?></td>

</tr>
<?php endforeach?>
Reply
#2

(This post was last modified: 06-07-2021, 09:16 AM by captain-sensible.)

if in a controller you assign data in the form $data = [ 'user'=>$usr,
'first_name'=>$first_name,
//then i dont see why you can't also add :
'userRole'=>$userRole]

in which case you only need one foreach loop in a view


but you can do things such as in view:
<html>
<table>

<?php
foreach($result as $res)
// $result is whats passed from controller and is an array, $res is a convient var to pass put
{
echo "<td>". $res["first_name"]. " ".$res["last_name"]. " </td> ";
}

foreach($result2 as $res2)

{
echo "<td>". $res2["someDbColumn"]." ".$res2["something_else"]."</td";

}
// the php code will just get processed in a linear manner
?>

</table>
</html>
SO you can have two foreach in a single table; however the main problem is going to be lining things up
or you have to use "dummy" <td> </td> to fill in the gaps containing perhaps &nbsp; which is none breaking space code
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#3

you want merg two associates arreay to gather

https://www.w3schools.com/php/func_array_merge.asp

Or join two table select columns you need
Enlightenment  Is  Freedom
Reply




Theme © iAndrew 2016 - Forum software by © MyBB