1

I have XML file. It's movie showtimes, and i try parse.

<?xml version="1.0" encoding="UTF-8"?> <billboard>
    <genres>
<shows>
<show id="160576" film_id="5710" cinema_id="89" hall_id="241">
                <begin>2012-11-15</begin>
                <end>2012-11-18</end>
                <times>
                                            <time time="12:30:00">
                            <prices>30, 55</prices>
                            <note><![CDATA[]]></note>
                        </time>
                                            <time time="14:45:00">
                            <prices>30, 55</prices>
                            <note><![CDATA[]]></note>
                        </time>
                                            <time time="17:00:00">
                            <prices>30, 55</prices>
                            <note><![CDATA[]]></note>
                        </time>
                                            <time time="23:45:00">
                            <prices>30, 55</prices>
                            <note><![CDATA[]]></note>
                        </time>
                                    </times>
            </show>

I parse my XML and echo content.But I can not echo "time" and "price".After insertion code "//echo time and price//" script doesn't work (white screen).Please help. P.S. Sorry for my english.

    $xmlstr = file_get_contents('test.xml');
$x = new SimpleXMLElement($xmlstr); 
$id_f = 89;
$cinema_id = "//show[@cinema_id=".$id_f."]";

$cinema=$x->xpath($cinema_id);
///////////////////////start/////////////////////////////////////////////
 foreach($cinema as $cinema)
      {
///////////string///////////////////
$begin_m = $cinema[0]->begin;
$end_m = $cinema[0]->end;
$film_id_m = $cinema[0]['film_id'];


/////////echo//////////////////////
echo "<b>Begin: </b>".$begin_m."<br>"; 
echo "<b>End: </b>".$end_m."<br>"; 
echo "<b>ID film: </b>".$film_id_m."<br>"; 
/////////echo time and price///

    foreach($cinema[0]->times->time as $k){
    $obj=current($k);
    $time_b = $obj['time']."\n";

    echo "Time: "$time_b."<br>";
    foreach($cinema[0]->times as $price){

    echo "Price: "$price[0]->pices."<br>";



}
/////////////   

    echo "<hr>";  
}

}

1 Answer 1

1

There are syntax errors in your code, causing a 500 Internal Server error (which shows the white screen).

Syntax error 1:

echo "Time: ".$time_b."<br>";
          // ^ you were missing this

Syntax error 2:

 echo "Price: ".$price[0]->pices."<br>";
            // ^ you were missing this

Also, in your foreach loop, you assign the element the same variable name as the array that you're looping. This won't cause an error but it will mean that your $cinema array is dead after the loop, it will be set to the value of the last element. Give it a different name:

foreach($cinema as $cinemaItem)

With those fixed, your code will output as you expect. In future you should check your error log or turn errors on because they will tell you where the syntax errors are:

error_reporting(E_ALL);
ini_set('display_errors', '1');
Sign up to request clarification or add additional context in comments.

Comments

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.