Arkane Posted December 2, 2008 Share Posted December 2, 2008 Hey, I'm trying to sort out a problem I'm having with my site. Currently its all updated manually, and its becomming too much for me. all the info I want on the page is currently kept in an xml file, so I'm hoping to use this to similar ways as SQL. Obviously I could just use SQL, but I can neither afford this, nor do I want to copy all of the data (currently 3000 items+) into another database. the data is all similar to this <list> <games> <game> <number>2</number> <title>Game Title</title> <comment></comment> </game> </games> </list> What i'm looking for is the hopes that I can use some form of loop to get the data from this. The number nodes are all unique, so what I want is to loop with this so that I can display the data from a certain number to 100 above it. Can anyone tell me how to do this? I realise I'm basically asking people to do the code for me, but I'm just going soooo slowly at picking it up that I really need something to learn it from. Thanks very much to any who can help Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/ Share on other sites More sharing options...
Mchl Posted December 2, 2008 Share Posted December 2, 2008 How can you not afford MySQL? It's free... Or if you can't afford MySQL powered hosting, there's always sqlite. For parsing XML files SimpleXML is very good solution. Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704220 Share on other sites More sharing options...
Arkane Posted December 2, 2008 Author Share Posted December 2, 2008 I was meaning I can't afford the MySQL hosting... and I keep breaking it on my computer. I looked at SimpleXML, but I didn't think it will work, because some of the title files contain colons and things. Plus, I am completely new to php, and don't really understand much of it to actually learn without examlpe... although I am trying, especially since I realise how ignorant that comment sounds. Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704228 Share on other sites More sharing options...
Mchl Posted December 2, 2008 Share Posted December 2, 2008 because some of the title files contain colons and things. If the files are not valid XML you will have problems anyway. Take a look at the examples in manual, and give SimpleXML a try. Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704253 Share on other sites More sharing options...
Arkane Posted December 2, 2008 Author Share Posted December 2, 2008 OK. i've had things working with colons already, but are they not actually valid XML? The stuff I was using was really picky about a lot of things, but its worked fine with htose so far. I'm trying the tutorials, http://uk2.php.net/SimpleXML, but I'm not having any luck. On the very first one I get an error on line 5 (from a straight copy/paste job - figured it would be easiest for me to learn through example). Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704327 Share on other sites More sharing options...
Mchl Posted December 2, 2008 Share Posted December 2, 2008 Post your code and error message and we will try to figure it out. Colons should not break XML, but other characters (like & < > etc) could. You can enclose data in CDATA section to avoid that. Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704336 Share on other sites More sharing options...
Arkane Posted December 2, 2008 Author Share Posted December 2, 2008 I did know about the & screwing things up... I found that out the hard way. I'll look into CDATA - cheers man, that will help me with another unrealted (kinda) problem. Parse error: syntax error, unexpected T_SL in E:\domains\a\arkanes-arkade.co.uk\user\htdocs\test\books.php on line 5 the code is literally a straight copy of the tutorial, and I also have the books.xml copied, and in the same folder as 'books.xml' <?php // load SimpleXML $books = new SimpleXMLElement('books.xml', null, true); echo <<<EOF <table> <tr> <th>Title</th> <th>Author</th> <th>Publisher</th> <th>Price at Amazon.com</th> <th>ISBN</th> </tr> EOF; foreach($books as $book) // loop through our books { echo <<<EOF <tr> <td>{$book->title}</td> <td>{$book->author}</td> <td>{$book->publisher}</td> <td>\${$book->amazon_price}</td> <td>{$book['isbn']}</td> </tr> EOF; } echo '</table>'; ?> I also double checked the phpinfo for my site, and I have SimpleXML '$Revision: 1.151.2.22.2.39 $', with Schema support enabled. Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704375 Share on other sites More sharing options...
Mchl Posted December 2, 2008 Share Posted December 2, 2008 I'm not sure if you can echo heredoc... And it seems that <<<EOF is not recognized as start of string (T_SL is 'shift left' or << operator) It works (kind of) when I change it to <<<XML Why not do it this way? <?php // load SimpleXML $books = new SimpleXMLElement('books.xml', null, true); ?> <table> <tr> <th>Title</th> <th>Author</th> <th>Publisher</th> <th>Price at Amazon.com</th> <th>ISBN</th> </tr> <?php foreach($books as $book) // loop through our books { ?> <tr> <td><?php echo $book->title ?></td> <td><?php echo $book->author ?></td> <td><?php echo $book->publisher ?></td> <td>$<?php echo $book->amazon_price ?></td> <td><?php echo $book['isbn'] ?></td> </tr> <?php } echo '</table>'; ?> Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704389 Share on other sites More sharing options...
Arkane Posted December 2, 2008 Author Share Posted December 2, 2008 thanks man, that does it perfectly. Now I'm just looking for a different tutorial to get some more things figured out. Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704398 Share on other sites More sharing options...
Arkane Posted December 3, 2008 Author Share Posted December 3, 2008 OK, heres the deal. Now that I've got that figured out, I have my xml parsing as needed, using $titles = $books->xpath('games/game'); foreach($titles as $title) Like I said though, theres 3000 items in this - I'm intending to use $_GET to give a start number, but can anyone tell me how to do a for of only 100 times, instead of 3000. Is there a way to do this, either by specifying the startnum and number of times to do it, or the start and finish numbers? Cheers Link to comment https://forums.phpfreaks.com/topic/135209-xml-database-to-replace-mysql/#findComment-704479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.