0

I am currently working on a small project that would convert a work rota from a simple HTML table to a YAML output. The table I am scraping doesn't repeat the date should additional staff members be working the same one, preferring to just display it once. This means that as my PHP script works through the table, for additional staff members on a certain date then no date is set, resulting in an empty value. So far, I have the following:

<?php 

include('simple_html_dom.php');

$html = str_get_html('<table>
                        <tbody>
                        <tr>
                        <td>Day</td>
                        <td>Jack</td>
                        </tr>  
                        <tr>
                        <td></td>
                        <td>Jill</td>
                        </tr> 
                        <tr>
                        <td>Night</td>
                        <td>John</td>
                        </tr>           
                        </tbody>
                        </table>');



foreach($html->find('table') as $element) {

  $td = array();
  foreach( $element->find('tr') as $row) {

     $shift = $row->children(0)->plaintext;
     $staff = $row->children(1)->plaintext;
      echo $shift;
      echo "<br />";
      echo "Staff: " . $staff;
      echo "<br />";
      echo "<br />";

  }

}

exit;
 ?>

This outputs as follows:

Day
Staff: Jack


Staff: Jill

Night
Staff: John

What I'm not sure how to do is have PHP use the same variable set from the previous foreach loop if none exists. That way, I could be able to output as follows:

Day
Staff: Jack

Day
Staff: Jill

Night
Staff: John

Would anyone be able to assist? Thanks!

1
  • 4
    Just check if the variable is empty, and if it is, don't assign it again. That way it will use the value from the previous iteration Commented Oct 22, 2013 at 8:10

2 Answers 2

2
if(!empty($row->children(0)->plaintext)){
   $shift = $row->children(0)->plaintext;
}
...

This will only assign a new value to $shift if the current row is not empty.

Alternatively:

$shift = (empty($row->children(0)->plaintext ? $shift : $row->children(0)->plaintext);
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Many thanks :) Can't believe it was something so minor!
1
foreach ($html->find('table') as $element) {
    $lastShift = $lastStaff = '';
    $td = array();
    foreach ($element->find('tr') as $row) {

        $shift = $row->children(0)->plaintext;
        $staff = $row->children(1)->plaintext;
        if (empty($staff)) {
            $staff = $lastStaff;
        }
        if (empty($shift)) {
            $shift = $lastShift;
        }
        echo $shift;
        echo "<br />";
        echo "Staff: " . $staff;
        echo "<br />";
        echo "<br />";
        $lastStaff = $staff;
        $lastShift = $shift;
    }
}

OR

foreach ($html->find('table') as $element) {
    $shift = $staff = '';
    $td = array();
    foreach ($element->find('tr') as $row) {
        if (!empty($row->children(0)->plaintext)) {
            $shift = $row->children(0)->plaintext;
        }
        if (!empty($row->children(1)->plaintext)) {
            $staff = $row->children(1)->plaintext;
        }
        echo $shift;
        echo "<br />";
        echo "Staff: " . $staff;
        echo "<br />";
        echo "<br />";
    }
}

1 Comment

Thanks! Worked brilliantly. I had to mark subzero's answer as correct as it came through first but tested this and it worked exactly as required, also.

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.