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
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 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 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 have a function manage_child which it is in a datatables that show all children details. My problem is I want to view only one child details at another pages. I have tried to implement this example of code in my function but it didnt work since I call it like this:
`<td><a href="?pages=child_view"</a><?php_e($child_id); ?></td>`
I have link the page where the details should go but it didn't work. I am still in my basic using php and I use mysqli for the database.
This is the example that I tried to implement in my code but still doesn't work.
echo "<td>".$row[id]."</td>";
echo "<td>".$row[l_name]."</td>";
echo "<td>".$row[f_name]."</td>";
echo "<td>".$row[ssn]."</td>";
this is my code
function manage_child(){
$query = query("SELECT * FROM register_child");
confirm($query);
?>
<div class='container-fluid'>
<div class='row'>
<h1 class="page-header">Children
<small>Information</small>
</h1>
<div class="panel-body">
<div class="dataTable_wrapper">
<table width="50%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>No</th>
<th>CHild ID</th>
<th>Child IC</th>
<th>Name</th>
<th>Address</th>
<th>Mother name</th>
</tr>
</thead>
<tbody>
<?php
$bil = 0;
while($row = fetch_array($query)) {
$password = $row['ic_no'];
$child_id = $row['child_id'];
$child_name = $row['child_name'];
$address = $row['address'];
$mother_name = $row['mother_name'];
$bil++;
?>
<tr class="odd gradeX">
<td><?php echo $bil; ?></td>
<td><?php _e($password);?></td>
<td><a href="?pages=child_view"</a><?php _e($child_id); ?></td>
<td><?php _e($child_name); ?></td>
<td><?php _e($address); ?></td>
<td><?php _e($mother_name); ?></td>
<?php
}// End of while
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
}
am trying to get data from the database using php i have about four fields in my database, using the code below my table gets broken when querying from database into the table..
this is mycodeenter image description here
public function departments_view($dept_id){
$query = $this->db->prepare("SELECT * FROM materials_tbl WHERE dept_id = $dept_id");
$query->execute();
if($query->rowCount()>0){
?>
<div class="table-responsive">
<?php
while($row=$query->fetch(PDO::FETCH_ASSOC)){
?>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Material Code</th>
<th>Topic</th>
<th>Description</th>
<th>Path</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row["material_code"];?></td>
<td><?php echo $row["topic"];?></td>
<td><?php echo $row["description"];?></td>
<td><?php echo $row["path"];?></td>
</tr>
</tbody>
</table>
<?php
}
?>
</div>
<?php
}else {
echo 'Nothing here.';
}
}
Try this code,
Actually what mistake you've made is you've put table, tbody and table header inside the loop, that's why your table was breaking.
public function departments_view($dept_id){
$query = $this->db->prepare("SELECT * FROM materials_tbl WHERE dept_id = $dept_id");
$query->execute();
if($query->rowCount()>0){
?>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Material Code</th>
<th>Topic</th>
<th>Description</th>
<th>Path</th>
</tr>
</thead>
<tbody>
<?php
while($row=$query->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row["material_code"];?></td>
<td><?php echo $row["topic"];?></td>
<td><?php echo $row["description"];?></td>
<td><?php echo $row["path"];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}else {
echo 'Nothing here.';
}
}