1

I want to format what is output by the following php script:

 <?php

$stop = $_POST["stop_number"];  // stop_number is an text input value provided by user
$depart_url = "http://64.28.34.43/hiwire?.a=iNextBusResults&StopId=" . $stop;
$html = file_get_contents($depart_url);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$my_xpath_query = "//td[@valign='top']";
$result = $xpath->query($my_xpath_query);

foreach($result as $result_object)
{
    echo $result_object->childNodes->item(0)->nodeValue,'<br>';
}

?>

Here is the output (at least in one instance, as the data changes over time).

18 - GOLD
OUTBOUND
8:17p
8:16p
8 - GREEN 
OUTBOUND
8:46p
8:46p
8 - GREEN 
OUTBOUND
18 - GOLD
OUTBOUND
5 - PLUM
OUTBOUND

EDIT:

I want the output info above to go in a table such as the one below. However instead of the text between tags, it would be variables, or items from the php script output.

<!DOCTYPE html>
<html>
<title>Departure Table</title>

<body>
<h4>Next Departures for Stop Number: __ </h4>
<table border="1px solid black">
    <tr>
        <th>Route</th>
        <th>Direction</th>
        <th>Scheduled</th>
        <th>Estimated</th>
    </tr>
    <tr>
        <td>18 - Gold</td>
        <td>Outbound</td>
        <td>8:17p</td>
        <td>8:16p</td>
    </tr>
    <tr>
        <td>8 - Green</td>
        <td>Outbound</td>
        <td>8:46p</td>
        <td>8:46p</td>
    </tr>
</table>
</body>
</html>
5
  • HTML or plain text output? Commented Jul 7, 2016 at 0:41
  • Sorry, Phil, I don't really know what you mean. I would like to be able to organize the output info into variables so that I can display them in a table on an html page. Commented Jul 7, 2016 at 0:59
  • Sounds like you want HTML then. Try echo $result_object->childNodes->item(0)->nodeValue, '<br>';. You should show the desired output format (table) in your question if that's what you actually want Commented Jul 7, 2016 at 1:02
  • You're going to need a better XPATH query than //td in order to group the results. Are they all grouped by specific <tr> elements? Commented Jul 7, 2016 at 1:30
  • I recognize this question might be becoming too vague...I guess what I'm looking for is how to assign the items in the output list to variables. That is my main problem here. (transferring these values to a html from php script is something I can figure out on my own). Commented Jul 7, 2016 at 1:33

1 Answer 1

1

Try appending a \n tag after your echo statement:

    echo $result_object->childNodes->item(0)->nodeValue."\n";

EDIT:

If you want to store your data in PHP variables, you could do something like this:

Store data in an array like variable (or any other data structure as per your needs) and iterate over the variable.

$store_data_in_array_variable = array();

foreach($result as $result_object)
{
    $store_data_in_array_variable[] = $result_object->childNodes->item(0)->nodeValue;

}

//iterate over all stored values

foreach ($store_data_in_array_variable as $key => $value) 
{
    echo $key;
    echo '<br>';
    echo $value;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This outputs the following:18 - GOLD OUTBOUND 8:17p 8:16p 8 - GREEN OUTBOUND 8:46p 8:46p 18 - GOLD OUTBOUND 8 - GREEN OUTBOUND 5 - PLUM OUTBOUND. So now there is spacing, but how do I manipulate the items for further formatting?

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.