Listing items from XML File

Hello,
Today I ahve restarted working (for the time ebing anyway) on some work with XML which I am having difficulty with. I ahve the following PHP page:

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

require_once('db.php');

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

$sql = "SELECT id FROM questions WHERE userid = '$user_id' AND type = 'text'";

$result = mysql_query($sql);

while($questionids = mysql_fetch_array($result)){
//----------------------------------------------------------
//use simplexml to find all questions with the ids from xml file
$new_result = array();
 
$xml = simplexml_load_file('xml/text_questions.xml');
 
foreach ($xml->question_list as $question)
{
    if (in_array((integer) $question->id, $result))
    {
        $result[] = $question->name;
    }
}
//----------------------------------------------------------------
}

$xml = simplexml_load_file('xml/text_questions.xml'); 

foreach($xml as $question){
    echo 'QID #'.$question->id.': '.$question->text.'<hr />';

}
?>

and as you can see with the sql queiry above, it is meant to find the id’s of questions which have a user id equal to the id of the user currently signed in, ie, from the session variable. It is then meant to search through an XML file to find all the questions where the question id was returned from the sql query.

For some reason, the above script, is just displaying all questions from the XML file instead of only those for that specific user.

The XML file is structured as follows:

<?xml version="1.0"?>
<question_list>
	<question>
		<id>
			1
		</id>
		<text>
			What is a Cat?
		</text>
	</question>
</question_list>

Please can someone help me figure out what is going wrong here?

Look at what the last section of code, after the loop, is doing.

Also be careful about the variables you’re using inside that loop :wink:

Morning Jake!, You really are a great help on here.

I see that I have set it to load the XML file twice so ahve merged it toegther:

while($questionids = mysql_fetch_array($result)){
//----------------------------------------------------------
//use simplexml to find all questions with the ids from xml file
$new_result = array();
 
$xml = simplexml_load_file('xml/text_questions.xml');
 
foreach ($xml->question_list as $question)
{
    if (in_array((integer) $question->id, $result))
    {
        $result[] = $question->name;
		echo 'QID #'.$question->id.': '.$question->text.'<hr />';
    }
}
//----------------------------------------------------------------
}

?>

It’s now giving me no questions outputted at all. I am a bit confused as to how the simpleXML lines are working. Would it be possible for you to explain what the code is doing in english first and then I can try and figure the last bit out?

I’m sure you could find that out yourself, with a bit of use of the PHP Manual :stuck_out_tongue:

[hint]The $result variable holds the mysql query, not an array.[/hint]

Right,

Just tried altering it and came up with this:

require_once('db.php');

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

$sql = "SELECT id FROM questions WHERE userid = '$user_id' AND type = 'text'";

$result = mysql_query($sql);

while($questionids = mysql_fetch_array($result)){
//----------------------------------------------------------
//use simplexml to find all questions with the ids from xml file
$new_result = array();

while($row=mysql_fetch_assoc($result))
{ 
$new_result[]=$row;
}
 
$xml = simplexml_load_file('xml/text_questions.xml');

foreach ($xml->question_list as $question)
{
    if (in_array($question->id, $new_result))
    {
		echo 'QID #'.$question->id.': '.$question->text.'<hr />';
    }
}
//----------------------------------------------------------------
}

?>

It still doesn;t show any questions though. I tried looking at the manual for simpleXML but it just does not seem SIMPLE for me. The code I ahev just does not seem to relate to what in the manual in any way. And I don;t quite understand what lines such as this:

$xml->question_list as $question

are doing as question is a child element of question_list so I would ahve expected it would be written like:

$xml->question_list->question as $question

?

Latest code:

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

require_once('db.php');

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

$sql = "SELECT id FROM questions WHERE userid = '$user_id' AND type = 'text'";

$result = mysql_query($sql);

$xml = simplexml_load_file('xml/text_questions.xml');

$questionids = array();
while($row = mysql_fetch_assoc($result)){
    $questionids[] = $result['id'];
}
$questions = $xml->question;

var_dump($questions);
echo '<br /><br />';
var_dump($questionids);
echo '<br /><br />';
var_dump($xml);
	
foreach ($xml->question_list as $question){
	if (in_array($question->id, $questionids)){
		printf('QID #%s: %s<hr />', $question->id, $question->text);
	}
}

?>

Output:

My Questions
object(SimpleXMLElement)#2 (2) { ["id"]=> string(8) " 1 " ["text"]=> string(21) " What is a Cat? " } 

array(1) { [0]=> NULL } 

