...

View Full Version : PHP4, XML and CSV



ttttt
11-12-2006, 03:42 PM
Hi,
I'm looking to use XML files instead of MySQL, but I'm running php4.
I have been searching google all afternoon, and can't find anything that helps. I was wondering if anyone has a decent tutorial on ?SAX? or something else. I do NOT however want to install anything from PEAR and I won't be persuaded to use MySQL.
If what I ask of is not possible, can I use CSV?
Thanks,

vinyl-junkie
11-12-2006, 07:04 PM
What kind of problem are you trying to solve, or what specific kind of application are you trying to write? It would help if we had a littlel more information on what you're trying to do.

ttttt
11-12-2006, 08:24 PM
Well, I'm trying to use this database to power my website which will act along the lines of a content management system. I would like to use XML instead of MySQL because of various reasons and I have no idea of how to use php4 and xml relationships.
Thanks for the reply,

firepages
11-13-2006, 01:36 AM
XML files are a poor substitute for a database because of the overhead of parsing them (PERL is much faster at munging text files), if the site is only small then perhaps its workable , php4 still has built in expat xml functions (http://www.php.net/xml) allowing you to write your own parsers, and if you search this forum there is a reference somewhere to simpleXML classes for php4

If I had to use a flatfile database I would use csv and/or serialized PHP arrays, SQLite would be worth a look but I think its an extesion in PHP4?

ttttt
11-15-2006, 04:20 PM
So how would I use csv and serialized arrays?

vinyl-junkie
11-15-2006, 04:49 PM
Here's an example of code that reads a CSV file and extracts each item into the appropriate variable for processing.


$handle = fopen("file.csv", 'r');
while(($data = fgetcsv($handle, 1000, ",")) !== false)
{
list($col1, $col2, ...) = $data;
}
fclose($handle);

ttttt
11-15-2006, 08:16 PM
Thanks for that!
And how exactly would I write the csv file? I've never used csv before, except for transferring email contacts.

vinyl-junkie
11-15-2006, 10:06 PM
$handle = fopen($filename, "w");
fwrite($handle, $csv_code);
fclose($handle);
where $filename = whatever file you want to write the data to

$csv_code = PHP code to write out comma separated values data

ttttt
11-17-2006, 04:22 PM
$handle = fopen($filename, "w");
fwrite($handle, $csv_code);
fclose($handle);
where $filename = whatever file you want to write the data to

$csv_code = PHP code to write out comma separated values data
I think I must not have been clear enough.
I meant by hand not using php.
Could you please explain how csv works, as in are there columns? etc.
Thanks,

GJay
11-17-2006, 04:30 PM
csv is just items of information separated by commas, for example:


fred,smith,fred.smith@hotmail.com,0181 993 4533,31/07/52,true,false


sometimes the first row in the file will contain identifiers for the columns. Fields that contain commas themselves are generally enclosed in "s, sometimes all data (or all text data) will be in "s regardless of its content (for consistency I guess).

ttttt
11-17-2006, 06:29 PM
Ok, thanks.
But with mysql, you can echo certain parts of each entry, by column. e.g.:


<?php
echo $data['id'];
?>
Is that possible with csv?
Thanks,

vinyl-junkie
11-18-2006, 01:38 AM
Refer to post #6 in this thread. The values you would echo out from that code are $col1, $col2, etc.

firepages
11-18-2006, 01:41 AM
the first line of a csv file usually contains the descriptions so when you load the below data with file() or fgetcsv() etc then $line[0] contains the descriptors

..........data.csv.........................................
0 | id,name,colour /*descriptors not data*/
1 | 1,apple,green
2 | 1,banana,yellow
..etc

from that file you could use $line[0] to give you a reference point
$d=array('id'=>0,'name'=>1,'colour'=>2);//created from line[0]

then $line[2][$d['color']];// would equal yellow

you could abstract that further and build functions to convert your array into a mysql-like associative assay or object but the point here is that it's just a csv file, not a relational database so you are going to have to do a lot of work with your data if you want to use it just like MySQL.

...and this is the simple stuff ;) when it gets to searching the data or trying to run queries against it then you will really hit the hard stuff, you can do it by creating separate index csv's etc but these have to be rebuilt constantly as your data changes and the text files get big.

Can I ask why you don't want to use a DataBase ? is it simply not available with your hosting ?

chump2877
11-18-2006, 03:34 AM
If you are going to be searching data, XML seems like a better alternative (to CSV) as it is already separated into nodes. But XML (and CSV) is slower (usually) for large amounts of data, as has already been said.

ttttt
11-18-2006, 09:23 AM
If you are going to be searching data, XML seems like a better alternative (to CSV) as it is already separated into nodes. But XML (and CSV) is slower (usually) for large amounts of data, as has already been said.
Well, I tried to use xml, but I simply don't know how, and I was discouraged from doing it.
The reason I don't want to use a database is that I've been trying for a very long time to get it working, but whatever I try, just doesn't work, I've posted here about it before, but I've done that quite a lot for simple problems. For example, cf helped me built two seperate pieces of code, but when I put them together with an if statement, they both refused to work.
I would definately consider xml, but I have no idea what popping and pushing things means and don't know where to start.
I don't have simpleXML or anything, just the base ingredients of php4.
Thanks,

vinyl-junkie
11-18-2006, 04:15 PM
Sounds like what you really need to do at this point is take a step back and try to really learn PHP and MySQL before trying to forge ahead with the problem that you're trying to solve in this thread. You need to really understand what you're doing first.

There are many forum threads, such as this one (http://www.codingforums.com/showthread.php?t=96825&highlight=learn+php), which offer up a variety of learning resources. I would suggest you pick whichever resource(s) that work best for you and really dive in. Of course, we're here to help if there's anything you don't understand.

ttttt
11-18-2006, 04:40 PM
Sounds like what you really need to do at this point is take a step back and try to really learn PHP and MySQL before trying to forge ahead with the problem that you're trying to solve in this thread. You need to really understand what you're doing first.

There are many forum threads, such as this one (http://www.codingforums.com/showthread.php?t=96825&highlight=learn+php), which offer up a variety of learning resources. I would suggest you pick whichever resource(s) that work best for you and really dive in. Of course, we're here to help if there's anything you don't understand.
Thanks, but I'm perfectly comfortable with PHP and MySQL, it's just putting them together. I know how to do it etc. but it normally doesn't work or when I feel it doesn't make a lot of sense. I understand, but I find it hard to personalize. I wanted to use an alternative to MySQL for this reason, which is why I started this thread.
I would prefer to use XML if at all possible.
Thanks,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum