0
<table style="width: 600px" class="slicedTable">
     <tr>
      <th>Spetsialist</th>
      <th>Tunnid</th>
     </tr>


<tr>            
       <?php foreach($specs as $specName => $spec): ?>
        <td><?php echo $specName?>
        <tr>
       <?php endforeach; ?>


<td>
        <?php foreach($tunnid as $tund): ?>
           <td><?php echo $tund?></td>

       </td>
       <?php endforeach; ?>
     </tr>
 </table>

I need this to print out a regular table where it goes like:
name 1 - value1
name2 - value2

Specs:

$specs = array();

            foreach($data as $row) {
                $workerName = $row['Worker']['name'];
                if (!isset($specs[$workerName])) {
                    $specs[$workerName] = array('procs' => array(), 'extras' => array());
                }

However I can't seem to accomplish this, help please.

7
  • Is $spec the "name" and $tund the "value"? Commented Jun 19, 2013 at 7:30
  • 1
    Why do you close the table 3 times? Commented Jun 19, 2013 at 7:31
  • Yes, exactly like that Commented Jun 19, 2013 at 7:32
  • Accidentally left it in, i tried two tables next to eachother from desperation. Commented Jun 19, 2013 at 7:32
  • What does your array look like? Commented Jun 19, 2013 at 7:35

2 Answers 2

1

Here is the code I fix for you:

Assuming the number of elements in $spec is same as or more than the number of elements in $tunnid. In coder's perspective, count($spec) >= count($tunnid).

<table style="width: 600px" class="slicedTable">
   <tr>
      <th>Spetsialist</th>
      <th>Tunnid</th>
   </tr>
   <?php 
      $i = 0;
      foreach($specs as $specName => $spec): 
    ?>
   <tr>            
       <td><?php echo $specName; ?></td>
       <td><?php echo isset($tunnid[$i]) ? $tunnid[$i] : '-'; ?></td>
    </tr>
    <?php 
         $i++;
      endforeach; 
    ?>
 </table>

Not really an elegant solution, I admit.

Sign up to request clarification or add additional context in comments.

4 Comments

but what if it's not? right now the table looks fine but the right values are next to eachother only when the number is the same.
If the number of elements in $tunnid is greater than number of elements in $specs, the remaining elements in $tunnid will not be shown.
okay, then there must be something wrong with my query, i'm not getting the right values next to the right names..
I updated the answer, now includes if count($spec) >= count($tunnid).
0

UPDATE:
Seems like your arrays might have different lengths, so I updated the code below to handle such a situation (it doesn't matter which array is longer).


You could do it like this:

<table style="width: 600px" class="slicedTable">
    <tr>
        <th>Spetsialist</th>
        <th>Tunnid</th>
    </tr>
<?php $len1 = count($specs); $len2 = count($tunnid);
      $specsKeys = array_keys($specs);
      for ($i = 0; $i < max($len1, $len2); $i++) {
          $name  = ($i < $len1) ? $specsKeys[$i] : ""; 
          $value = ($i < $len2) ? $tunnid[$i] : ""; ?>
    <tr>
        <td><?php echo($name); ?></td>
        <td><?php echo($value); ?></td>
    </tr>
<?php } ?>
</table>

See, also, this short demo (*updated*).

2 Comments

If you take a look at the demo provided, you can see there is no error. The only reason I can think of to raise an error in your code would be if $specs and $tunnid don't have the same length or if you are using a variable named $i elsewhere in your code. In any case without the exact error (and code) I can't figure out what the problem in your code is.
@user2485439: Taking into account your comments in another answer it seems like your arrays might have different lengths. I know you have probably solved your problem by now, but in any case I updated my answer to be able to handle arrays of different lengths.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.