I am building a library management system using PHP, Mysql. There is a category for each book. Users will be able to see how many books there are in each category. But I can't figure out how many books there are in each category. I want to find out how many books there are in each category.
<div class="row">
<div class="col-md-12">
<!-- Advanced Tables -->
<div class="panel panel-default">
<div class="panel-heading">
Categories Listing
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Book Amount</th>
<th>Status</th>
<th>Creation Date</th>
<th>Updation Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT tblcategory.CategoryName, tblcategory.Status, tblcategory.CreationDate, tblcategory.UpdationDate from tblcategory";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="center"><?php echo htmlentities($result->CategoryName);?></td>
<td class="center">#</td>
<td class="center"><?php if($result->Status==1) {?>
Active
<?php } else {?>
Inactive
<?php } ?></td>
<td class="center"><?php echo htmlentities($result->CreationDate);?></td>
<td class="center"><?php echo htmlentities($result->UpdationDate);?></td>
<td class="center">
<a href="edit-category.php?catid=<?php echo htmlentities($result->id);?>"><button class="btn btn-primary"><i class="fa fa-edit "></i> Edit</button>
<a href="manage-categories.php?del=<?php echo htmlentities($result->id);?>" onclick="return confirm('Are you sure you want to delete?');"" > <button class="btn btn-danger"><i class="fa fa-pencil"></i> Delete</button>
</td>
</tr>
<?php $cnt=$cnt+1;}} ?>
</tbody>
</table>
</div>
</div>
</div>
<!--End Advanced Tables -->
</div>
</div>
My Database:
The Output Should be:
Calculate category wise book count by using subquery. Then use LEFT JOIN with main table. Use COALESCE() for replacing NULL value to 0.
SELECT c.CategoryName
, COALESCE(b.book_amount, 0) book_amount
, CASE WHEN c.Status = 1 THEN 'Active' ELSE 'Inactive' END Status
, c.CreationDate
, c.UpdationDate
FROM category c
LEFT JOIN (SELECT catid
, COUNT(id) book_amount
FROM book
GROUP BY catid) b
ON c.id = b.catid
ORDER BY b.book_amount DESC
Related
I have created two tables, table1 and table2
table1 includes (id, codes and titles).
table2 includes id, table1_id, column1, column2, column3
I want Insert value on table1(codes , titles)
and show in table using PHP
and insert table2(column1, column2, column3) and show it
under table1 values.
This my codes
<?php
include('config.php');
$ret=" SELECT * from table1
";
$stmt= $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute() ;//ok
$res=$stmt->get_result();
$cnt=1;
while($row=$res->fetch_object())
{
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr >
<td style="border: none"><?php echo $row->codes; ?></td>
<td style="border: none"><?php echo $row->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria-
describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret=" SELECT * from table2";
$stmt= $mysqli->prepare($ret);
$stmt->execute();
$res=$stmt->get_result();
while($row=$res->fetch_object() )
{
?>
<tr>
<td><?php echo $row->column1 ?></td>
<td> <?php echo $row->column3 ?></td>
<td> <?php echo $row->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data-
target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>
I expected to shows me like this
but when I run that code it shows me , one value
You made two mistakes:
Used same variable name $row for iteration in outer and inner loop
Didn't specified table1_id in where condition in second query
Try below code, I am, sure It will work
<?php
include('config.php');
$ret = " SELECT * from table1
";
$stmt = $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute(); //ok
$res = $stmt->get_result();
$cnt = 1;
while ($row1 = $res->fetch_object()) {
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr>
<td style="border: none"><?php echo $row1->codes; ?></td>
<td style="border: none"><?php echo $row1->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria- describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret = " SELECT * from table2 where table1_id = ".$row1->id;
$stmt = $mysqli->prepare($ret);
$stmt->execute();
$res = $stmt->get_result();
while ($row2 = $res->fetch_object()) {
?>
<tr>
<td><?php echo $row2->column1 ?></td>
<td> <?php echo $row2->column3 ?></td>
<td> <?php echo $row2->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data- target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>
This is my table body.....
<section class="content">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title"></h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Category ID</th>
<th>Category Image</th>
<th>Category Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM `categories`";
$res_data = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($res_data)) {
// code...
?>
<tr>
<td><?php echo $row['Category_ID']; ?></td>
<td><?php echo "<img src=\"data:image;base64," . $row['Category_Image_Temp'] . "\" alt=" . $row['Category_Image_Name'] . " width=\"50px\" height=\"50px\">"; ?></td>
<td><?php echo $row['Category_Name']; ?></td>
<td><textarea><?php echo $row['Category_Name']; ?></textarea></td>
<td><a class="btn btn-info btn-sm" href="#">
<i class="fas fa-pencil-alt">
</i>
Update
</a></td>
<td><a class="btn btn-danger btn-sm" href="#">
<i class="fas fa-trash">
</i>
Delete
</a></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<th>Category ID</th>
<th>Category Image</th>
<th>Category Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</section>
So when I remove <td><?php echo "<img src=\"data:image;base64," . $row['Category_Image_Temp'] . "\" alt=" . $row['Category_Image_Name'] . " width=\"50px\" height=\"50px\">"; ?></td> this line from the code, then only the pagination and search appears.
Main issue is when I display images in the datatables the pagination and search doesn't appears.
I want to display pagination and search when I display images in datatables.
Anybody can help?
Thanks....
I am making a modal using looping data from the database on my project but I want to fetch all data in a database by selecting query GROUP BY color where id = modal id but my controller can't get the modal id which makes it error on selecting. please help me to fix it... thank you
this is the View code :
<table class="table table-bordered table-hover table table-sm" id="dataTable" width="100%" cellspacing="0">
<thead class="warna-header">
<tr>
<th rowspan="2" style="vertical-align:middle;text-align:center;">Tipe</th>
<th colspan="3" class="text-center bg-danger">1</th>
<th colspan="3" class="text-center bg-warning">2</th>
<th colspan="3" class="text-center bg-success">3</th>
<th rowspan="2" style="vertical-align:middle;text-align:center;" class="text-center">ACTION</th>
</tr>
</thead>
<tbody>
<?php foreach ($st_mobil as $st_mbl) : ?>
<tr>
<td style="font-size:12px;color:black;"><?php echo $st_mbl['jenismobil']; ?></td>
<td style="font-size:12px;color:black;text-align:center;"><?php echo $st_mbl['beli1']; ?></td>
<td style="font-size:12px;color`enter code here`:black;text-align:center;"><?php echo $st_mbl['jual1']; ?></td>
<td style="font-size:12px;color:black;text-align:center;"><?php echo $st_mbl['sisa1']; ?></td>
<td class="text-center">
<a href="" class="btn btn-success btn-sm view_detail" data-toggle="modal" title="Edit Data" data-target="#ViewStockMobilModal<?php echo $st_mbl['id']; ?>">
<span class="icon text-white-10">
<i class="fas fa-search"></i>
</span>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
-- Modal View
<?php foreach ($st_mobil as $st_mbl) : ?>
<div class="modal fade bd-example-modal-lg" id="ViewStockMobilModal<?php echo $st_mbl['id']; ?>" tabindex="-1" role="dialog"
aria-labelledby="ViewStockMobilModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header bg-primary">
<h5 class="modal-title" id="jenismobil" name="jenismobil">
<font color=white><?php echo $st_mbl['jenismobil']; ?></font>
</h5>
<button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<table class="table table-bordered table-striped table-sm">
<thead class="btn-primary">
<tr>
<th style="font-size:12px;" class="text-center bg-danger">ST</th>
<th style="font-size:12px;" class="text-center bg-danger">BK</th>
<th style="font-size:12px;" class="text-center bg-danger">BJ</th>
</tr>
</thead>
<?php foreach($view_detail as $vdt) : ?>
<tbody>
<tr>
<td style="font-size:12px;color:black;"><?php echo $vdt['warna']; ?></td>
<td style="font-size:12px;color:black;text-align:center;"><?php echo $vdt['st_in_1']; ?></td>
<td style="font-size:12px;color:black;text-align:center;"><?php echo $vdt['st_out_1']; ?></td>
</tr>
</tbody>
<?php endforeach; ?>
</table>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
This is the controller :
$data['st_mobil'] = $this->db->get('st_mobil')->result_array();
$id = $this->input->get($id); // i want to get the id of my modal
$data['view_detail'] = $this->stock_model->get_view_modal($id);
This is my model :
public function get_view_modal($id)
{
$query = "SELECT `id`,`jenismobil`,`warna`,`st_in_1`,`st_out_1`,`st_sisa_1`,`st_in_2`,`st_out_2`,`st_sisa_2`,`st_in_3`,`st_out_3`, st_sisa_3`
FROM `st_mobil`
GROUP BY `warna`
WHERE `id` = '$id'
";
return $this->db->query($query)->result_array();
}
When you getting the id
$id = $this->input->get('id'); // this should corrected like this.
How to add the total cost of the purchase items in my table?
I want to add another row , where in the sales total , of the user who bought the specific items to the supplier.
sales_detail table.
sales table.
Sample photo of where i want to add the total price
This would be a big help , thank you guys!
This is my php file.
<h1 class="page-header">Product Sales Report</h1></center>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<center>
<form action="total_sales.php" method="post">
From: <input type="text" class="datepicker" placeholder="E.G.(2018-01-14)" name="dayfrom" required pattern="[0-9]{4}+[0-9]+[0-9]"> To: <input type="text" class="datepicker" placeholder="E.G.(2018-02-11)" name="dayto" required pattern="[0-9]{4}+[0-9]+[0-9]">
<input type="submit" value="Show Sales" name="salesbtn" ></form></center>
<table width="100%" class="table table-striped table-bordered table-hover" id="prodTable">
<thead>
<tr>
<th class="hidden"></th>
<th>Purchase Date</th>
<th>Customer</th>
<th>Product Name</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
$sq=mysqli_query($conn,"select * from sales_detail left join product on product.productid=sales_detail.productid left join sales on sales.salesid=sales_detail.salesid left join customer on sales.userid=customer.userid where product.supplierid='".$_SESSION['id']."' order by sales.sales_date desc");;
while($sqrow=mysqli_fetch_array($sq)){
?>
<tr>
<td class="hidden"></td>
<td><?php echo date('M d, Y h:i A',strtotime($sqrow['sales_date'])); ?></td>
<td><?php echo $sqrow['customer_name']; ?></td>
<td><?php echo $sqrow['product_name']; ?></td>
<td><?php echo $sqrow['sales_qty']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
I added a conditional statement inside the while loop to output the sales total for a user of specific products.
Updated Code:
<h1 class="page-header">Product Sales Report</h1></center>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<center>
<form action="total_sales.php" method="post">
From: <input type="text" class="datepicker" placeholder="E.G.(2018-01-14)" name="dayfrom" required pattern="[0-9]{4}+[0-9]+[0-9]"> To: <input type="text" class="datepicker" placeholder="E.G.(2018-02-11)" name="dayto" required pattern="[0-9]{4}+[0-9]+[0-9]">
<input type="submit" value="Show Sales" name="salesbtn" ></form></center>
<table width="100%" class="table table-striped table-bordered table-hover" id="prodTable">
<thead>
<tr>
<th class="hidden"></th>
<th>Purchase Date</th>
<th>Customer</th>
<th>Product Name</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
$sales_total_id = array();
$sq=mysqli_query($conn,"select * from sales_detail left join product on product.productid=sales_detail.productid left join sales on sales.salesid=sales_detail.salesid left join customer on sales.userid=customer.userid where product.supplierid='".$_SESSION['id']."' order by sales.sales_date desc");
while($sqrow=mysqli_fetch_array($sq)){
if( !isset( $sales_total_id[$sqrow['salesid']] ) ) {
$sales_total_id[$sqrow['salesid']] = TRUE;
?>
<tr>
<td colspan="5" align="center"><strong>Sales Total: <?php echo $sqrow['sales_total']; ?></strong></td>
</tr>
<?php
}
?>
<tr>
<td class="hidden"></td>
<td><?php echo date('M d, Y h:i A',strtotime($sqrow['sales_date'])); ?></td>
<td><?php echo $sqrow['customer_name']; ?></td>
<td><?php echo $sqrow['product_name']; ?></td>
<td><?php echo $sqrow['sales_qty']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
I have a subject and a departments table, Each subject is associated with departments table. I am trying to select all subjects including departments name. The code bellow work perfect but show only two records. Any Help will be appreciated
//Select statement
$selects=$connection->query("SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
ON subjects.related_to = departments.dep_id");
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th>Sub Id</th>
<th>Subject Name</th>
<th>Related to Department</th>
<th>Related to Semester</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
while($result=$select->fetch_assoc()) {
?>
<tr>
<td><?php echo $result['id']; ?></td>
<td class="center"><?php echo $result['name']; ?></td>
<td class="center"><?php echo $result['dept']; ?></td>
<td class="center"><?php echo $result['related_to_sem']; ?></td>
<td class="center">
<a class="btn btn-info" href="#">
<i class="icon-edit icon-white"></i>
Edit
</a>
<a class="btn btn-danger" href="#">
<i class="icon-trash icon-white"></i>
Delete
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
select
subjects.id,subjects.name,
subjects.related_to,
subjects.related_to_sem,
departments.dept
from
subjects
LEFT OUTER JOIN departments ON subjects.id=departments.dep_id
Please try the following. I think you may have used the wrong fields in the join condition:
SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
/* ON subjects.id = departments.dep_id */
ON subjects.dep_id = departments.id
A "foreign key" in the subjects table for department is much more likely to be subjects.dep_id