Chris,
So far this is what I have.. Its almost there! Here is the code that I am using.. I had to change what you had up there a little tiny bit, I appreciate you introducing me to new functions that I have not yet used like array_map! Way cool.
<?
$file = "technotes.inc";
$fp = fopen($file,"r");
//We'll need this function soon:
function explode_records($string)
{ //String contains a single record. Separate it into an array of fields.
$fields = explode('<eof>',$string);
foreach($fields as $field)
{ eregi('^(.+): (.+)$', $field, $matches);
echo "match 1: " . $matches[1] . " Match 2: " . $matches[2] . "<br>";
$array[$matches[1]]=$matches[2];
}
return $array;
}
//Inhale the entire file into a big string,
$string = fread($fp, filesize($file));
fclose($fp); // Funny, I don't remember opening it :-)
//We'll kill off newlines that would only cause trouble later
$string = str_replace("\n",'',$string);
$string = str_replace("$",'',$string);
//explode it into an array on "<eor>"
$array = explode('<eor>',$string);
//You now have an array of records.
//Use the array_map function:
$array = array_map('explode_records',$array);
print_r(array_values($array));
//I guess you'll want an SQL INSERT query:
//First, the field names (assuming that there are already columns with suitable names in mytable):
$keys=join(',',array_keys($array));
//And their values
$values=join(',',array_values($array));
echo $sql="INSERT INTO mytable ($keys)VALUES($values)";
?>
The only problem with this script right now is that it does not print out the correct SQL statement. The SQL statement it does print is as follows.
INSERT INTO mytable (0,1,2,3)VALUES(Array,Array,Array,Array)
My test file has 4 arrays, my question is how to go about inserting the keys where the keys should go, and the values where the values should go for each array. I am having a bit of trouble with it.
Thanks again for your help!!
Cheers,
John