Page 1 of 1

SimpleXML syntax problem

Posted: Tue Oct 10, 2006 3:46 am
by mikeeeeeeey
Hey guys, lovely rainy morning, perfect for a spot of SimpleXML and PHP.

I'm building an XML/PHP Content Management System at the moment (pretty small scale) and I've found most of the programming surprisingly straight forward.

However, from the home page (which displays all the articles in the CMS by parsing the XML), I've got links to edit the articles, taking you to an editing page.

The editing page works fine and does everything it should, but I'm having trouble getting a variable from the URL to the XML syntax.

Confused? me too! here's how it looks...

Code: Select all

$num = $_GET['edit'];

$xmlDoc = "xmlDocument_Articles.xml";
$article = simplexml_load_file($xmlDoc);

$section = $article->slide[$num];
the last bit, in which the var $section looks to a specific part of the XML for parsing just doesn't seem to want to take a variable in those square brackets. During testing, placing numbers between them i.e.

Code: Select all

$section = $article->slide[5];
works fine, but if I can figure out how to get that little number in that little place, I'd be a lot happier.

Thanks in advance, and hope the weather gets better! :D

Posted: Tue Oct 10, 2006 3:58 am
by volka
try

Code: Select all

<?php
$num = $_GET['edit'];

$xmlDoc = "xmlDocument_Articles.xml";
$article = simplexml_load_file($xmlDoc);

$section = $article->slide[$num];

echo '<pre>';
echo "\nnum:"; var_dump($num);
echo "\nsection: "; var_dump($section);
echo "count(slide): ", count($article->slide);
echo '</pre>';
flush();
?>

Posted: Tue Oct 10, 2006 4:13 am
by mikeeeeeeey
ooh information...

Code: Select all

num:string(1) "7"

section: NULL
count(slide): 8


Fatal error: Call to a member function children() on a non-object in C:\....\editSlide.php on line 50
so if section is NULL, that means there's something wrong with that variable...

it has to be that syntax. it really must hate that

Code: Select all

$section = $presentation->slide[$num];

Posted: Tue Oct 10, 2006 4:15 am
by volka
Fatal error: Call to a member function children() on a non-object in C:\....\editSlide.php on line 50
$presentation is not an object?
-> simplexml_load_file() failed.

Posted: Tue Oct 10, 2006 4:18 am
by mikeeeeeeey
nah, $presentation is an object, the program is just failing because of the syntax for the $section variable.

as I put at the ^^top, if...

Code: Select all

$section = $presentation->slide[5]
everything runs fine, however this isn't dynamic. so trying to take a var from the URL and putting...

Code: Select all

$section = $presentation->slide[$someVariable]
gives the fatal error above.

it really is as simple as the syntax used around that point.

Posted: Tue Oct 10, 2006 4:20 am
by volka

Code: Select all

<?php
$num = (int)$_GET['edit'];

$xmlDoc = "xmlDocument_Articles.xml";
$article = simplexml_load_file($xmlDoc);

$section = $article->slide[$num];

echo '<pre>';
echo "\nnum:"; var_dump($num);
echo "\nsection: "; var_dump($section);
echo "count(slide): ", count($article->slide);
echo '</pre>';
?>

Posted: Tue Oct 10, 2006 4:24 am
by mikeeeeeeey
ahh you beast!

(int) saves the day.

reminds me of a javascript calculator in which parseInt() was heavily used.

volka, thanks a lot man. living upto your status once again :wink:

Posted: Tue Oct 10, 2006 4:39 am
by volka
But it's it's a bit strange the extension does not perform an implicit cast to int/long. There can't be string indicies or am I missing something?

Posted: Tue Oct 10, 2006 5:32 am
by mikeeeeeeey
pfft, too many big words!
but seriously, I didn't understand much of that?