Export to Excel from html data using PHP - php

I have displayed a table of data from MYSQL DB. I have a button to export to excel. When I click on the button the excel file is created but it is empty or sometimes nothing is happening. What could be wrong here?
I have code like below:
view.php:
<div id="div_users">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th class="mn_th01">#</th>
<th class="mn_th02">Date</th>
<th class="mn_th03">Name</th>
<th class="mn_th04">Place</th>
<th class="mn_th05">Notes</th>
<th class="mn_th06">Sales Exec.</th>
<th class="mn_th07">Image</th>
</tr>
</thead>
<tbody>
<?php
date_default_timezone_set('Asia/Kolkata');
$date = date('d/m/Y', time());
echo $date . "\n";
$count=1;
$query = "SELECT * FROM ua_myadd_details WHERE STR_TO_DATE(notes_date,'%d/%m/%y') = STR_TO_DATE('$date', '%d/%m/%y') ORDER BY M.id DESC";
$result = mysqli_query($bd,$query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $row["notes_date"]; ?></td>
<td><?php echo $row["firstname"] . " " . $row["lastname"]; ?></td>
<td><?php echo $row["city"]; ?></td>
<td><?php echo $row["notes"]; ?></td>
<td><?php echo $row["executive"]; ?></td>
</tr>
<?php $count++; }
}
else
{?>
<tr>
<td colspan="7">
<?php echo "No Data Found"; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
I have a button like this:
<div class="col-md-3">
<button name="create_excel" id="create_excel" class="btn btn-success btn-block"><i class="fas fa-arrow-alt-circle-down"></i> Export to Excel</button>
</div>
In the script I have written:
<script>
$(document).ready(function(){
$('#create_excel').click(function(){
var excel_data = $('#div_users').html();
var page = "export.php?data=" + excel_data;
window.location = page;
});
});
</script>
export.php:
<?php
header('Content-Type: application/vnd.ms-excel');
header('Content-disposition: attachment; filename='.rand().'.xls');
echo $_GET["data"];
?>

Related

PHP View data from a single record by clicking on the view button

I have two pages students_list.php and students_profile.php
On students_list.php i have a table showing all records of all students and the VIEW button which when clicked it should open the students_profile.php page and get all the data from the single record selected based on the std_id
UPDATE: So now i get the id http://localhost/sms/student_profiles.php?std_id=3 when i click on the VIEW button but i am still not retrieving the data from this record selected
This is the code for students_list.php
<?php
require_once 'include/database/connect.php';
try {
$pdo = new PDO("mysql:host=$host_db;dbname=$name_db", $user_db, $pass_db);
$sql = 'SELECT * FROM students';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $name_db :" . $e->getMessage());
}
?>
<table id="data-table-default" class="table">
<thead>
<tr>
<th width="1%">#</th>
<th width="1%" data-orderable="false"></th>
<th>Name & Surname</th>
<th>Gender</th>
<th>Faculty</th>
<th>Semester</th>
<th>Mobile</th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr class="odd gradeX">
<td width="1%" class="f-s-600 text-inverse"><?php echo ($row['std_id']) ?></td>
<td width="1%" class="with-img"><?php echo ($row['std_status']) ?></td>
<td><?php echo ($row['std_name']) ?></td>
<td><?php echo ($row['std_gender']) ?></td>
<td><?php echo ($row['std_faculty']) ?></td>
<td><?php echo ($row['std_semester']) ?></td>
<td><?php echo ($row['std_mobile']) ?></td>
<td align="right">
<i class="fas fa-eye"></i> View
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and below this is the student_profile.php
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
}
?>
<h4><?php echo ($row['std_name']) ?></h4>
<div class="d-flex">
<div class="pr-4"><strong>NATIONAL#</strong> <?php echo ($row['std_number']) ?></div>
<div class="pr-4"><strong>DOB</strong> <?php echo ($row['std_dob']) ?></div>
<div class="pr-4"><strong>GENDER</strong> <?php echo ($row['std_gender']) ?></div>
<div class="pr-4"><strong>MOBILE</strong> <?php echo ($row['std_mobile']) ?></div>
</div>
<div class="pt-1 font-weight-bold">Master's Degree (MBA)</div>
<div>STD# <strong><?php echo ($row['std_id']) ?></strong></div>
<div>email#example.com</div>
You don't fecth the data from the query. So $row will never contain the array you expect:
$row = mysqli_fetch_assoc($res); //this will populate $row
in your code:
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($res);
}
?>

