-1

Here I having one multidimensional array; in this array I am having two indexes:

  1. mandatoryCheckingReport
  2. specialCharectorReport

Now what I want to do means I have to take the values from that two index and I have to display in html table, but I don't know how display array values in my html table. I am new to development, please update me with an answer.

print_r($viewWorkSheet);

    Array
(
    [mandatoryCheckingReport] => Array
        (
            [uniformID] => Array
                (
                    [testCase] => uniformID empty checking
                    [devTeamResult] => success
                    [testingTeamResult] => test case success
                )

            [studentID] => Array
                (
                    [testCase] => studentID empty checking
                    [resultCode] => C001
                    [devTeamResult] => success
                    [testingTeamResult] => test case success
                )
        )

    [specialCharectorReport] => Array
        (
            [uniformID] => Array
                (
                    [testCase] => uniformID specialCharector checking
                    [devTeamResult] => success
                    [testingTeamResult] => test case failure
                )

            [studentID] => Array
                (
                    [testCase] => studentID specialCharector checking
                    [devTeamResult] => success
                    [testingTeamResult] => test case failure
                )
       )

)

My expected output is like this:

<!DOCTYPE html>
<html>
<head>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>

<h2>HTML Table</h2>

 <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>Heading</th>
                  <th>Test Case</th>
      <th>Input Field</th>
                  <th>DevTeam Result</th>
                  <th>TestingTeam Result</th>
                  <th>Expected Result</th>
                </tr>
                </thead>
                <tbody>
    <!-- mandatoryCheckingReport array values start here -->
                <tr>
      <td rowspan="5">mandatoryCheckingReport</td>
                  <td>uniformID empty checking</td>
                   <td>uniformID</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case success</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
               <tr>
                  <td>studentID empty checking</td>
                   <td>studentID</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case success</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
     <tr>
                  <td>startFrom empty checking</td>
                   <td>startFrom</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case success</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
    <tr>
                  <td>limit empty checking</td>
                   <td>limit</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case success</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
    <tr>
                  <td>profileID empty checking</td>
                   <td>profileID</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case failure</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
    
    <!-- specialCharectorReport array values start here -->
    
    
    <tr>
      <td rowspan="5">specialCharectorReport</td>
                  <td>uniformID specialCharector checking</td>
                   <td>uniformID</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case failure</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
               <tr>
                  <td>studentID specialCharector checking</td>
                   <td>studentID</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case failure</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
     <tr>
                  <td>startFrom specialCharector checking</td>
                   <td>startFrom</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case success</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
    <tr>
                  <td>limit specialCharector checking</td>
                   <td>limit</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case success</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
    <tr>
                  <td>profileID specialCharector checking</td>
                   <td>profileID</td>
                  <td>success</td>
                  <td><small class="label label-success"> test case failure</small></td>
                  <td>OK</td><!-- Static is Ok fine -->
                </tr>
    
                </tbody>
          </table>
</body>
</html>
  • Could you share any code you've tried to use to solve your problem? I'd imagine using nested `foreach` loops in the template would work. https://stackoverflow.com/questions/8283892/php-html-template-with-loop-capabilities is a similar question. – Scotty Waggoner Jul 19 '18 at 06:45
  • @ Scotty Waggoner, check i have updated what i have tried, but it is not aliening properly –  Jul 19 '18 at 07:00

1 Answers1

0

Your code is actually OK, but there is a minor mistake. In your code, you print out $key in every row. I fix this code so that the $key only show at the start of $value display since $key is displayed with rowspan.

EDIT

I change the value of rowspan to make it more dynamic.

<tbody>
    <?php foreach( $viewWorkSheet as $key => $value): ?>
        <?php $row = true; ?>
        <?php foreach( $value as $key1 => $value1): ?>
            <tr>
                <?php if ($row): ?>
                    <td rowspan="<?php echo count($value); ?>"><?php echo $key; ?></td>
                    <?php $row = false; ?>
                <?php endif; ?>
                <td><?php echo $value1['testCase']; ?></td>
                <td><?php echo $key1; ?></td>
                <td><?php echo $value1['devTeamResult']; ?></td>
                <td><small class="label label-success"> <?php echo $value1['testingTeamResult']; ?></small></td>
                <td>OK</td><!-- Static is Ok fine -->
            </tr>
        <?php endforeach; ?>
    <?php endforeach; ?>
</tbody>
david
  • 3,225
  • 9
  • 30
  • 43
  • what do you mean by dynamic? – david Jul 19 '18 at 07:16
  • Might be nice to use the index of the inner loop and check if it equals 0 instead of making a new variable for it. Is that available in `$key1` or is that a custom key in the map? I'm not quite sure how you might get at it in PHP if the keys aren't ids. It's been a while since I've written PHP. Here are some ideas https://stackoverflow.com/questions/1070244/how-to-determine-the-first-and-last-iteration-in-a-foreach-loop – Scotty Waggoner Jul 19 '18 at 07:16
  • `$key1` is not an integer – david Jul 19 '18 at 07:16
  • @david PHP is fun :) Seems like there should be a way to get at the index of an item in a map but I guess it's not that simple... – Scotty Waggoner Jul 19 '18 at 07:27