Please Help - SimpleXML question

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
Quietpaw
Forum Newbie
Posts: 3
Joined: Fri Nov 18, 2005 2:39 pm
Location: Madison, WI

Please Help - SimpleXML question

Post by Quietpaw »

Hello!

I am trying to load Book objects with information from an XML file. I'm having problems loading my authors() array in my books object with Authors from the XML file if there is more than one <Author></Author> within the <Book> node. I seem to be stupid when it comes to Arrays! I'm having trouble in my foreach loop for Authors. Currently, I cannot even write the author name into my $authors array I created here, it gives me a "Can't use function return value in write context". Now I assume there's a much easier way, like an append or add or put function for Arrays, but I can't seem to find such a function anywhere in the manuals. Is there some way I can just add each author to my $Book->Authors array? Here is the code I am attempting:

Code: Select all

//set up an array to hold multiple Book objects
$Books = array();
$i = 0;
//go through each Book attributes listing and assign each node to Book object variables
foreach($xml->Items->Item->ItemAttributes as $Items) {
	$Books[$i] = new Book();
	$Books[$i]->ISBN = $Items->ISBN;
	$Books[$i]->title = $Items->Title;
	$Books[$i]->publisher = $Items->Publisher;
	$Books[$i]->datePub = $Items->PublicationDate;
	$Books[$i]->listPrice = $Items->ListPrice->FormattedPrice;
	$k = 0;
	$authors = array();
	foreach($Items->Author as $name => $node) {
		$authors($k) = ($node);
		$k++;
	}
	$Books[$i]->authors = $authors;
	$i++;
	}
Thanks for your help!!

HawleyJR:Please use tags when Posting PHP Code In The Forums
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Try using print_r to help problem solve. is $xml a class?
Quietpaw
Forum Newbie
Posts: 3
Joined: Fri Nov 18, 2005 2:39 pm
Location: Madison, WI

re:

Post by Quietpaw »

$xml is a variable that I think gets declared when it's called. It's part of the basic startup that SimpleXML suggests doing in all the code snippets. I believe it represents the context node of the XML document.

I've been using print_r to make sure the books are loading, and they are. The XML to PHP stuff is working. The problem is that I can't take repeating nodes in the same book and load them into arrays. Books only have one ISBN and Publisher and Date, but they have many Authors. I can read each author, but I can't load that array $authors I created, nor the $Books[$i]->Authors array inside my Object. I just don't know how! Isn't there a function I can call with the arrays?
Quietpaw
Forum Newbie
Posts: 3
Joined: Fri Nov 18, 2005 2:39 pm
Location: Madison, WI

Oh dear, I got it!

Post by Quietpaw »

*blush*

I'm sorry, I figured it out. It was just a typing error..... I was typing $authors(0) for my array instead of $authors[0]. Square brackets vs VB.Net () brackets. Ugh! I never should have taken these two classes at the same time. :-)

Thank you hawleyjr for responding to me so quickly!!
Post Reply