php - i wan to export an excel from mysql database. i want to have the column name from users with the help of check box -


this normal csv query. want take column name user input. form containing checkbox or dropdown. how should modify it? have tried checkbox isset($_post["checkboxname"]) not working. me please.

<?php      include("../../../config.php");     //if(isset($_post["submit"])){      $xls_filename = 'export_'.date('y-m-d').'.xls'; // define excel (.xls) file name     $start_date=$_post["date_time"];     $sql_ex = "select device_name,description,card,device_module,uid device";     $result = @mysql_query($sql_ex,$conn) or die("failed execute query:<br />" . mysql_error(). "<br />" . mysql_errno());      // header info settings     header("content-type: application/xls");     header("content-disposition: attachment; filename=$xls_filename");     header("pragma: no-cache");     header("expires: 0");      /***** start of formatting excel *****/     // define separator (defines columns in excel &amp; tabs in word)     $sep = "\t"; // tabbed character      // start of printing column names names of mysql fields     ($i = 0; $i<mysql_num_fields($result); $i++) {       echo mysql_field_name($result, $i) . "\t";     }     print("\n");     // end of printing column names      // start while loop data     while($row = mysql_fetch_row($result))     {       $schema_insert = "";       for($j=0; $j<mysql_num_fields($result); $j++)       {         if(!isset($row[$j])) {           $schema_insert .= "null".$sep;         }         elseif ($row[$j] != "") {           $schema_insert .= "$row[$j]".$sep;         }         else {           $schema_insert .= "".$sep;         }       }       $schema_insert = str_replace($sep."$", "", $schema_insert);       $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);       $schema_insert .= "\t";       print(trim($schema_insert));       print "\n";     }      ?> 

firstly not excel format. you're making tsv file (and corrupting data removing line break characters). use fputcsv , write straight output, escape data don't have to.

for column selector, set associative array column names keys , user-friendly labels values. use form checkboxes created iterating array, name="columns[]" , value set column name , label echoed next checkbox. in part of script reading received values, first check if isset($_post['columns']) , if each value in posted value array exists in first array. if everything's okay, implode() posted columns use in query.


start off, don't have use post form reads data:

<?php $columns = [     'device_name' => 'device name',     'description' => 'description',     ... ];  // validate if (isset($_get['columns'])) {     if (!is_array($_get['columns'])) {         unset($_get['columns']);     }     else {         foreach ($_get['columns'] $k => $column) {             if (!is_string($column) || !isset($columns[$column])) {                 unset($_get['columns'][$k]);             }         }         if ($_get['columns']) {             $_get['columns'] = array_values($_get['columns']);         }         else {             unset($_get['columns']);         }     } }  // no (valid) column selection if (!isset($_get['columns']) {?>     <form action="">         <?php foreach ($columns $column => $label) {?>             <label><input type=checkbox value="<?php echo $column;?>">                 <?php echo $label;?></label>         <?php }?>     ... <?php } else {     ...     $sql_ex = 'select ' . implode(', ', $_get['columns']) . ' device';     ...     // column headings     fputcsv($output, array_intersect_key($columns, array_flip($_get['columns'])));     // output rows     ... } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -