Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

return html from a php function

Hi,

How should I return this html block? It's been awhile since I did any raw php

<?php 

$jobs = array
  (
  array('id'=>'LW12345','MK'=>1,'jobTitle'=>'C4BH35','jobStartDate' => date("d/m/Y"),'qty'=>5000,'jobDuration'=>'3:00'),
  array('id'=>'LW15783','MK'=>1,'jobTitle'=>'C4BH75','jobStartDate' => date("d/m/Y",strtotime("+ 1 day")),'qty'=>12000,'jobDuration'=>'3:00'),
  array('id'=>'DG12345','MK'=>1,'jobTitle'=>'C5BH50','jobStartDate' => date("d/m/Y",strtotime("+ 2 day")),'qty'=>6000,'jobDuration'=>'3:00'),
  array('id'=>'LK17543','MK'=>1,'jobTitle'=>'C5BH35','jobStartDate' => date("d/m/Y",strtotime("+ 3 day")),'qty'=>9000,'jobDuration'=>'3:00')
  );

function machineJobs($jobs) {
  foreach ($jobs as $job) {?>
            <tr>
              <td><?php $job['$id'] ?></td>
              <td><?php $job['$jobTitle'] ?></td>
              <td><?php $job['$jobStartDate'] ?></td>
              <td><?php $job['$jobDuration'] ?></td>
              <td><?php $job['$qty'] ?></td>
            </tr>  
  <?php return }
}

Just thought should I be echoing out the html??

1 Answer

Alexandre Babeanu
Alexandre Babeanu
10,947 Points

This should work. Then you can echo it wherever.

<?php 

$jobs = array(
     array('id'=>'LW12345','MK'=>1,'jobTitle'=>'C4BH35','jobStartDate' => date("d/m/Y"),'qty'=>5000,'jobDuration'=>'3:00'),
     array('id'=>'LW15783','MK'=>1,'jobTitle'=>'C4BH75','jobStartDate' => date("d/m/Y",strtotime("+ 1 day")),'qty'=>12000,'jobDuration'=>'3:00'),
     array('id'=>'DG12345','MK'=>1,'jobTitle'=>'C5BH50','jobStartDate' => date("d/m/Y",strtotime("+ 2 day")),'qty'=>6000,'jobDuration'=>'3:00'),
     array('id'=>'LK17543','MK'=>1,'jobTitle'=>'C5BH35','jobStartDate' => date("d/m/Y",strtotime("+ 3 day")),'qty'=>9000,'jobDuration'=>'3:00')
 );

function machineJobs($jobs) {
    $output = "";
    foreach ($jobs as $job) {
            $output .= "<tr>";
            $output .= "<td>".$job['$id']."</td>";
            $output .=  "<td>".$job['$jobTitle']."</td>";
            $output .=  "<td>".$job['$jobStartDate']."</td>";
            $output .=  "<td>".$job['$jobDuration']."</td>";
            $output .=  "<td>".$job['$qty']."</td></tr>"  
   }

   return $output;
}
?>

<?php echo machineJobs($jobs); ?>

Hi thank you for your help, I had to amend your code slightly by taking out the $ sign when calling the key inside the foreach. I also added an if statement for best practice reasons.

function machineJobs($jobs) {
    $output = "";
     foreach ($jobs as $job) {
      if ( isset($job['id']) and isset($job['jobTitle']) and isset($job['jobStartDate']) and isset($job['jobDuration']) and isset($job['qty'])){
               $output .= "<tr>";
              $output .= "<td>".$job['id']."</td>";
              $output .=  "<td>".$job['jobTitle']."</td>";
              $output .=  "<td>".$job['jobStartDate']."</td>";
              $output .=  "<td>".$job['jobDuration']."</td>";
              $output .=  "<td>".$job['qty']."</td> </tr>";  
   }}

   return $output;
}