MySQL sales daily report - php

Hello guys I'm creating a daily report. I'm a beginner on my MySQL.
I want to generate this kind of data.
I have a table in my phpmyadmin with this structure
<table>
<tr>
<th>ID</th>
<th>Item_desc<th>
<th>Total_price<th>
<th>Quantity</th>
<th>Date</th>
<th>Branch</th>
</tr>
<tr>
<td>1</td>
<td>Bottle</td>
<td>100 </td>
<td>10 </td>
<td>June 26,2016 </td>
<td>Rizal</td>
</tr>
<tr>
<td>2</td>
<td>Bottle</td>
<td>120 </td>
<td>12 </td>
<td>June 26,2016 </td>
<td>Rizal</td>
</tr>
<tr>
<td>3</td>
<td>Bottle</td>
<td>100 </td>
<td>10 </td>
<td>June 26,2016 </td>
<td>Rizal</td>
</tr>
</table>
But I'm getting only 1 record. I want to get all of that records with the sum of total_price and quantity. This is my current query. Thank you.
$query= $this->db->select('*,SUM(total_price),SUM(quantity)')->from('sales')->where('branch',$_SESSION['branch'])->where('date',$from)->get();

Try this
$this->load->library('session');
$branch = $this->session->userdata('branch');
$query = $this->db->query("SELECT *,SUM(total_price) AS price,SUM(quantity) As quantity
FROM sales
WHERE branch = '$branch' AND WHERE date = '$from' ");
$result = $query->result_array();
return $result;

Related

Sql query from database

I need some help with how I can soft query from sql.
I have a php script with an html page who is showing the result from the query.
I have this query:
$ready_orders = DB::query( 'SELECT * FROM ordertable where orderid is NOT null ORDER by id DESC')->fetchAll( DB::FETCH_ASSOC );
From the HTML I have this;
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<td> want counter here</td>
<td>result 1</td>
<td>result 2</td>
<td>result 3</td>
<td>result 4</td>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</tr>
</table>
The query is doing all I want, but need the result to be in a listing.
Like a counter 1,2,3,4,5 etc
Is it possible?
include a
select count * from ordertable
Then insert this result in the first
SELECT COUNT(orderid) AS Order_Count
FROM ordertable
WHERE orderid IS NOT NULL
using foreach loop
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<?php
$i = 1;
foreach($ready_orders as $result){ ?>
<td>result <?php echo $i++; ?></td> //your counter
<?php } ?>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</tr>

How to show mysql multi row / mysql_fetch_array results in a table?

I am trying to show mysql_fetch_array() results in a table.
I want to show guests name,their country and their agreed time who are traveling on a same date.
Following code works fine. The code fetches the row values and prints it.
$select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor / guest loop starts here
echo $row['country'].'<br/>';
}
$select_agreed_time = mysql_query('SELECT agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_agreed_time, MYSQL_ASSOC)) { //visitor / guest agreed time loop starts here
echo $row['agreed_time'].'<br/>';
}
if there are 5 guests for a same date I am getting all of their names one below another when I execute the above code. Same I am getting there countries and agreed time too.
Now I want to show those results in a HTML table.
I tried several line of code but nothing works.
My HTML table should be as following:
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td class="text-left">Name 1</td>
<td class="text-left">Country 1</td>
<td class="text-left">Ag Time 1</td>
</tr>
<tr>
<td class="text-left">Name 2</td>
<td class="text-left">Country 2</td>
<td class="text-left">Ag Time 2</td>
</tr>
<tr>
<td class="text-left">Name 3</td>
<td class="text-left">Country 3</td>
<td class="text-left">Ag Time 3</td>
</tr>
<tr>
<td class="text-left">Name 4</td>
<td class="text-left">Country 4</td>
<td class="text-left">Ag Time 4</td>
</tr>
<tr>
<td class="text-left">Name 5</td>
<td class="text-left">Country 5</td>
<td class="text-left">Ag Time 5</td>
</tr>
</tbody>
</table>
How can create that table td s according to my mysql_fetch_array() ?
The above table structure is for 5 guests found or resulted by mysql_fetch_array()
First of all I think you dont need 3 different queries for your solution..
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<?php
$result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
<?php
}
?>
</table>
Firstly, you should use mysqli.
Secondly, shouldn't be sending so many queries to the database for information you can get in one query;
$select_guests = $mysqli->query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
Next, you want to fetch the number of rows.
$rows = $mysqli
You should also look into the PHP for function;
for ($i = 1; $i < $rows; $i++) {
$thisRow = $select_guests->fetch_row()
echo
' <tr>
<td class="text-left">'.$select_guests['name'].'</td>
<td class="text-left">'.$select_guests['country'].'</td>
<td class="text-left">'.$select_guests['time'].'</td>
</tr>
'; //This last line is to insert a line break and indent (for tidy HTML)
}
Give this a go, hopefully I've helped you.
I haven't completely solved it for you though, in order to change over to mysqli, you will need to make a few small changes which you can find in the mysqli link I sent you. The benefits are worth it.
$select_all = mysql_query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting all details for the same date
while($row = mysql_fetch_array($select_all , MYSQL_ASSOC)) {//loop starts here
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
}

how to show the count of particular name? & show it page using php sql?

