<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0" >
<thead>
<tr class=" card-header py-2" style=" text-align:center;">
<th> Er no</th>
<th> Name</th>
<th> Branch</th>
<th> Adm Year</th>
<th>Profile</th>
</tr>
<?php
if (isset($_POST['pass'])) {
$d = $_POST['spass'];
include('bcpdb.php');
$iq = "select * from stuinfo where ERNO like '%$d' ";
$qs = mysqli_query($con, $iq);
//echo "Serch Query Data";
if (!$qs) {
echo mysqli_error($con) . " DATA NOT INSERTED";
}
} else {
include('bcpdb.php');
$iq = "select * from stuinfo ";
$qs = mysqli_query($con, $iq);
//echo "select Query Data";
}
if (!$qs) {
echo mysqli_error($con) . " DATA NOT INSERTED";
}
while ($res = mysqli_fetch_assoc($qs)) {
?>
<tbody>
<tr>
<td> <?php echo $res['ERNO']; ?></td>
<td> <?php echo $res['SNAME']; ?></td>
<td><?php echo $res['BRANCH']; ?></td>
<td> <?php echo $res['ADMYEAR']; ?></td>
<td style="text-align:center;">
</td>
<?php
}
?>
</tr>
</tbody>
</table>
Just show rows in page not fetch all rows in datatable. I want all of the data of database fetch in datatable plugin, when I put some data in html table row column then it works perfectly, fetch and search all rows of data but in database data which fetch by select query, fetch only one row
[1]: https://i.stack.imgur.com/44FRd.jpg
Related
I'm using Bootstrap DataTables to display data from database but I have an issue with missing sorting and searching. Values from database are displaying correctly so I'm assuming I have syntax error(missing ' or sth), it allows me to display data but features of DataTables aren't working.
Here is my code:
<div class="table-responsive">
<table id="orders_data" class="table table-striped table-bordered">
<thead>
<tr>
<td> Imię </td>
<td> Nazwisko </td>
<td> Książki </td>
</tr>
</thead>
<?php
while($row = mysqli_fetch_array($result))
{
$sql2 = "SELECT * FROM books_has_authors JOIN books on books_has_authors.books_book_id = books.book_id
WHERE authors_id_author = '".$row["id_author"]."'";
$result2 = mysqli_query($conn, $sql2);
echo '
<tr>
<td>'.$row['fname'].'</td>
<td>'.$row['lname'].'</td>
<td>';
while($row2 = mysqli_fetch_array($result2)){
echo '<p>'.$row2['name'].'</p>';
}
echo '</td>';'
</tr>
';
}
?>
</table>
</div>
i am implementing clothing shopping website in which admin manages stock for example add, update, delete stock. i am having problem with my delete query. stock is displayed in table and every row has its own delete button. when i click on delete button it always takes last row id and delete that and not that row which i want. Query is taking always last row id.
CODE:
<form action="" method="post" enctype="multipart/form-data" name="deleting" >
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></td>
</tr>
<?php
}
?>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$id=$_POST['ID']; //problem is here: it always takes last row id
$link=mysqli_connect("localhost","root","") or die("Cannot Connect to the database!");
mysqli_select_db("login",$link) or die ("Cannot select the database!");
$query="DELETE FROM add_stock WHERE id='".$id."'";
$result=mysqli_query($query,$link) or die(mysqli_error($link));
if($result)
{
echo '<script>confirm("Are you sure want to delete this record?")</script>';
echo '<script>alert("Record ".$id." removed successfully!")</script>';
}
else
{
die ("An unexpected error occured while <b>deleting</b> the record, Please try again!");
}
}
?>
I would rather suggest not to use <form> just for deleting purpose. You can use <a> tag to redirect it to other page for deleting purpose.
Here is my code.
index.php
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query($sql);
while ($result = mysqli_fetch_array($rs_result)) {?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name']; ?></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td>
<a href="deleteStock.php?id=<?php echo $result['id'];?>">
<input type="button" value="Delete" >
</a>
</td>
</tr>
<?php }?>
</table>
<?php
if(isset($_GET['delete'])){
if($_GET['delete'] == "success"){
echo '<script>alert("Record removed successfully!")</script>';
}
if($_GET['delete'] == "fail"){
echo '<script>alert("An unexpected error occured while <b>deleting</b> the record, Please try again!")</script>';
}
}
?>
<script>
$(document).on('click', "#myTable a", function(e) {
if(confirm("Are you sure want to delete this record?")) {
return true;
} else {
return false;
}
});
</script>
deleteStock.php
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$link = mysqli_connect("localhost", "root", "") or die("Cannot Connect to the database!");
mysqli_select_db("login", $link) or die("Cannot select the database!");
$query = "DELETE FROM add_stock WHERE id = $id";
$result = mysqli_query($query, $link) or die(mysqli_error($link));
if($result){
header("location:index.php?delete=success");
} else {
header("location:index.php?delete=fail");
}
}?>
[Important: And, still you have not created separate file for DB Connection, which I have already mentioned in my answer of your question modal popup keep populating only first item data on all item buttons 1 Week before. It implies, you don't learn from your mistake or you don't need any suggestions.]
Your script taking last row in post always because you have single form for all your data and your last value will be overwrite all previous data. Intead of it remove form from out side of table add add it to td where you have specify hidden field and delete button. Like below:
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><form action="" method="post" name="deleting" ><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></form></td>
</tr>
<?php
}
?>
</table>
Also Remove enctype="multipart/form-data" from form its needed only if you have to upload file
I am trying to output data onto my webpage from a database. I am able to so successfully, but in a very untidy way. I have tried to put the data in a table on the website with no success.
Below, data is retrieved from the db, but just echoed out crudely. It looks untidy but it outputs successfully onto the webpage
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Address: " . $row["address"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
I try to put the data into an HTML table(on the same page, by combining PHP and HTML with no success. How can put this data into an organised table successfully?
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
</tbody>
</table>
</div>
Below is roughly how I would like the data to be structured, how can achieve this?
Name | Email | Address
Jo |J#o.com|12 Street
Ben |B#e.com|23 street
try this:
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Try below code :
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) { ?>
<td><?php $row["name"]?></td>
<td><?php $row["email"]?></td>
<td><?php $row["address"]?></td>
<?php }
} ?>
</tr>
</tbody>
</table>
</div>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
?>
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead> <tbody>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php }
} else {
echo "<tr><td colspan=3>0 results</td></tr>";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
You can always print the results from the select like this:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["name"]."</td>
<td>".$row["email"]."</td>
<td>".$row["address"] . "</td></tr>";
}
It's not a very clean way, but for each row should print another row, below the three table headers.
There isn't really a very neat way to do this beyond the other answers.
But what you can do is keep the HTML and PHP separate as much as possible in the same file.
The PHP at the top of the file can be as follows:
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
$htmlToDisplay = ''; //this variable will contain table rows
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
//create the HTML row - we'll use this later
$htmlToDisplay .= "<tr><td>".$row['name']."</td><td>". $row['email']."</td><td>".$row['address']."</td></tr>";
}
} else {
$htmlToDisplay = "<tr><td>0 results</td><tr>";
}
Then you can have your HTML after your closing PHP tag (?>) as follows:
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- echo your php row(s) here in a slightly neater way since it's one line -->
<?php echo $htmlToDisplay; ?>
</tbody>
</table>
This will make the the different parts of the file (the PHP and HTML) more readable.
You could also look at using a PHP template engine like smarty.
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.';
}
}
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