What am I doing wrong here. I want the array we get at first to be transposed. Then the regex breakups one of the columns so we can query another table in the database to replace that column header with the correct header. Is the Regex correct? Is the transpose correct?
This will get the query we want
exportMysqlToCsv($tablename,$tokenmain, $id);
function exportMysqlToCsv($tablename,$tokenmain, $id, $filename = 'Results.csv'){
$sql_query = "select * from $tablename where $tokenmain";
// Gets the data from the database
$result = mysql_query($sql_query);
$f = fopen('php://temp', 'wt');
$first = true;
$temp = mysql_fetch_array($result);
I hope this transposes the array we just got . Not sure here
array_unshift($temp, null);
$transposed_array = call_user_func_array('array_map', $temp);
Then we loop through transposed-array an replace the column header we receive (ex. 111X2222X3333) and split these up to query the headers we want to replace them with and output it to the excel file. This is where I am very confused.
for ($i = 0; $i < $count; $i++) {
if ($transposed_array[$i][0])//starts with a number
{
$sid = regexp /^[0-9]*/
$gid = regexp /X[0-9]*X/
//remove first and last character from $gid
$gid = str_replace("X", "", $gid)
$qid = regexp /[0-9]*$/
$question_text = mysql_query("select question from lime_questions where sid = ".$sid." and gid = ".$gid." and qid = ".$qid." limit 1");
$transposed_array[$i][5] = $question_test
}
This should output the array to the csv file
while ($row = mysql_fetch_assoc($result))
{
if ($first)
{
fputcsv($f, array_keys($row));
$first = false;
}
fputcsv($f, $row);
} // end while
//resume normal processing
$size = ftell($f);
rewind($f);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: $size");
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
header("Content-type: text/csv");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
fpassthru($f);
exit;
Not sure what you want to achieve because the folloing are not valid PHP expressions
$sid = regexp /^[0-9]*/
$gid = regexp /X[0-9]*X/
$qid = regexp /[0-9]*$/
But form your comments
I want this loop to split up the column headers we recieve from the first array. An example of a column header is 111X222X3333. I want to split them up using regex so each one is its own attribute like sid =111 gid=222 and qid=3333
All you need is
$string = "111X222X3333" ;
list($sid,$gid,$qid) = explode("X", $string);