Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

PHP foreach loop with array table

How do I change it into table form with three headings which are: Company name (ABC and XYZ), Branch (Kuching and Sibu for both companies respectively), and Staff name (for both companies according to the array). I'm a beginner. Please help as I'm stuck. Something's wrong with my code.

<html>
<style>
</style>

<body>
<?php

$v_company= array(
    'ABC'=>array('Kuching'=>array('Michael', 'Jenny'), 
                'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
                ),
    'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'), 
                'Sibu'=>array('John', 'Alicia')
    )
);




?>

<table>

<tr><th>Company's Name</th>
    <th>Branch</th>
    <th>Staff's Name</th>
</tr>

<?php

foreach($v_company as $v_company_name=>$v_company_info){
    echo "<tr>";
    echo "<td>";
    echo "$v_company_name <br>";
    echo "</td>";
    echo "</tr>";


    foreach($v_company_info as $v_branch=>$v_staffs){

                echo "<td>";
                echo "$v_branch <br/>";
                echo "</td>";


        foreach($v_staffs as $v_staff){
                echo "<td>";
                echo "$v_staff <br/>";
                echo "</td>";
            echo "Company: ". $v_company_name. ",Branch: ". $v_branch. ",Staff: " .$v_staff . "<br>";
        }


    }   

}

?>
</table>
</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this code

<html>
    <style>
    </style>
    <body>
        <?php

$v_company= array(
    'ABC'=>
        array('Kuching'=>array('Michael', 'Jenny'), 
             'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
                ),
    'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'), 
                'Sibu'=>array('John', 'Alicia')
    )
);





?>
<table>

<tr><th>Company's Name</th>
    <th>Branch</th>
    <th>Staff's Name</th>
</tr>
<?php
$company = '';
$branch = '';
$staff = '';
foreach($v_company as $v_company_name=>$v_company_info){
    foreach($v_company_info as $v_branch=>$v_staffs){
        foreach($v_staffs as $v_staff){
              echo "<tr>";
                  echo "<td>";
                        if($company == '' || $company != $v_company_name){
                            $company = $v_company_name;
                            echo "$v_company_name <br>";
                        }

                  echo "</td>";
                  echo "<td>";
                        if($branch != $v_branch){
                            $branch = $v_branch;
                            echo "$v_branch <br>";
                        }

                  echo "</td>";
                  echo "<td>";
                      echo "$v_staff <br/>";
                  echo "</td>";
              echo "</tr>";  
         }
    }    
}
?>
</table>
</body>
</html>

You can copy paste if you want. Happy coding


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...