I want to fetch records from database and display it in data tables, but when i run this PHP code in a browser, it only shows records in a simple table. I want this result in jquery data tables for searching and sorting.
javascript function for data tables (uses jquery.dataTables.js)
<script type="text/javascript" language="javascript">
$(document).ready(function() {
('#datatable').DataTable();
})
</script>
mysql function for fetching records
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("runindia",$con);
$query="select *from admin_login";
$rs=mysql_query($query,$con);
?>
<div class="container">
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<?php
while($r=mysql_fetch_array($rs))
{ ?>
<tbody>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
</tbody>
<?php
} //closing while
mysql_close($con);//mysql connection close
?>
</table>
Try keeping 'tbody' out of the loop:
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<tbody>
<?php
while($r=mysql_fetch_array($rs))
{ ?>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
<?php
} //closing while
mysql_close($con);//mysql connection close
?>
</tbody>
</table>
Or try something better like getting the data via an AJAX call in a JSON format
<?php
$con=mysqli_connect("localhost","root","", "runindia");
$query="select * from admin_login";
$rs=mysqli_query($con, $query);
?>
<div class="container">
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<?php
while($r = mysqli_fetch_array($rs, MYSQLI_ASSOC))
{ ?>
<tbody>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
</tbody>
<?php
} //closing while
mysqli_close($con);//mysqli connection close
?>
</table>
Related
im trying to delete from a datatable when i click the remove button the data will be removed for a quick action in the admin side but my code dose not work i tried to fix it but i don't see any problem in the code here is my code
<div class="table-responsive">
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>
i give the delete button a value when clicked so it will delete with that value but it did not seems to work can any one help me
you have to placed table inside <form></form> tag
<div class="table-responsive">
<form>
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>
Right now I am writing a program in core PHP, but I am not able to write a perfect oops concept as I m rewriting the code many times.
for Eg.
public function all()
{
$init = new Database;
$sql = "SELECT * FROM categories";
// Result
$result = $init->conn->query($sql);
if ($result->num_rows > 0) {
?>
<table class="table table-hover">
<thead>
<tr>
<th>Action</th>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<form action="update_categories.php?update_id=<?php echo $row['id'] ?>" method="post">
<button type="submit" class="btn btn-primary" name="update">Update</button>
</form>
<form action="delete_categories.php?id=<?php echo $row['id'] ?>" method="post">
<button type="submit" name="delete" class="btn btn-danger">Delete</button>
</form>
</td>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["category_name"] ?></td>
<td><?php echo $row["category_description"] ?></td>
</tr>
<?php
} ?>
</tbody>
<?php
} else {
?>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">no result</th>
<td>no result</td>
<td>no result</td>
</tr>
</tbody>
<?php
}
}
So here you can see I have to fetch the data from the database but in this function, I have included Html too so if you want to add database data to another place I have to write the function for that again and fetch the values.
Is there any way I can only fetch the value and i can use the function to print it wherever I want.
I am trying to sort the output of the echo data that i am trying to bring from mysql database. There are different rows in the table and i want to sort by one of the rows named city. But it is showing data has last in first out. The code is here:
<?=APP::getElement()->triggerMessage(); ?>
<div class="panel">
<?php include_once(APP_HTML.'sub_menu.php');
$data=$model->data['data'];
// echo"<pre>";
// print_r($data);
?>
</div>
<?php include_once('search.php'); ?>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">All County</div>
<form name="List" id="List" class="form-horizontal" role="form" method="post" action="<?php echo $base_url.'delete';?>">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th><input type="checkbox" id="checkAll" name="checkAll" class="selectAll" /></th>
<th>County Name</th>
<!--<th> Parent County</th>
<th> County Status</th>-->
<!--<th> Product Type</th>
<th> Model No.</th>
<th> Image</th>
<th> Description</th>-->
<!-- <th>Country Name</th> -->
<th>Added Date</th>
<th>Updated Date</th>
<!-- <th>Weightage</th> -->
<th>Status</th>
<th width="12%">Action</th>
</tr>
</thead>
<tbody>
<?php
$pagination=$model->data['pagination'];
foreach($data as $row){
// echo $row['project_name'];
?>
<tr>
<td><input type="checkbox" id="id" name="id[]" class="checkID" value="<?php echo $row['id']; ?>"/></td>
<td><?php echo $row['city'] ?></td>
<td><?php echo $row['added_date']; ?></td>
<td><?php echo $update = empty($row['updated_date'])?'N/A':$row['updated_date']; ?></td>
<td><?php echo APP::getElement()->getStatus($row['status']); ?></td>
<td>
<?php $fields=array('update','delete','status'); ?>
<?=APP::getElement()->getActionButton($fields,$row);?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
</div>
<?php echo $pagination; ?>
please help.
Try to put ORDER BY in your SQL query.
Documentation
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.';
}
}
i am successfully fetching data from model named user_model to my controller named as login_controller. as shown below
in user_model:
function fetchData()
{
$this->db->select('DeviceName, DeviceType,RegistrationDateTime,LastUpdateDateTime,LastPushNotificationSent');
$query = $this->db->get('userinfo');
$res=$query->result();
if($res)
{
return $res;
}
else
{
return NULL;
}
}
in my controller i have:
function displayDatabase()
{
$data['tableInfo']=$this->user_model->fetchData();
$this->load->view('adminPanel_view',$data);
}
where in adminPanel_view i am doing this:
<table class="table table-striped table-bordered table-hover table-full- width" id="sample_1">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<?php echo $r->DeviceName ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
but i am unable to show the data in table format. What am i doing wrong here.The data is displayed like this
Please try this in your view file...
<table class="table table-striped table-bordered table-hover table-full- width" id="sample_1">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<td><?php echo $r->no ?></td>
<td><?php echo $r->DeviceName ?></td>
<td><?php echo $r->DeviceType ?></td>
<td><?php echo $r->RegistrationDateTime ?></td>
<td><?php echo $r->LastUpdateTime ?></td>
<td><?php echo $r->LastPushNotificationSent ?></td>
<td><?php echo $r->Actions ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Hope this will help you.