-1

I have a HTML table in php code , I need the content of that table to be exported in to .csv file through a button (Hyperlink) from the same page.

Please support me in this.

       <html>
    <body>
     <?php
      $dt1 = $_POST["bday1"];
      $dt2 = $_POST["bday2"];

    // Create connection to Oracle
      $conn = oci_connect("cse", "mahesh123", "XE");

     $query = "select 
               trdt,SYMBOL,BID_PRICE,ASK_PRICE,OPEN_PRICE,
               HIGH_PRICE,LOW_PRICE,LAST_TRADED_PRICE,CLOSE_PRICE,
               to_char(TRADE_VOLUME,'999,999,999,999')TR_Volume,
               to_char(SHARE_VOLUME,'999,999,999,999')Share_Vol,
               to_char(TURNOVER,'999,999,999,999') Turnover,
               to_char(ISSUED_QUANTITY ,'999,999,999,999') Issued_Qty
               from daily_trades where trdt between TO_DATE('$dt1','YYYY-MM- 
               DD')and TO_DATE('$dt2','YYYY-MM-DD')
               and symbol='PABC-N-0000' order by trdt";

              //Date    Symbol  Short_Name  Bid_Price   Ask_Price    
           Open_Price   High_Price  Low_Price   Last_Traded_Price    
          Close_Price   Trade_Volume    Share_Volume    Turnover     
          Issued_Qty

            $stid = oci_parse($conn, $query);
           $r = oci_execute($stid);

               // Fetch each row in an associative array
             print '<table style="font-size:13px;">';
         echo "<tr><th >Date</th><th>Symbol</th><th>Bid_Price</th> 
    <th>Ask_Price</th><th>Open_Price</th><th>High_Price</th> 
    <th>Low_Price</th><th>Last_Traded</th> 
     <th>Close_Price</th><th>Trade_Volume</th><th>Share_Volume</th> 
      <th>Turnover</th><th>Issued_Qty</th></tr>";

        while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
       print '<tr>';
      foreach ($row as $item) {
   print '<td>'.($item !== null ? htmlentities($item, ENT_QUOTES) : 
   '&nbsp').'</td>';
         }
       print '</tr>';
          }
       print '</table>';

             ?>

          </body>
          </html>

I need to export the data in to a csv file.

user1793864
  • 241
  • 2
  • 6
  • 13
  • 1
    Is there anything you have tried? Any research? If not, this question is too broad for SO. – Jeff Mar 28 '18 at 10:48
  • 2
    https://stackoverflow.com/questions/4249432/export-to-csv-via-php I don't know if this could help with your problem. – Cusino Mar 28 '18 at 10:50
  • 2
    Possible duplicate of [Export to CSV via PHP](https://stackoverflow.com/questions/4249432/export-to-csv-via-php) – Jeff Mar 28 '18 at 10:52

1 Answers1

-1
<div class="form-group">
        <button onclick="Export()" class="btn btn-primary">Export to CSV File</button>
    </div>
    <!--  /Content   -->

    <script>
        function Export()
        {
            var conf = confirm("Export users to CSV?");
            if(conf == true)
            {
                window.open("export.php", '_blank');
            }
        }
    </script>
</div>

//create a file name export.php


 $query = "select 
               trdt,SYMBOL,BID_PRICE,ASK_PRICE,OPEN_PRICE,
               HIGH_PRICE,LOW_PRICE,LAST_TRADED_PRICE,CLOSE_PRICE,
               to_char(TRADE_VOLUME,'999,999,999,999')TR_Volume,
               to_char(SHARE_VOLUME,'999,999,999,999')Share_Vol,
               to_char(TURNOVER,'999,999,999,999') Turnover,
               to_char(ISSUED_QUANTITY ,'999,999,999,999') Issued_Qty
               from daily_trades where trdt between TO_DATE('$dt1','YYYY-MM- 
               DD')and TO_DATE('$dt2','YYYY-MM-DD')
               and symbol='PABC-N-0000' order by trdt";

              //Date    Symbol  Short_Name  Bid_Price   Ask_Price    
           Open_Price   High_Price  Low_Price   Last_Traded_Price    
          Close_Price   Trade_Volume    Share_Volume    Turnover     
          Issued_Qty

            $stid = oci_parse($conn, $query);
           $r = oci_execute($stid);
$users = array();


while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
$users[] = $row;
}

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=Users.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('No', 'First Name', 'Last Name', 'Email'));

if (count($users) > 0) {
    foreach ($users as $row) {
        fputcsv($output, $row);
    }
}