Display results in a table form in view - php

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.

Related

instant delete when the button is cliked

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");
} ?>

How to fix broken line of table when querying information from database

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.';
}
}

How to avoid displaying duplicated values or overwrite duplicated values in php foreach loop in table?

I'm currently working on codeigniter. I want to display a value that is not been duplicated or overwrite a duplicated value from mysql database into the datatable of php foreach loop.
Here is the table code:
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
foreach($EMPLOYEES as $employee){?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php } ?>
</tbody>
</table>
Why not just use -
$EMPLOYEES = array_unique($EMPLOYEES);
Try this
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php $emp='';
foreach($EMPLOYEES as $employee){
if($emp!=$employee->username){ # if username contain duplicate values
?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php }$emp=$employee->username;} ?>
</tbody>
</table>

How can I stop repetition if row data same in column using PHP and MySQL

Here is my code. I am newer in programming. If I made any mistake please help me to solve it.
Thanks.
<table class="table table-striped" style="width:1300px; font-size:30px;" border="1px solid black">
<tr>
<th>Reference No</th>
<th>Category</th>
<th>Total Price</th>
</tr>
<?php
include("../db/db.php");
$sql_cost_details="select b.* from lc_details a, cost_details b where b.reference_no=a.reference_no AND a.lc_details_no='$lc_no' order by reference_no ASC";
$run_sql=mysqli_query($con, $sql_cost_details);
$i=0;
$total=0;
while($row_cost_details=mysqli_fetch_array($run_sql)){
$reference_no=$row_cost_details['reference_no'];
$category=$row_cost_details['category'];
$total_price=$row_cost_details['value'];
$i++;
?>
<tr align="center">
<td><?php echo $reference_no; ?></td>
<td><?php echo $category; ?></td>
<td><?php echo $total_price; ?></td>
</tr>
<?php } ?>
</table>
It shows this type of result
Expected result is this:
Use the below which solve your problem:
<table class="table table-striped" style="width:1300px; font-size:30px;" border="1px solid black">
<tr>
<th>Reference No</th>
<th>Category</th>
<th>Total Price</th>
</tr>
<?php
include("../db/db.php");
$sql_cost_details="select b.* from lc_details a, cost_details b where b.reference_no=a.reference_no AND a.lc_details_no='$lc_no' order by reference_no ASC";
$run_sql=mysqli_query($con, $sql_cost_details);
$i=0;
$total=0;
$prev = '';
while($row_cost_details=mysqli_fetch_array($run_sql)){
$reference_no=$row_cost_details['reference_no'];
$category=$row_cost_details['category'];
$total_price=$row_cost_details['value'];
$i++;
?>
<tr align="center">
<td><?php if ($prev != $reference_no){ echo $reference_no; } ?></td>
<td><?php echo $category; ?></td>
<td><?php echo $total_price; ?></td>
</tr>
<?php
$prev = $row_cost_details['reference_no'];
} ?>
</table>

Display database records in jQuery data tables

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>

Categories