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']);
Related
I want to output statement you are approve if the row status = 1, the data can be output now i just want the statement saying approve.
here's my code
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<tr>
<th>Nominee Name</th>
<th>Year</th>
<th>Department</th>
<th>StudentID</th>
<th>Position</th>
<th>View File</th>
<th>Nominee Photo</th>
<th>Nominated by</th>
<th>status</th>
</tr>
<?php
foreach ($nominees as $noms) {
?>
<tr>
<td><?php echo $noms['name']; ?></td>
<td><?php echo $noms['year']; ?></td>
<td><?php echo $noms['department']; ?></td>
<td><?php echo $noms['studentid']; ?></td>
<td><?php echo $noms['position']; ?></td>
<td></td>
<td><img src="<?php echo base_url(); ?>uploads/<?php echo $noms['photo']; ?>" style='width:50px;height:50px;'></td>
<td><?php echo $noms['nomname']; ?></td>
<?php if ($noms['approve'] == 1) { ?>
<td>Approve</td>
<?php } else { ?>
<td>pending</td>
<?php } ?>
</tr>
<?php } ?>
</table>
</div>
</div>
If the $nominees array returns the stays, then you can do like this
<?php if ( $nom['status'] == 1) { ?>
<td>Approve</td>
<?php }else{ ?>
<td>pending</td>
<?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"];
?>
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>';
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
Var dump result
I am fetching data in MySQL into an HTML table:
<div id="toggleDiv" class="">
<div class="box-body" id="toggleDiv_2">
<div class="row">
<div class="col-md-6">
<?php
$select_patient_info_cash =
"SELECT * FROM patient_info WHERE id_logged = :id_logged".
" AND patient_id = :patient_id AND payment_type = :pt";
$select_patient_info_cash_stmt =
$conn->prepare($select_patient_info_cash);
$select_patient_info_cash_stmt->bindValue(":id_logged", $id_logged);
$select_patient_info_cash_stmt->bindValue(":patient_id", $patient_id);
$select_patient_info_cash_stmt->bindValue(":pt", "cash");
$select_patient_info_cash_stmt->execute();
$select_patient_info_cash_stmt->fetch(PDO::FETCH_ASSOC);
$select_patient_info_cash_stmt_count =
$select_patient_info_cash_stmt->rowCount();
if($select_patient_info_cash_stmt_count > 0){ ?>
<table style="text-align:center"
class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Project Description</th>
<th>Project Cost</th>
<th>Date Of Pay</th>
</tr>
</thead>
<?php foreach ($select_patient_info_cash_stmt as $cash) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
</table>
<?php } else { ?>
<?php } ?>
</div><!-- /.col -->
Now I test it for a user that have data in patient info where payment_type != cash, and the <thead> didn't show up. But when I test it where payment_type=cash the <thead> shows up but no data are echoed into line.
It should show me 2 new lines after <thead> and I can't figure out why data are not displayed on the page
I think you miss ->fetch() from your prepared statement. According to PHP docs:
<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
?>
Therefore you need to modify your code to something like:
<?php while ($cash = $select_patient_info_cash_stmt->fetch()) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
I also suggest that you should use a MVC framework or some sort of template engines. Mixing PHP and HTML is a very bad practice.
References: http://php.net/manual/en/pdo.prepared-statements.php