• stefania92

    (@stefania92)


    Hey,

    I have a problem I can not solve.
    I have made a page called “Reports” in the admin menu, I have displayed the table with the fields in page, now I have a button and export the table to CSV format between 2 dates, I have data start and end data. I succeeded to display the table but the download will automatically appear and I don’t want this, I want to download it only when I press the button. I mention that all the code is written in functions.php. Here is my code :

    
    function my_admin_menu() {
    add_menu_page( ‘Reports’, ‘Reports’, ‘manage_options’, ‘reports.php’, ‘reports_admin_page’, ‘dashicons-tickets’, 6 );
    }
    function reports_admin_page(){ ?>
    
    Reports that will allows to create a report SF Requests
    ” method=”post”>
    Start Date
    End Date
    Export to CSV
    
    example 1
    example 2
    “;
    
    while($row = mysqli_fetch_array($result)) {
    echo “”;
    echo “” . $row[‘id’] . “</td>”;
    echo “<td>” . $row[‘user’] . “</div></td>”;
    echo “</tr>”;
    }
    echo “</table>”;
    
    mysqli_close($con);
    ?>
    <?php
    $database = “y”;
    $con = mysql_connect(“x”,’z’,”, ‘y’) or die( “Unable to Connect database”);
    mysql_select_db($database,$con) or die( “Unable to select database”);
    //$date1 = $_POST[‘date1’];
    //$date2 = $_POST[‘date2’];
    // Table Name to export in csv
    $ShowTable = “table”;
    
    $FileName = “reports_”.date(‘Ymd’) . “.csv”;
    $file = fopen($FileName,”w”);
    //$sql = mysql_query(“SELECT * FROM $ShowTable WHERE $start >= $date1 AND $end <= $date2″);
    $sql = mysql_query(“SELECT * FROM $ShowTable“);
    $row = mysql_fetch_assoc($sql);
    
    $HeadingsArray=array();
    foreach($row as $name => $value){
    $HeadingsArray[]=$name;
    }
    fputcsv($file,$HeadingsArray);
    
    while($row = mysql_fetch_assoc($sql)){
    $valuesArray=array();
    foreach($row as $name => $value){
    $valuesArray[]=$value;
    }
    fputcsv($file,$valuesArray);
    }
    fclose($file);
    
    header(“Location: $FileName”);
    ?>
    </div>
    <?php
    }
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Something is not right with your code as posted, it’s going from HTML to PHP without <?php ?> delimiters. It appears some lines did not get copied after “Reports that will…”

    I don’t see how the download is automatically appearing. Can you link to the page where this occurs? It would greatly help in our understanding of your issue.

    To start a download upon clicking a button, what you really want is a link to the file that includes the “download” attribute. Style the link to appear like a button if you like.

    BTW, the header() line near the end is throwing a “Headers already sent” error because output has already been sent. The Location: path is not followed.

Viewing 1 replies (of 1 total)

The topic ‘Export WordPress Table to CSV from page created in admin’ is closed to new replies.