object(SimpleXMLElement)#1 (1) { ["question"]=> array(18) { [0]=> object(SimpleXMLElement)#4 (2) { ["id"]=> string(8) " 1 " ["text"]=> string(21) " What is a Cat? " } [1]=> object(SimpleXMLElement)#5 (2) { ["id"]=> string(8) " 2 " ["text"]=> string(25) " What is your name? " } [2]=> object(SimpleXMLElement)#6 (2) { ["id"]=> string(8) " 3 " ["text"]=> string(25) " Where are you now? " } [3]=> object(SimpleXMLElement)#7 (2) { ["id"]=> string(8) " 5 " ["text"]=> string(34) " this text should be entered " } [4]=> object(SimpleXMLElement)#8 (2) { ["id"]=> string(8) " 6 " ["text"]=> string(31) " this is another question " } [5]=> object(SimpleXMLElement)#9 (2) { ["id"]=> string(1) "6" ["text"]=> string(14) "$question_text" } [6]=> object(SimpleXMLElement)#10 (2) { ["id"]=> string(1) "6" ["text"]=> string(13) "Where are we?" } [7]=> object(SimpleXMLElement)#11 (2) { ["id"]=> string(1) "9" ["text"]=> string(20) "Where is Manchester?" } [8]=> object(SimpleXMLElement)#12 (2) { ["id"]=> string(2) "10" ["text"]=> string(12) "who is sara?" } [9]=> object(SimpleXMLElement)#13 (2) { ["id"]=> string(2) "11" ["text"]=> string(6) "tester" } [10]=> object(SimpleXMLElement)#14 (2) { ["id"]=> string(2) "12" ["text"]=> string(6) "tester" } [11]=> object(SimpleXMLElement)#15 (2) { ["id"]=> string(2) "13" ["text"]=> object(SimpleXMLElement)#22 (0) { } } [12]=> object(SimpleXMLElement)#16 (2) { ["id"]=> string(2) "14" ["text"]=> string(21) "what is alisons name?" } [13]=> object(SimpleXMLElement)#17 (2) { ["id"]=> string(2) "15" ["text"]=> string(15) "Where is Imran?" } [14]=> object(SimpleXMLElement)#18 (2) { ["id"]=> string(2) "16" ["text"]=> string(17) "where is beijing?" } [15]=> object(SimpleXMLElement)#19 (2) { ["id"]=> string(2) "17" ["text"]=> string(8) "22222222" } [16]=> object(SimpleXMLElement)#20 (2) { ["id"]=> string(2) "18" ["text"]=> string(19) "where is neil" } [17]=> object(SimpleXMLElement)#21 (2) { ["id"]=> string(2) "19" ["text"]=> string(31) "What type of Object is Emerald?" } } }

XML:

<question_list>
<question>
<id>
1 
</id>
<text>
What is a Cat? 
</text>
</question>
<question>
<id>
2 
</id>
<text>
What is your name? 
</text>
</question>
</question_list>

The thing that strikes me here is that you’ve got:

$questions = $xml->question; 
var_dump($questions);

which outputs:

object(SimpleXMLElement)#2 (2) { ... } 

Stating that $xml->question is a SimpleXMLElement.

However, when you var_dump($xml):

object(SimpleXMLElement)#1 (1) { ["question"]=> array(18) { ... } ... } 

Stating that $xml->question is an array.

Are you sure that this is the current uploaded version?

Positive that that is the latest version

I don’t know if this is the problem - in fact I doubt it is - but your XML declaration is missing.

Put this at the top of your XML file:

<?xml version="1.0" encoding="UTF-8" ?>

It did already have that, just that Opera doesn’t show that when viewing an XML document

Ok then.

Soooo, forgetting that dodgy error (or perhaps a slip-up on my behalf) and forcing an array:

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

require_once('db.php'); 

echo '<h2>My Questions</h2>'; 
$sql = "SELECT id FROM questions WHERE userid = '$user_id' AND type = 'text'"; 
$result = mysql_query($sql); 
$xml = simplexml_load_file('xml/text_questions.xml'); 

$questionids = array(); 

while($row = mysql_fetch_assoc($result)){ 
    $questionids[] = $row['id']; 
}

echo '<h3>MySQL Question IDs: </h3>';
    echo '<ul>';
    foreach($questionids as $qid){
        echo "<li>{$qid}</li>";
    }
    echo '</ul>';
echo '<h3>XML Question IDs:</h3>';
    echo '<ul>';
    foreach ($xml->question as $question){ 
        echo "<li>{$question->id} - ";
        if (in_array($question->id, $questionids)){ 
            //echo "QID #{$question->id}: {$question->text}<hr />";
            echo 'Match';
        }else{
            echo 'No match';
        }
        echo '</li>';
    }
    echo '</ul>';
?>

What is the output?

Output:

My Questions
MySQL Question IDs:
19
XML Question IDs:
1 - No match
2 - No match
3 - No match
5 - No match
6 - No match
6 - No match
6 - No match
9 - No match
10 - No match
11 - No match
12 - No match
13 - No match
14 - No match
15 - No match
16 - No match
17 - No match
18 - No match
19 - Match

In which case, it is working.

sooo, remove the debug code and uncomment the original output 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' AND type = 'text'"; 
$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 "QID #{$question->id}: {$question->text}<hr />";
    }
}
?>

There should be 1 question, #19, output for that user.

Excellent, Thanks, the question does show up. I hvae no idea why it didn;t work before. Thanks

is thinking someone should check $user_id. :stuck_out_tongue:

Off Topic:

Doh! That’ll teach me for reading the thread, composing a post then buggering off to make a coffee before submitting!

huh?