jber Posted July 21, 2007 Share Posted July 21, 2007 Well , a final question. I´ve implemented a server in which people can log and fill surveys. As an external admin i want to save the result of the query (the query consists in viewing some fields of the table that people fills) into a file so i can send it via mail to the person i want. I´ve got the query results in a php page, and presented via table (using html) so i´d like to save this into an html page echo "<table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">"; for ($i = 0; $i < mysql_num_fields($result); $i++) { print "<th>".mysql_field_name($result, $i)."</th>\n"; } while ($registro = mysql_fetch_row($result)) { echo "<tr>"; foreach($registro as $clave) { echo "<td bgcolor=\"#BBBBBB\"style=\"border:2px groove black\" align=\"center\">",$clave,"</td>"; } } echo "</tr></table>"; Can you help me? I´ve implemented the save as option in javascript, but i want my application to save the page automatically without asking anything but filename (and this is even optional for me) Thanks in advance Quote Link to comment Share on other sites More sharing options...
jber Posted July 21, 2007 Author Share Posted July 21, 2007 Another option that works partially is $query2 = " SELECT " .$select_cols . " FROM " .$_SESSION["nomtab"]. " INTO OUTFILE 'textfilename.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '--' LINES TERMINATED BY '\n' "; It does write the results in the file, but i want, to improve the application, to display row by row and if possible started by field´s name I attach the code for this page so you can view it better . What is not in english are variable names Thanks in advance [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
socratesone Posted July 21, 2007 Share Posted July 21, 2007 Try this: $pageData = "<table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">"; for ($i = 0; $i < mysql_num_fields($result); $i++) { $pageData .= "<th>".mysql_field_name($result, $i)."</th>\n"; } while ($registro = mysql_fetch_row($result)) { $pageData .= "<tr>"; foreach($registro as $clave) { $pageData .= "<td bgcolor=\"#BBBBBB\"style=\"border:2px groove black\" align=\"center\">",$clave,"</td>"; } } $pageData .= "</tr></table>"; $fileName = "pagedata.txt"; $location = "output/path"; // relative path name $fullPath = $location . "/" . $fileName; // Check to see if the directory exist. If not, create it. if(!is_dir($location)) { mkdir($location); // default permission "0777" (all permissions, all users, which is probably what you want) } // Open up the file. (If the file exists, this will append the data to the file) // You can prevent this by adding a unique identifier to $fileName, such as the data and time, ie: // $fileName = "Output" . date("Y_M_j_H_s"); if (!$handle = fopen( $fullPath, "a")) { echo "Could not create file"; exit; } else { // *** // This is the line that actually writes the file // *** if(fwrite($handle,$pageData )===FALSE) { echo "Could not write to file"; exit; } fclose($handle); } NOTE This code is a modified version of the code posted by Elliott Brueggeman on php.net (http://us.php.net/manual/en/function.fwrite.php) Quote Link to comment Share on other sites More sharing options...
jber Posted July 21, 2007 Author Share Posted July 21, 2007 i´ve tried your solution but it does not work, it outputs an error that i can´t solve the error is : Parse error: syntax error, unexpected $end in C:\wamp\www\primeros_pasos\resultadofinal.php on line 103 I´ve searched if i have missed an ending of </html> or something like that, but i can´t find the error. Please, can you help me? I attach the code [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
redarrow Posted July 21, 2007 Share Posted July 21, 2007 there a missing } Quote Link to comment Share on other sites More sharing options...
jber Posted July 21, 2007 Author Share Posted July 21, 2007 I know it is something like what you say but i can´t find it!! I´ll Keep searching Quote Link to comment Share on other sites More sharing options...
jber Posted July 22, 2007 Author Share Posted July 22, 2007 Now it partially works. It do writes the name of each field in a html page (because i save the query in a html file, including the headers like this : $pageData = "<html><body><table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">"; but it does not write any line of the results, like it does not enter in the while loop Could it be the condition? Thanks in advance for your help Quote Link to comment Share on other sites More sharing options...
jber Posted July 22, 2007 Author Share Posted July 22, 2007 Topic solved! It is necessary to call the query again to make it works Thank you 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.