Generate multiple table (For viewing)

I would like to ask on how to make my table to appear as following:
But I only manage to get my table to appear like this:
As you can see, the table is separated according to the different year or semester of when they stored their stuff. It is basically a history storage of that particular person. Unfortunately, I don't know how to make the generated table to attach together for the one with same year and semester instead of separating it. Even the numbering is affected. Below is the code that I have so far:
<?php
include("connect.php");
include("header.php");
if(isset($_GET['invstuview_bag'], $_GET['invstuview_name']))
{
$get_id = $_GET['invstuview_bag'];
$get_name = $_GET['invstuview_name'];
?>
<br/>
<ul class="breadcrumb">
<li class="breadcrumb-item">View History</li>
<li class="breadcrumb-item-active">Baggage Detail History</a></li>
</ul>
<div id="cssword">Baggage Detail History For <?php echo $get_name; ?>(<?php echo $get_id; ?>)</div>
<div class="container" style="width:70%;">
<div class="table-responsive">
<?php
$sql_join = "SELECT student.*,location.*, inventory.*, baggage.*
FROM
student,location, inventory, baggage
WHERE
student.student_id = inventory.invstu_id AND
baggage.baggage_id = inventory.invbag_id AND
location.location_id = inventory.invloc_id AND
invstu_id = '$get_id'
ORDER BY inventory_id";
$result_join= mysqli_query($connect,$sql_join);
$prev_year = "";
$prev_sem = 0;
$get_year = "";
$get_sem = 0;
while($row_join=mysqli_fetch_assoc($result_join))
{
$counter = 1;
$prev_year = $row_join["invstu_year"];
$prev_sem = $row_join["invstu_sem"];
//if the data is of same year and semester
if(($prev_year!="") && ($prev_sem!=0) && ($prev_year == $get_year) && ($prev_sem == $get_sem))
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
//if data is not of same year or semester
else
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
</br></br>
Room: <?php echo $row_join["invstu_room"]; ?><br/>
Year: <?php echo $row_join["invstu_year"]; ?><br/>
Semester: <?php echo $row_join["invstu_sem"]; ?><br/>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
}
?>
</div>
<div align="right">
<button type="button" name="back" class="btn btn-success" id="back" style="width:200px; display:inline-block; margin-top:2%; margin-bottom:1%; background-color: #4CAF50" onclick="window.location.href='view.php'">Back</button>
</div>
</div>
<?php
}
?>
Any ideas is highly appreciated.
Move these out of the while loop:
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
echo'</table>';

how to view records in a database for just a particular user with a specific condition in php and mysqli

