biggieuk Posted March 15, 2009 Share Posted March 15, 2009 Hi all, Im having a bit of trouble finding a solution as to how i can export a list of results to a .csv file. These results are returned from the database using the following two sql statements: // To return a list of users that have the $groupid value in one of thier selections, selections are stored like (12,16,22,55) "SELECT users.username FROM users WHERE users.selections REGEXP '[[:<:]]".$groupid."[[:>:]]'" //At the top of the excel file there should be the name of the session, time & venue using this sql "SELECT sessions.group,title,room,time FROM sessions WHERE groupid = ".$groupid" Any advice would be great. I have tried editing an existing csv file but can only get it to return the list of users. <?php require_once('../Connections/hwbltaconf.php'); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=SessionUsers.csv"); header("Pragma: no-cache"); header("Expires: 0"); mysql_select_db($database_hwbltaconf, $hwbltaconf); $query = "SELECT users.username FROM users WHERE users.selections REGEXP '[[:<:]]11[[:>:]]'"; $exp = mysql_query($query); $count = mysql_num_fields($exp); for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($exp, $i).","; } while($row = mysql_fetch_row($exp)) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . ","; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r", "", $data); if ($data == "") { $data = "\n(0) Records Found!\n"; } print "$header\n$data"; exit; ?> Thanks Quote Link to comment Share on other sites More sharing options...
biggieuk Posted March 15, 2009 Author Share Posted March 15, 2009 Update: Looking to get this like the attached example.csv [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.