i'm outputting some info from a mysql query into a text file using fputs(). right now my code looks like this:
$return = "\n";
while($arr = mysql_fetch_array($result)){
fputs($fp, "\"".$arr["name"]."\",");
fputs($fp, "\"".$arr["address1"]."\",");
fputs($fp, "\"".$arr["address2"]."\",");
fputs($fp, "\"".$arr["city"]."\",");
fputs($fp, "\"".$arr["state"]."\",");
fputs($fp, "\"".$arr["zip"]."\"");
fputs($fp, $return);
}
ideally, though, i'd like to accomplish this with a function call as follows:
function addString($string){
fputs($fp, "\"".$arr["$string"]."\",");
}
while($arr = mysql_fetch_array($result)){
foreach ($arr as $key => $value){
addString($value);
}
fputs($fp, $return);
}
of course, it's not working at all. i thought maybe it was a scope problem, so i tried declaring $fp like so:
global $fp;
$fp = fopen($filename, "w");
but i still get errors.
two questions, then:
1) how should i structure this function?
2) is foreach the correct way to step through this array, or is there a better/smarter/faster method?
thanks for your help.