Join 2 table to add another table , sales total - php

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>

Related

how to show two mysql tables values at one while loop using php

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 } ?>

date filter not wroking for mysql in php

Table name :- add_user
data is inserting in it, there is a column due_date storing date in (dd-mm-yyyy) format
now i want to create a filter for due_date. But my code is not working. When i'm running the code it is showing all data, it is not filtering it.
here is my code
<form class="form-inline" method="post" action="">
<label> <b>From Date :  </b></label>
<input type="date" name="s_date" class="form-control"> 
<input type="date" name="e_date" class="form-control">
<button type="submit" name="search" class="btn btn-success">Search</button>
</form>
<?php
error_reporting(0);
include_once "config2.php";
if(count($_POST)>0) {
$s_date=date("dd-mm-yyyy", strtotime($_POST['s_date']));
$e_date=date("dd-mm-yyyy", strtotime($_POST['e_date']));
$result = mysqli_query($conn,"SELECT * FROM add_user WHERE due_date BETWEEN '$s_date' AND '$e_date'");
}
?>
<center>
<table class='table table-bordered'>
<thead class="thead-dark">
<tr>
<th>Action</th>
<th>Pet Photo</th>
<th>Owner</th>
<th>Mobile Number</th>
<th>Category</th>
<th>Pet Id</th>
<th>Pet Name</th>
<th>Address</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><button class="btn btn-info">View</button></td>
<td><img src="/pet_img/<?php echo $row["signature"]; ?>"width="60px"height="80px"> </td>
<td><img src="/pet_img/<?php echo $row["photo"]; ?>"width="60px"height="80px"> <br><b> <?php echo $row["name"]; ?> </td>
<td><?php echo $row["mobile"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><b><font color="green"><?php echo $row["pet_id"]; ?></font></b></td>
<td><?php echo $row["pet_name"]; ?></td>
<td><?php echo $row["add1"]; ?> <?php echo $row["add2"]; ?> <?php echo $row["add3"]; ?> <?php echo $row["pin"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
Your column data type needs to be either timestamp or date/datetime. You can't compare date strings in this format.

How to sort the output of sql data in php

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

Mini Add to Cart Coding

I am creating a mini shopping cart, where I displayed my item and and quantity of my item, I want to add my product price into quantity, how I can do this?
My code is
enter code here
<?php
Include_once("connection.php");
?>
<form action="view.php" action ="GET">
<div align="center">
<table align="center" border="2" width= 70%>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Total</th>
</tr>
<?php
$view = mysqli_query($connection, "select * from `add_cart`");
$afshan=mysqli_fetch_assoc($view);
if($afshan)
{
?>
<td><?php echo $afshan['name']?></td>
<td><?php echo $afshan['price']?></td>
<td><img width="50px" height="50px" src="image/<?php echo $afshan['image'];? >"></td>
<td><input type="number" name="quantity" min="0" max="3" step="1" value="0"> <input type="submit" name="cart" value="Add to Cart"></td><td></td>
<?php
}
?>
</table>
</div>
</form>
echo $afshan['price']*$afshan['quantity']

sort data by field heading php mysql

I have a database
that have two tables.
The sql query fetching data correctly.
I want to sort fields data by typing table heading in text box and click on go that in table footer.
Many Thanks
<table border="0" cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th class="capt" colspan="6">Available Projects</th>
</tr>
<tr>
<th>Select</th>
<th>Project</th>
<th>Crawler</th>
<th>Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include(dirname(__file__)."/includes/dbConn.php");
$result = mysql_query("SELECT *, ( SELECT name FROM projects WHERE projects.id = crawlers.pid ) AS pname FROM `crawlers`", $appConn);
while($row = mysql_fetch_array($result))
{
?>
<tr id="crawler_<?php echo $row['id']; ?>">
<td>
<input value="" id="check" type="checkbox">
<input id="crawler_id_<?php echo $row['id']; ?>" type="hidden" value="<?php $row['id']; ?>" /> <!-- crawler id -->
</td>
<td><?php echo $row['pname']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>username#domain.com</td>
<td>Enabled</td>
<td><a class="edit" href="#">edit</a><a class="add" href="#">run</a><a class="delete" href="#">delete</a></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="6"> Sort Fields by <input value="type here" id="field" type="text"> Go </th>
</tr>
</tfoot>
</table>
I think, you are looking for ORDER BY
Have a look at the MySQL Documentation about the SELECT syntax: http://dev.mysql.com/doc/refman/5.0/en/select.html

Categories