Not printing questions from XML file

Hey,
I am trying to fix this small page that is not ecoing out questions from an XML file. The entire page code is:

<?php 
session_start(); 
$user_id = $_SESSION['user_id']; 
require_once('db.php'); 

echo '<h2>My Questions</h2>'; 

//Load the IDs from the database into a compact array:
$questionids = array(); 
$sql = "SELECT id FROM questions WHERE userid = '$user_id'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
    $questionids[] = $row['id'];
}

//load the XML file and loop - when there's a match, output
$xml = simplexml_load_file('xml/text_questions.xml'); 
foreach ($xml->question as $question){
    if (in_array($question->id, $questionids)){ 
        echo "{$question->text}";
    }
}

?>

The XML is:

<?xml version="1.0" encoding="UTF-8"?>
<question_list>
	<question>
		<id>
			1
		</id>
		<text>
			What is the capital city of Australia?
		</text>
	</question>
</question_list>

I have tried everything I can think of. All variables seem to have their correct values. I have checked everything is correct in the database. Question does exist but is not being echoed out.

I would appreciate a quick reply if possible

Thanks

Regards,
Neil

Try displaying the array content (print_r) and put an echo of $question->id inside the foreach loop.

Out of blind curiousity, why not just store the questions in the database along with the id’s?
My first impulse is to escape the echo for the variable… i dont like using object-references inside strings…
and then echo out a debug message to make sure the script is actually entering that IF.

I altered the echo to a simple text string and that is not being displayed so it seemes like it’s not even getting into that bit of the loop. As it happens, none of the XML parts of other pages are working either for some strange reason.

I would put everything in a database table but I have more to write about by using XML

I ahve var_dumped the £questionids array and it includes just id 1 as it should as only 1 question is stored. So it should find the question in the XML with the id of 1 and echo it out but it is just not.

Add echo statements to your script to follow the flow… and see which loops and ifs it enters, and which not.

It is 100% getting inside the foreach loop as I put an echo statement in there. That echo was only printed out once as I have only 1 question in the XML

The echo inside the if is not being reached

Just added a second question for the same user and the echo inside the foreach has printed out twice and another question id has been added to the $questionids array

Latest Code:

<?php 
session_start(); 
$user_id = $_SESSION['user_id']; 
require_once('db.php'); 

echo '<h2>My Questions</h2>';

//Load the IDs from the database into a compact array:
$questionids = array(); 
$sql = "SELECT id FROM questions WHERE userid = '$user_id'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
    $questionids[] = $row['id'];
}
var_dump($questionids);

//load the XML file and loop - when there's a match, output
$xml = simplexml_load_file('xml/text_questions.xml'); 
foreach ($xml->question as $question){
echo "here?";
    if (in_array($question->id, $questionids)){ 
        echo $question->id." - ".$question->text."<br />";
		echo "stuff";
    }
}

?>

Ok, so it gets in the foreach loop, but it doesn’t get into the if.

Instead of echoing a text, why don’t you echo the value you check in the if?

I just tried echoing $question->id in the foreach and that prints the question ids

I also tried echoing the question->text inside the foreach and that also works.

It’s the if where it’s failing but I can’t see any errors with it?

Translation: if (in_array($question->id, $questionids)){ is returning FALSE.

Replace “here?”; with $question->id." #<br>"; , make sure that the question ID is being loaded correctly from the XML.

lol… posts going on while i was posting :stuck_out_tongue:

try array_search instead? Maybe it’s a bug.

$question->id will be an object (of type SimpleXMLElement) and your $questionids array isn’t an array of similar objects. That’s why in_array returns false (because there is no matching object in the array); you’ll need to cast the object to a string or integer (whichever you prefer) like font=monospace $question->id[/font]


if (in_array( (int) $question->id, $questionids)){ 

Excellent, Thanks so much. I would ahve never thought of that as I am still learning PHP.

Thanks!

That will help tremendously

Nice one, salathe :cool:

I have tested like and this works

$xml = simplexml_load_file(‘text_question.xml’);

foreach ($xml->question as $question){

//if (in_array($question-&gt;id,$question)){ 

      echo "{$question-&gt;text}";

//}

}

Just a suggestion,
Can you run that xml file in your web browser… is itwell formed?..
last year it happened to me that culprit was some unescaped charaters in the text field(question in your case) which was casing xml to break and php wont echo it…
if it is the case xml escape will help you
for eg i guess you can’t have ? sign in your question unless escaped…