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.
Related
I have a table named 'entries' with the columns: originator, collaborator, engagement_partner, niche, and fee_potential. Some of the rows in the table have the same originator. I have a form that someone fills out, submits it to the database. I have tried using GROUP BY, but need some assistance on this.
My WordPress SQL statement: This will be used in the WordPress function file.
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM entries group by originator");
I need to display the data in a table format below. Basically, I need to group each entry based on Originator that are the same.
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
</tbody>
</table>
<br>
<br>
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Second Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>Second Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
</tbody>
</table>
I suggest you fetch all the entries and group them with php like so;
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM entries");
$originators = [];
foreach($rows as $row)
{
$originators[$row->originator][] = $row;
}
then loop through the originators to fill the table, something like this:
<?php
foreach($originators as $originator => $entries){
?>
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<?php foreach($entries as $entry){ ?>
<tr>
<td><b> <?php echo $entry->originator; ?></b></td>
<td><?php echo $entry->collaborator; ?></td>
<td><?php echo $entry->engagement_partner; ?></td>
<td><?php echo $entry->niche; ?></td>
<td><?php echo $entry->fee_potential;?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php}?>
It doesn't looks like a GROUP BY statement is what you are looking for.
Seeing your example HTML structure, the thing you are looking for is an ordered list of rows. You can then use php to sort the results and then use them
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM `entries`");
$sortedRows = [];
foreach($rows as $row) {
if (!isset($sortedRows[$row->originator])) {
$sortedRows[$row->originator] = [];
}
$sortedRows[$row->originator][] = $row;
}
a different approach is using ORDER BY in the query, which i won't fully work out since i dont know what your approach is on creating the HTML structure:
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM `entries` ORDER BY `originator` ASC");
$currentOriginator = NULL;
foreach ($rows as $row) {
if ($row->originator !== $currentOriginator) {
//create table
$currentOriginator = $row->originator;
}
//add the row to the table
}
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;
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>
}
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'];
}
?>
There is only one record in the table so why does it loop like i have 5 table with one letter each
$query = "Select * from click_tracker";
$result = mysql_query($query);
$all_clicks = mysql_fetch_array($result);
foreach($all_clicks as $click){
print "
<table border=\"1\">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>{$click['url_destination']}</td>
<td>{$click['count']}</td>
</tr>
</table>";
}
here is the table returned
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>h</td>
<td>h</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>h</td>
<td>h</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>5</td>
<td>5</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>5</td>
<td>5</td>
</tr>
</table>
mysql_fetch_array fetches one row as an array. When you try to loop over that result with your foreach, you are actually looping through all the columns of the row you returned (twice, actually, because by default, mysql_fetch_array returns an array with both numeric and indexed keys!)
If you want to get all the rows in your result set (and you more than likely do), you need to use a while loop to keep fetching rows until there aren't anymore:
$all_clicks = array();
while ($row = mysql_fetch_array($result))
{
$all_clicks[] = $row;
}
and then when you iterate over $all_clicks, each iteration will have a complete row.
mysql_fetch_array() returns rows, it looks like your foreach is looping over fields in a row not rows in a result set.
You appear to be printing multiple tables. I don't think this is what you intend though. You need to print the table's opening and closing tags, and the headings, outside of the loop. You should also call mysql_fetch_array() in the loop and not just once.
print "
<table border=\"1\">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>";
$query = "Select * from click_tracker";
$result = mysql_query($query);
while ($click = mysql_fetch_array($result)) {
print "
<tr>
<td>{$click['url_destination']}</td>
<td>{$click['count']}</td>
</tr>";
}
print "</table>";
You should also consider escaping the data in $click, but I don't know what your data looks like so I'm not sure what to put in the area just between the while and print statements.
You need to do it like this:
$query = "Select * from click_tracker";
$result = mysql_query($query);
while($click = mysql_fetch_assoc($result)) {
do a print_r($all_clicks) and check the result is what you expect it to be.
you don't really need to use a foreach if there's only one result.
$query = "Select * from click_tracker";
$result = mysql_query($query);
$all_clicks = mysql_fetch_array($result);
print "
<table border=\"1\">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>{$all_clicks['url_destination']}</td>
<td>{$all_clicks['count']}</td>
</tr>
</table>";