简体   繁体   中英

For loop inside foreach loop php

I am successfully fetching students name and subject dynamically ( generate pdf ) in codeigniter. Now I want to fetch student record to get result like the following:

Student name   Drawing    Math   Computer
A               89        66     92         
B               65        72     83 
C               62        71     86 
D               78        73     83
E               82        91     82

I am getting student name vertically, now I want student name horizontally like above array, current array is

Array
(
    [0] => Array
    (
        [oral] => 30
        [Written] => 20
        [Drawing] => 10
        [Listening] => 70
    )

    [1] => Array
    (
        [oral] => 20
        [Written] => 60
        [Drawing] => 10
        [Listening] => 40
    )

    ...
)

Here is the code that generates view, where student record is shown vertically:

<?php foreach ($studentrecord as $rec) {?>
    <tr>
        <td width="200px"><span><?php echo $rec['StudentName'];?></span></td>
        <td><span>&nbsp;<?php ?></span></td>

        <td><span>&nbsp;</span></td>
        <td><span>&nbsp;</span></td>
        <td><span>&nbsp;</span></td>
    </tr>
<?php  } ?>

Now I want to fetch marks horizonaly, how do I do that?

You can use this.

<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td width="200px"><span><?php echo $rec['StudentName'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>

There is several way of doing it. Intuitively I would do a 2x loops.

<?php
$array = [["oral"=>12,"writen"=>20],["oral"=>13,"writen"=>15],["oral"=>12,"writen"=>18]];
?>
<!DOCTYPE html>
<html>
  <body>
       <table>
      <tr>
        <th>student</th>
        <th>oral</th>
        <th>writen</th>
      </tr>

        <?php
        foreach($array as $key => $value) {
            // $key is index of student
            // $value is his notes.
            echo "<tr>"; 
            echo "<td>".$key."</td>";
            // need to do a second loop for each mark 
            foreach($value as $key2 ) {
                echo "<td>";
                echo $key2;
                echo "<td>";
            }
            echo "</tr>";
        }
        ?>

    </table>
  </body>

</html>

the output will be : output

You can try this, do not forget to validate the possible empty situations

$studentrecord = Array(
                    0 => Array
                    (
                        'oral' => 30,
                        'Written' => 20,
                        'Drawing' => 10,
                        'Listening' => 70
                    ),
                    1 => Array
                    (
                        'oral' => 20,
                        'Written' => 60,
                        'Drawing' => 10,
                        'Listening' => 40
                    )
);

echo "<table border='1'>";
foreach ($studentrecord as $rec) 
{
    echo "<tr><td>".join($rec,"</td><td>")."</td></tr>";
}
echo "</table>";

Plz try this

        <?php foreach ($studentrecord as $key=> $rec) {?>
        <tr>
            <td width="200px"><span><?php echo $rec[$key]['StudentName'];?></span> 
        </td>
            <td><span>&nbsp;<?php ?></span></td>
            <td><span>&nbsp;</span></td>
            <td><span>&nbsp;</span></td>
            <td><span>&nbsp;</span></td>
        </tr>
        <?php  } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM