Jump to content

XML Database to replace MySQL


Arkane

Recommended Posts

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

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.

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).

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.

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>';  
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.