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>';
Related
i'm trying to build a website where users can find location of my company, but i need to hide the information at the first time and only show the location that users needed.
Here's my code:
<?php
$condition = '';
if(isset($_REQUEST['Kota']) and $_REQUEST['Kota']!=""){
$condition .= ' AND Kota LIKE "%'.$_REQUEST['Kota'].'%" ';
}
if(isset($_REQUEST['Outlet']) and $_REQUEST['Outlet']!=""){
$condition .= ' AND Outlet LIKE "%'.$_REQUEST['Outlet'].'%" ';
}
if(isset($_REQUEST['Alamat']) and $_REQUEST['Alamat']!=""){
$condition .= ' AND Alamat LIKE "%'.$_REQUEST['Alamat'].'%" ';
}
$userData = $db->getAllRecords('lokasi','*',$condition,'ORDER BY id DESC');
?>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr class="bg-primary text-white">
<th>No</th>
<th>Cabang GO</th>
<th>Nama Kota</th>
<th>Alamat Outlet</th>
<th>No Telepon</th>
</tr>
</thead>
<tbody>
<?php
$s = '';
foreach($userData as $val){
$s++;
?>
<tr>
<td><?php echo $s;?></td>
<td><?php echo $val['Kota'];?></td>
<td><?php echo $val['Outlet'];?></td>
<td><?php echo $val['Alamat'];?></td>
<td><?php echo $val['Nomor'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Start out with <div style=visibility:hidden>. Later change it to 'visible' using JavaScript code.
All the Code you have added is fine, you need few more things to add and it will be all working.
Add HTML Form with an input field for entering searching location and a submit button.
<form action="" method="post">
Search Location: <input type="text" name="location"> <input type="submit" name="submit">`enter code here`
</form>
Add "if" Condition to check the user submitted the form and the result from the db is not empty before the table, where you are displaying all the locations.
<?php if(isset($_POST['submit']) && count($userData) > 0){ ?>
<table class="table table-striped table-bordered">
<thead>
<tr class="bg-primary text-white">
<th>No</th>
<th>Cabang GO</th>
<th>Nama Kota</th>
<th>Alamat Outlet</th>
<th>No Telepon</th>
</tr>
</thead>
<tbody>
<?php
$s = '';
foreach($userData as $val){
$s++;
?>
<tr>
<td><?php echo $s;?></td>
<td><?php echo $val['Kota'];?></td>
<td><?php echo $val['Outlet'];?></td>
<td><?php echo $val['Alamat'];?></td>
<td><?php echo $val['Nomor'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?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 put PHP codes inside my HTML table. This makes table don't work again. It search status will not work, and show entries won't change, beside that, after what I get 11 datas inside the table, it will show only 10 in it and won't create page two.
But, If I put manually with HTML, it works fine. What's the problem? If I create manually inside HTML 's it works fine.
include_once 'info.php';
$query = $config -> prepare("SELECT `banID`, `user_banned`, `ban_reason`, `ban_time`, `user_banner`, `ban_timestamp` FROM `samp_ban` ORDER BY `banID` DESC LIMIT 10");
if($query -> execute())
{
$query_results = $query->fetchAll();
} foreach( $query_results as $query_result ) {
if($query_result["ban_time"] == 0) { $query_result["ban_time"] = "Permanent"; }}
?>
<div class="contentpanel">
<ol class="breadcrumb breadcrumb-quirk">
<li><i class="fa fa-home mr5"></i> Home</li>
<li>Non Grata List</li>
</ol>
<div class="panel">
<div class="panel-heading">
<h4 class="panel-title">Non GrataLIST</h4>
<p>Here's the non grata list.</p>
</div>
<div class="panel-body">
<div class="table-responsive">
<table id="dataTable1" class="table table-bordered table-striped-col">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>REASON</th>
<th>TIME</th>
<th>ADMIN</th>
<th>DATA</th>
</tr>
</thead>
<?php foreach( $query_results as $query_result ) {
if($query_result["ban_time"] == 0) { $query_result["ban_time"] = "Permanent"; } ?>
<tbody>
<tr>
<td><?php echo $query_result["banID"]; ?></td>
<td><?php echo $query_result["user_banned"]; ?></td>
<td><?php echo $query_result["ban_reason"]; ?></td>
<td><?php echo $query_result["ban_time"]; ?> </td>
<td><?php echo $query_result["user_banner"]; ?></td>
<td><?php echo $query_result["ban_timestamp"]; ?></td>
</tr><?php } ?>
</tbody>
</table>
</div>
</div>
</div><!-- panel -->
</div><!-- contentpanel -->
The only thing I can see that might be problematic with this code is that you open the table body tag in the foreach loop, but close it outside of the loop. So I would suggest opening you table body tag before the foreach loop:
<tbody>
<?php
foreach( $query_results as $query_result ) {
if($query_result["ban_time"] == 0) { $query_result["ban_time"] = "Permanent"; } ?>
<tr>
<td><?php echo $query_result["banID"]; ?></td>
<td><?php echo $query_result["user_banned"]; ?></td>
<td><?php echo $query_result["ban_reason"]; ?></td>
<td><?php echo $query_result["ban_time"]; ?> </td>
<td><?php echo $query_result["user_banner"]; ?></td>
<td><?php echo $query_result["ban_timestamp"]; ?></td>
</tr>
<?php } ?>
</tbody>
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