How would I make an admin only download page in php?
-
Is there any place to learn about making a secure admin page that lets me download my database into an excel file? For example, if I had an online survey and I saved the answers to the survey on the DB, instead of having an email being sent to me every time some one completes the survey. Someone could go to an admin page and download the DB in an excel format? The Excel form would have all of the Answered survey questions on it of course.
-
Answer:
Why don't you try using google docs? You can create a form that can embed in your site, and have the responses submitted populate a google docs spreadsheet.
Stuart Starr at Quora Visit the source
Other answers
<?php // Include the database file or connect to mySQL and select dabase here include('database.php'); // Your SQL query $sql = "Select * from table_name "; $result = mysql_query($sql); $sep = "\t"; //tabbed character // Mention your filename and path where you have to store that excel file $fp = fopen('../file_name.xls', "w"); $schema_insert = ""; $schema_insert_rows = ""; for ($i = 0; $i < mysql_num_fields($result); $i++) { $schema_insert_rows.=mysql_field_name($result,$i) . "\t"; } $schema_insert_rows.="\n"; fwrite($fp, $schema_insert_rows); //start while loop to get data while($row = mysql_fetch_row($result)) { //set_time_limit(60); // $schema_insert = ""; for($j=0; $j<mysql_num_fields($result);$j++) { if(!isset($row[$j])) $schema_insert .= "NULL".$sep; elseif ($row[$j] != "") $schema_insert .= strip_tags("$row[$j]").$sep; else $schema_insert .= "".$sep; } $schema_insert = str_replace($sep."$", "", $schema_insert); //this corrects output in excel when table fields contain \n or \r //these two characters are now replaced with a space $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); $schema_insert .= "\n"; //$schema_insert = (trim($schema_insert)); //print $schema_insert .= "\n"; //print "\n"; fwrite($fp, $schema_insert); } fclose($fp); // Code here will allow you to download the the excel file of your database $filename = '../file_name.xls'; $ctype="application/[extension]"; // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); // change, added quotes to allow spaces in filenames header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); ?>
Siddharth Vyas
Related Q & A:
- How can I make My Yahoo! my home page?Best solution by Yahoo! Answers
- How do I make my yahoo my home page?Best solution by Yahoo! Answers
- How can I make Yahoo as my home page?Best solution by Yahoo! Answers
- How can I make Yahoo.com my home page?Best solution by Yahoo! Answers
- How do I make yahoo finance my main page again?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.