2

I am new in PHP & MySQL. I know other languages but for this project it is being done in PHP. I found some code online that I was tweaking so it would accomplish my task. I am trying to export data from MySQL database, display it on screen and give an option to download to excel. The code runs and works, however it is not placing my data from the SQL database on the lines, only the table columns.

<?php
$conn = new mysqli('localhost', 'root', '');   
mysqli_select_db($conn, 'hcap');   

$data = mysqli_query($conn,"SELECT `Org_ID`,'Org_Name', 'Org_Address', 'Org_Address2', 'Org_City', 'Org_State', 'Org_Zip', 'Org_County',
'Org_Website', 'Org_Phone', 'Org_fax', 'Org_Email'
FROM `organization`"); 

if(isset($_POST["ExportType"])) {
    switch($_POST["ExportType"]) {
        case "export-to-excel" :
            // Submission from
            $filename = $_POST["ExportType"] . ".xls";       
            header("Content-Type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=\"$filename\"");
            ExportFile($data);
            //$_POST["ExportType"] = '';
            exit();
        default :
            die("Unknown action : ".$_POST["action"]);
            break;
    }
}
function ExportFile($records) {
    $heading = false;
    if(!empty($records))
        foreach($records as $row) {
            if(!$heading) {
                // display field/column names as a first row
                echo implode("\t", array_keys($row)) . "\n";
                $heading = true;
            }
            echo implode("\t", array_values($row)) . "\n";
        }
        exit;
}
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<title>HCAP Organization Report</title>

<div><h3>HCAP Organization Report</h1></div>
<div> 
    <div id="container" >
        <div class="col-sm-6 pull-left">
            <div class="well well-sm col-sm-12">
                <b id='project-capacity-count-lable'><?php echo count($data);?></b> records found.
                <div class="btn-group pull-right">
                    <button type="button" class="btn btn-info">Action</button>
                    <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <ul class="dropdown-menu" role="menu" id="export-menu">
                        <li id="export-to-excel"><a href="#">Export to excel</a></li>
                        <li class="divider"></li>
                        <li><a href="#">Other</a></li>
                    </ul>
                </div>
            </div>
            <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
                <input type="hidden" value='' id='hidden-type' name='ExportType'/>
            </form>
            <table id="" class="table table-striped table-bordered">
                <tr>
                    <th>Organization ID</th>
                    <th>Organization Name</th>
                    <th>Organization Address</th>
                    <th>Organization Address 2</th>
                    <th>City </th>
                    <th>State </th>
                    <th>Zip Code </th>
                    <th>County </th>
                    <th>Website </th>
                    <th>Phone Number </th>
                    <th>Fax Number </th>
                    <th>E-Mail Address </th>
                </tr>
                <tbody>
                    <?php foreach($data as $row):?>
                        <tr>
                            <td><?php echo $row ['Org_ID']?></td>
                            <td><?php echo $row ['Org_Name']?></td>
                            <td><?php echo $row ['Org_Address']?></td>
                            <td><?php echo $row ['Org_Address2']?></td>
                            <td><?php echo $row ['Org_City']?></td>
                            <td><?php echo $row ['Org_State']?></td>
                            <td><?php echo $row ['Org_Zip']?></td>
                            <td><?php echo $row ['Org_County']?></td>
                            <td><?php echo $row ['Org_Website']?></td>
                            <td><?php echo $row ['Org_Phone']?></td>
                            <td><?php echo $row ['Org_fax']?></td>
                            <td><?php echo $row ['Org_Email']?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>   
<script  type="text/javascript">
  $(document).ready(function() {
    jQuery('#export-menu li').bind("click", function() {
      var target = $(this).attr('id');
      switch(target) {
        case 'export-to-excel' :
          $('#hidden-type').val(target);
          //alert($('#hidden-type').val());
          $('#export-form').submit();
          $('#hidden-type').val('');
          break
      }
    });
  });
</script>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Rob A
  • 21
  • 1
  • Tips (not solutions): `implode("\t", array_values($row))` should be `implode("\t", $row)`. Declare the database as the 4th parameter of `new mysqli()`. Use backticks instead of single quotes to quote table column names. – mickmackusa Nov 06 '18 at 05:24
  • which php version are you using? I think it should work with version >= 5.4 – Yohanes Gultom Nov 06 '18 at 05:31

1 Answers1

0

You use single quotes instead of backticks. That is why you are seeing the literal column name where you expect to see the database value.

Use this instead:

SELECT `Org_ID`,`Org_Name`, `Org_Address`, `Org_Address2`, `Org_City`,
       `Org_State`, `Org_Zip`, `Org_County`, `Org_Website`, `Org_Phone`,
       `Org_fax`, `Org_Email`
FROM `organization`

or just don't use any quoting (because it is unnecessary for your query -- there are no RESERVED KEYWORDS or special characters to worry about).

I tested this advice to be true and accurate on my localhost.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    Since 5.4 `mysqli_result` has implemented `Traversable` http://php.net/manual/en/class.mysqli-result.php – Yohanes Gultom Nov 06 '18 at 05:33
  • Thanks so much for the help. what a lifesaver. i knew it was something dumb. side question which isn't a big deal. there was a built in counter and i can't figure out why it won't count the records. thanks again. – Rob A Nov 06 '18 at 14:47
  • I can review your code if you send me a 3v4l.org link. But it is almost 1am for me and I am shattered. I'll check here when I wake up. – mickmackusa Nov 06 '18 at 14:56
  • ok. i tried to do the 3v4l.org but not sure how to send the link to you. – Rob A Nov 06 '18 at 19:26
  • Just copy your script into the textarea, click eval, the page will reload with your script sill in it, the url will be updated. Just send me the new url. – mickmackusa Nov 06 '18 at 20:19
  • like this? https://3v4l.org/pgtUT/vld#output – Rob A Nov 06 '18 at 20:22
  • Sorry, I misunderstood what you meant by a counter. I didn't actually need you to post your script a second time. If you are saying `echo count($data);` doesn't work, then I recommend trying: `data->num_rows` but I am not yet out of bed to confirm this will work. – mickmackusa Nov 06 '18 at 20:31
  • the $data->num_rows worked ! thanks again for all your help. sorry to have bothered you while you were sleeping. thanks again. now get some rest. – Rob A Nov 06 '18 at 21:46