I am creating a website for my final year school project (a hospital management system using php and mysqli) and I am trying to create a search option whereby a patient can input his/her patient id and view all the details relating to him/her (time to meet with the doctor, date, doctors name etc.)
Code:
<?php
$con = mysqli_connect("localhost", "root", "", "ghospital");
if (mysqli_connect_errno()) {
echo "something is wrong with the database" . mysqli_connect_errno();
}
if (isset($_POST['patient_see_appointment'])) {
$patient_id = mysqli_real_escape_string($con, $_POST['patient_id']);
$sel_user = "SELECT * FROM appointments WHERE patient_id = '$patient_id'";
$run_user = mysqli_query($con, $sel_user);
while ($row = mysqli_fetch_array($run_user)) {
$id = $row['id'];
$patient_id = $row['patient_id'];
$patient_name = $row['patient_name'];
$phone = $row['phone'];
$doctor = $row['doctor'];
$nurse = $row['nurse'];
$time = $row['time'];
$am_pm = $row['am_pm'];
$day = $row['day'];
$month = $row['month'];
$year = $row['year'];
}
?>
<div id="" class="view_emergency" style="margin-top: 50px;">
<div class="container" >
<div class="row">
<div class="col-md-12 col-sm-12 ">
<h3 style="text-align: center;" >Patients Report Reports</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>patients id</th>
<th>patients name</th>
<th>phone no</th>
<th>Doctor </th>
<th>Nurse</th>
<th>Date</th>
<th>Time</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $patient_id; ?></td>
<td><?php echo $patient_name; ?></td>
<td><?php echo $phone; ?></td>
<td><?php echo $doctor; ?></td>
<td><?php echo $nurse; ?></td>
<td><?php echo $day; ?>:<?php echo $month; ?>:<?php echo $year; ?></td>
<td><?php echo $time; ?>:-<?php echo $am_pm; ?></td>
<td>Print</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php }?>
but it is just displaying just one single row where the patient id exist but the patients id exists more than one times in the database and it is suppose to display every single row where the patient id exists.
You need to iterate and print the query result.
Please try with this
<?php
$con = mysqli_connect("localhost", "root", "", "ghospital");
if (mysqli_connect_errno()) {
echo "something is wrong with the database" . mysqli_connect_errno();
}
if (isset($_POST['patient_see_appointment'])) {
$patient_id = mysqli_real_escape_string($con, $_POST['patient_id']);
$sel_user = "SELECT * FROM appointments WHERE patient_id = '$patient_id'";
$run_user = mysqli_query($con, $sel_user);
?>
<div id="" class="view_emergency" style="margin-top: 50px;">
<div class="container" >
<div class="row">
<div class="col-md-12 col-sm-12 ">
<h3 style="text-align: center;" >Patients Report Reports</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>patients id</th>
<th>patients name</th>
<th>phone no</th>
<th>Doctor </th>
<th>Nurse</th>
<th>Date</th>
<th>Time</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($run_user)) {
$id = $row['id'];
$patient_id = $row['patient_id'];
$patient_name = $row['patient_name'];
$phone = $row['phone'];
$doctor = $row['doctor'];
$nurse = $row['nurse'];
$time = $row['time'];
$am_pm = $row['am_pm'];
$day = $row['day'];
$month = $row['month'];
$year = $row['year'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $patient_id; ?></td>
<td><?php echo $patient_name; ?></td>
<td><?php echo $phone; ?></td>
<td><?php echo $doctor; ?></td>
<td><?php echo $nurse; ?></td>
<td><?php echo $day; ?>:<?php echo $month; ?>:<?php echo $year; ?></td>
<td><?php echo $time; ?>:-<?php echo $am_pm; ?></td>
<td>Print</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php }?>
Keep all the PHP at the top. Loop through, and concatenate to an $output string.
$output = '';
while ($row = mysqli_fetch_array($run_user)) {
$id = $row['id'];
$patient_id = $row['patient_id'];
$patient_name = $row['patient_name'];
$phone = $row['phone'];
$doctor = $row['doctor'];
$nurse = $row['nurse'];
$time = $row['time'];
$am_pm = $row['am_pm'];
$day = $row['day'];
$month = $row['month'];
$year = $row['year'];
$output .= "<tr>
<td>$id</td>
<td>$patient_id</td>
<td>$patient_name</td>
<td>$phone</td>
<td>$doctor</td>
<td>$nurse</td>
<td>$day:$month:$year</td>
<td>$time:-$am_pm</td>
<td>Print</td>
</tr>";
}
Then in the HTML:
<tbody><?= $output; ?></tbody>
1) Put <tr></tr> inside while loop.
2) Use mysqli_prepare: A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements are very useful against SQL injections, because parameter values, which are transmitted later using a different protocol, need not be correctly escaped. If the original statement template is not derived from external input, SQL injection cannot occur.
<?php
$con = mysqli_connect("localhost", "root", "", "ghospital");
if (mysqli_connect_errno()) {
echo "something is wrong with the database" . mysqli_connect_errno();
}
if (isset($_POST['patient_see_appointment'])) {
$stmt = mysqli_prepare($con, "SELECT * FROM appointments WHERE patient_id = ?");
mysqli_stmt_bind_param($stmt, "i", $_POST['patient_id']);
$run_user = mysqli_stmt_execute($stmt);
?>
<div id="" class="view_emergency" style="margin-top: 50px;">
<div class="container" >
<div class="row">
<div class="col-md-12 col-sm-12 ">
<h3 style="text-align: center;" >Patients Report Reports</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>patients id</th>
<th>patients name</th>
<th>phone no</th>
<th>Doctor </th>
<th>Nurse</th>
<th>Date</th>
<th>Time</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_stmt_fetch($stmt)){ $id = $row['id'];?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['patient_id']; ?></td>
<td><?php echo $row['patient_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['doctor']; ?></td>
<td><?php echo $row['nurse']; ?></td>
<td><?php echo $row['day']." : ".$row['month']." : ".$row['year']; ?></td>
<td><?php echo $row['time'].":-".$row['am_pm']; ?></td>
<td>Print</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php }?>
Quick Links:
Prepared Statements and Bound Parameters
mysqli_prepare

how do i create html table like on the picture

i have a 3 table and im doing inner join in the output below
how can i achive something like this
so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html
this is my code
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
</td></tr>
<?php endwhile; ?>
</table>
for the table to expand
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$("td[colspan=5]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.slideUp();
} else {
$target.closest("tr").next().find("p").slideToggle();
}
});
});
});//]]>
</script>
this is the output of the code
i need to group the data below by tracknum but i need the signatoryname
i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.
update: so far this is my code
UPDATE: below is the correct code:
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL sp_trasactionsignatory()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $r['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Problem:
How do I get all the signatoryname for each tracknum?
Solution:
Your very first initial query would be like this,
(Since you didn't provide the table name, change the table name from the below queries)
$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum");
Then comes to your table,
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
<tr>
<td><?php echo $r['tracknum']; ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
$result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
while ($row = $result_set->fetch_assoc()){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Don't forget to change the tablename in both the queries.
Edited:
If you using PDO extensions to execute your queries then you can do something like this,
<?php
$_tempp1 = $r['tracknum'];
$stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
$stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>

PHP echo money format plus amount

I have a table for total report and I want to get a value from a database and format the numbers and have an output like this PHP 10,000.00 not like this 10000
Here is my trial PHP code. I have a table for print of Total reports and got problem in formatting the total amount.
function printpage()
{
window.print()
}
</script></head>
<body>
<br>
<br>
<div class="container">
<center>
<div class="alert alert-success" align="center"><h1>TOTAL REPORT</h1></div>
<br />
<table class="table table-striped table-bordered" border="1" width="900">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Start of Event</th>
<th>End of Event</th>
<th>Quantity</th>
<th>Amount</th>
<th>Status</th>
<th>Confirmation</th>
</tr>
</thead>
<tbody>
<?php
$con=mysql_connect("localhost","root","")or die(mysql_error());
$db=mysql_select_db('pm')or die(mysql_error());
$query=mysql_query("select * from reservation ")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['id'];
?>
<tr>
<?php $row['id'] ?>
<td><?php echo $row['firstname'] ?></td>
<td><?php echo $row['lastname'] ?></td>
<td><?php echo $row['address'] ?></td>
<td><?php echo $row['arrival']?></td>
<td><?php echo $row['departure'] ?></td>
<td><?php echo $row['result'] ?></td>
<td><?php
$number = 'payable';
$payable = 0;
setlocale(LC_MONETARY,"en_PHP");
echo $row("The price is %i", $payable);
?> </td>
<td><?php echo $row['status'] ?></td>
<td><?php echo $row['confirmation'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
$result = mysql_query("SELECT sum(payable) FROM reservation") or die(mysql_error());
while ($rows = mysql_fetch_array($result)) {
?>
<?php }
$result1 = mysql_query("SELECT result(qty) FROM rooinventory") or die(mysql_error());
while ($rows1 = mysql_fetch_array($result1)) {
?>
<div class="pull-right">
<div class="span">
<div class="alert alert-info"><i class="icon-credit-card icon-large"> </i> Total Amount: <?php echo $rows1['sum(p)']; ?></div>
</div>
</div>
</center>
<?php } ?>
Your locale is wrong and $number should actually be a $row value.
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $row['payable']);
For Windows machines
echo '$' . number_format(floatval($row['payable']));
Your code need to be fixed. Also en_US might not be enough depending on your system (ie: Debian needs en_US.utf-8):
setlocale(LC_MONETARY, "en_US.utf-8");
echo money_format("The price is %i", $row['payable']);

Categories