i have a agent table
& where all agent details hai been display.
So along with the details i want to show count the agent name has been displayed in the table in same table.
i tried this query
Select count(name) from agent;
but it shows whole table count.
i want result to be this:
<table border="1">
<tr>
<td>name</td>
<td>count</td>
</tr>
<tr>
<td>bhaskar</td>
<td>1</td>
</tr>
<tr>
<td>amit</td>
<td>4</td>
</tr>
<tr>
<td>sushant</td>
<td>8</td>
</tr>
</table>
2/5
Saurabh 4/5
bt wat i m getting is this:
<table border="1">
<tr>
<td>name</td>
<td>count</td>
</tr>
<tr>
<td>bhaskar</td>
<td>13</td>
</tr>
<tr>
<td>amit</td>
<td>13</td>
</tr>
<tr>
<td>sushant</td>
<td>13</td>
</tr>
</table>
so how to show particular name count in table row in php page??
Try this
select count(name)as count,name from agent group by name
Update code
<?php $query=mysql_query("SELECT count(name) as count,phone,email,t_id,name FROM transfer WHERE agent_id='$id' group by name ORDER BY t_id DESC"); while($row=mysql_fetch_array($query)) {
echo $row['name'];
echo $row['count'];
}
?>

WordPress $wpdb->get_results show all the rows in a table

In WordPress, I have a table called wp_users
and I want to show all of them in a custom html table. So, my query goes like this
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<?php
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM wp_users");
foreach ( $rows as $row ) ?>
<tr>
<td>echo $row->first_name;</td>
<td>echo $row->first_name;</td>
<td> echo $row->first_name;</td>
</tr>
<?php
}
?>
This one is giving result for all the first_name inside the first table like this
<tr>
<td>Test 1Test 2</td>
<td>Test 12Test 2</td>
<td> test#test.comtest2#test.com</td>
</tr>
but I want the result be like this
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<tr>
<td>Test 1</td>
<td>Test 12</td>
<td> test#test.com</td>
</tr>
<tr>
<td>Test 2</td>
<td>Test 2</td>
<td> test2#test.com</td>
</tr>
</table>
So, Can someone tell me how to do this?
Any help and suggestions wll be really appreciable. Thanks
The answer is simple you just have to give the correct columnname inside foreach loop.
Right now you are just trying to print the firstname. And there is a bracket"{" missing after the foreach loop
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<?php
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM wp_users");
foreach ( $rows as $row )
{
?>
<tr>
<td>echo $row->first_name;</td>
<td>echo $row->Lastname;</td>
<td> echo $row->Email;</td>
</tr>
<?php }
?>

Fetch Values of same name item, without repeating item name?

I am making a page of my products, in which i have many products with having same names but different specifications and prices.
PHP code:
<code>
$SelectList = "SELECT a.p_id, a.dis,a.name,b.p_id,b.rate
FROM tst1 a
INNER JOIN max b
ON a.p_id = b.p_id GROUP BY name";
$Query = mysql_query($SelectList) or die(mysql_error());
$Num_rows = mysql_num_rows($Query);
$array = array();
while($Data=mysql_fetch_assoc($Query))
{
$array['name'][] = $Data['name'];
$array['rate'][] = $Data['rate'];
$array['dis'][] = $Data['dis'];
}
</code>
<code>
<table border="1">
<thead>
<th>Description</th>
<th>Unit</th>
<th>Qty</th>
<th>Rate</th>
</thead>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>65 mm</td>
<td>Nos</td>
<td>15</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>85 mm</td>
<td>Nos</td>
<td>45</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">WASHER VALVES</td>
</tr>
<tr>
<td>32 mm</td>
<td>Nos</td>
<td>95</td>
<td>30%</td>
</tr>
</table>
</code>
So what i want is, if product name is same so just fetch only "name" once, and its "unit, qty & rates" all of them.
<code>
<table style="position:relative; top:10px; bottom:10px; left:200px" border="1">
<thead>
<th>Description</th>
<th>Unit</th>
<th>Qty</th>
<th>Rate</th>
</thead>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>65 mm</td>
<td>Nos</td>
<td>15</td>
<td>40%</td>
<td>85 mm</td>
<td>Nos</td>
<td>45</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">WASHER VALVES</td>
</tr>
<tr>
<td>32 mm</td>
<td>Nos</td>
<td>95</td>
<td>30%</td>
</tr>
</table>
</code>
check this subquery.. this will help you out..
$qry = mysqli_query("select * from product group by name");
while($row = mysqli_fetch_array($qry))
{
echo "<tr><td colspan="0">'".$row['name']."'</td></tr>";
$qry2 = mysqli_query("select * from product where name = '".$row['name']."'");
echo "<tr>";
while($row2 = mysqli_fetch_array($qry2))
{
echo "<td>'".$row2['mm']."'</td><td>'".$row2['nos']."'</td><td>'".$row2['qty']."'</td><td>'".$row2['percentage']."'</td>";
}
echo "</tr>";
}
You may use either distinct or group by operator in your SQL to do so.
Usage of distinct looks like this:
SELECT DISTINCT column_name,column_name
FROM table_name;
You may take a look at this example of distinct.
Usage of group by looks like this:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Here is an example of group by.
I hope this helps.

Categories