SimpleXML syntax problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

SimpleXML syntax problem

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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();
?>
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post 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];
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>';
?>
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

pfft, too many big words!
but seriously, I didn't understand much of that?
Post Reply