My question is why it don't show $aantalkeermenu. but it show's the other variable's from the query can anyone help me with that?
This is my query:
$sql = "SELECT res.reserveringsnr, res.datum, menr.reserveringsnr, menr.menunr, SUM(menr.aantalkeermenu), men.menunr, men.menunaam, men.voorgerecht, men.hoofdgerecht, men.nagerecht FROM reserveringen AS res
INNER JOIN menus_regel as menr
ON res.reserveringsnr = menr.reserveringsnr
INNER JOIN menus AS men
ON menr.menunr = men.menunr
WHERE res.datum = '".$invoerdatum."'
GROUP BY menr.menunr
";
Here is show it on my webpage:
$result = mysql_query($sql);
echo "<table>";
echo "<th>Aantal</th> <th> Menunaam </th><th> Voorgerecht </th> <th> Hoofdgerecht </th> <th> Nagerecht </th>";
while($row = mysql_fetch_assoc($result)) {
$datum = $row['datum'];
$aantalkeermenu = $row['aantalkeermenu'];
$menunr = $row['menunr'];
$menunaam = $row['menunaam'];
$voorgerecht = $row['voorgerecht'];
$hoofdgerecht = $row['hoofdgerecht'];
$nagerecht = $row['nagerecht'];
$aantalkeermenu = $row['aantalkeermenu'];
// open tr
echo "<tr id='$menunr' class='edit_tr'>";
echo "<td><span>$menunr</span></td>";
echo "<td><span>$aantalkeermenu</span></td>";
echo "<td><span>$menunaam</span></td>";
echo "<td><span>$voorgerecht</span></td>";
echo "<td><span>$hoofdgerecht</span></td>";
echo "<td><span>$nagerecht</span></td>";
echo "</tr>";
// close tr
}
echo "</table>";
When you
select ... SUM(menr.aantalkeermenu)
the resulting column won't be called aantalkeermenu. You need to explicitely name the column, like this
select ... SUM(menr.aantalkeermenu) as aantalkeermenu
and then it will appear in under that name in the resulting row.
You need to alias the SUM(menr.aantalkeermenu) other wise I believe it is returned as SUM(menr.aantalkeermenu)
The following SQL should help.
$sql = "
SELECT res.reserveringsnr, res.datum, menr.reserveringsnr, menr.menunr,
SUM(menr.aantalkeermenu) AS aantalkeermenu, men.menunr, men.menunaam, men.voorgerecht,
men.hoofdgerecht, men.nagerecht
FROM reserveringen AS res
INNER JOIN menus_regel as menr
ON res.reserveringsnr = menr.reserveringsnr
INNER JOIN menus AS men
ON menr.menunr = men.menunr
WHERE res.datum = '".$invoerdatum."'
GROUP BY menr.menunr";
Related
I would like to create a matrix grid using widths and drops from the database.
The price would need to be displayed in the associated width and drop cell.
For example:
width x drop: 600 700 800 900
600 226
700 236
800 248
900 290
Some values will be not be there as the product instance has not been created.
I have three tables:
1) product_instance (id, product_name, width_id, drop_id, price)
2) product_width (id, width)
3) product drop (id, drop)
This is what I have so far:
<table>
<thead>
<tr>
<th>w x d</th>
<?
$query = "SELECT * FROM product_instance
INNER JOIN product_width ON product_instance.width_id
= product_width.width_id
INNER JOIN product_drop ON product_instance.drop_id =
product_drop.drop_id
INNER JOIN products ON product_instance.product_id =
products.product_id WHERE product_instance.product_id
= 1";
$select_products = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_products)) {
$width_value = $row['width_value'];
echo "<th>$width_value</th>";
}
?>
</tr>
</thead>
<tbody>
<?
$query = "SELECT DISTINCT * FROM product_instance
INNER JOIN product_width ON
product_instance.width_id
= product_width.width_id
INNER JOIN product_drop ON product_instance.drop_id =
product_drop.drop_id
INNER JOIN products ON product_instance.product_id =
products.product_id WHERE product_instance.product_id
= 1";
$select_products = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_products)) {
$drop_value = $row['drop_value'];
$price = $row['price'];
echo "<tr>";
echo "<td>$drop_value</td>";
echo "<td>$price</td>";
echo "</tr>";
}
?>
</tbody>
</table>
I'm not sure how to do this and have looked around but cant find a solution.
I managed to find a workable solution using PHPivot
My code now looks like this:
<?php
require 'pivot/PHPivot.php';
//Get the data
//Could be from a database, JSON, etc
$query = "SELECT * FROM product_instance
INNER JOIN product_width ON product_instance.width_id =
product_width.width_id
INNER JOIN product_drop ON product_instance.drop_id =
product_drop.drop_id
INNER JOIN products ON product_instance.product_id =
products.product_id WHERE product_instance.product_id = 1";
$select_products = mysqli_query($connection, $query);
$result = [];
$counter = 0;
while ($row = mysqli_fetch_array($select_products)) {
$menu[$counter] = array(
"width" => $row['width_value'],
"price" => $row['price'],
"drop" => $row['drop_value']
);
array_push($result,$menu[$counter]);
$counter ++;
}
$data = $result;
//Just needs to be saved in an associative PHP array
$products = PHPivot::create($data)
->setPivotRowFields('width')
->setPivotColumnFields('drop')
->setPivotValueFields('price',PHPivot::PIVOT_VALUE_SUM,
PHPivot::DISPLAY_AS_VALUE, 'Drop')
->addFilter('width', '', PHPivot::COMPARE_NOT_EQUAL)
//Filter out blanks/unknown genre
->generate();
echo $products->toHtml();
?>
I looked for an answer in here, but couldn't find one. I created a way of displaying data from my database in a nice table. Now when I realised I need a way of sorting that data after I executed my query?
first I run main sql query to get basic data:
$sql = "SELECT {$wpdb->users}.ID, firstname.meta_value as first_name,
lastname.meta_value as last_name, webaria_company.meta_value as webaria_company
FROM {$wpdb->users}
INNER JOIN (SELECT user_id, meta_value FROM {$wpdb->usermeta}
WHERE meta_key = 'first_name') as firstname ON {$wpdb->users}.ID = firstname.user_id
INNER JOIN (SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'last_name') as lastname ON {$wpdb->users}.ID = lastname.user_id
INNER JOIN (SELECT user_id, meta_value FROM {$wpdb->usermeta}
WHERE meta_key = 'webaria_company') as webaria_company ON {$wpdb->users}.ID = webaria_company.user_id";
$asuserlist = $wpdb->get_results($sql);
Then for each user ID i run a query to get each users results dynamically
<table width="100%">
<thead>
<tr>
<th align="center">Full Name</th>
<th align="center">Company</th>
<th align="center">Attainment Score</th>
</tr>
</thead>
<?php
if (empty($asuserlist)) {
echo "No Users' Data Available Yet!";
}
else {
foreach($asuserlist as $key=>$value){
$result = $wpdb->get_results( "SELECT `score_as` FROM `wattp2_as_score` WHERE `user_id`='".$asuserlist[$key]->ID."' AND `approved`=1 AND `date` > '".$sqldateoneyear."' ORDER BY `date` DESC LIMIT 4" );
// count results in the array of AS SCORE
$countresults = count($result);
// add all results of AS SCORE
$sum = 0;
foreach($result as $key2=>$value){
if(isset($value->score_as))
$sum += $value->score_as;
}
// get the average AS SCORE
$as_score1 = ($sum) / ($countresults);
// round AS SCORE
$as_score_round = round($as_score1);
if ($countresults == 0) {
$as_score_hund = "No Data"; } else {
$as_score_hund = $as_score_round . " over " . $countresults; }; ?>
<tbody><tr><td><?php echo $asuserlist[$key]->first_name . " " . $asuserlist[$key]->last_name; ?></td><td><?php echo $asuserlist[$key]->webaria_company; ?></td><td><?php echo $as_score_hund; ?></td></tr></tbody>
<?php } }?>
</table>
The problem is that i need to be able to sort the table results by the data that is calculated inside the loop and I honestly don't know, whether it is possible or I have to redo my code. I need to be able to sort the table by $countresults and $as_score_hund.
Any advise is welcome, even brief
This is a small section of code i've been working with.
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->here i want percentage</td>";
echo "</tr>";
}
?>
What I would like to do is display the percentage of total/target in the final column of my table. Not sure what the best way to do this is and would be keen to hear some ideas for this.
I did try doing it just as total/target in the echo but thinking it probably needs to be evaluated earlier as a variable or something? Whatever i tried didn't work anyway...
Looked at this but not sure how to implement it in my scenario - MSSQL display percentage calculated from two columns
Try this,
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp, (ifnull(t.total,0)/target)*100 as present FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->present</td>";
echo "</tr>";
}
?>
Here is logic
((cast("A/B" as float)/"C") * 100.00) AS "D%"
all value must be integer and when you are dividing them it must be convert as a float so calculation goes correctly.
Hope it will work for you.
I have managed to create a JOIN query for three tables and can successfully echo out the results in a echoed table, here is my code:
<?php
$sql="SELECT a.product_id, a.Options_id, b.product_name, b.product_price, c.Options_name, c.Price_diff
FROM ProductOptions a
JOIN Products b ON a.product_id = b.product_id
JOIN Options c ON a.Options_id = c.Options_id
ORDER BY product_name DESC";
$result = mysql_query($sql);
if (!$result)
{
echo "An error occurred ".mysql_error();
exit;
}
echo "<table border=1>\n<tr><th></th><th bgcolor=\"#DFE8EC\">Name</th><th>Flavors & Size</th><th bgcolor=\"#DFE8EC\">Price</th><th>Price Difference</th><th bgcolor=\"#DFE8EC\"></th></tr>\n";
while ($line = mysql_fetch_array($result)) {
$name = $line["product_name"];
$price = $line["product_price"];
$options=$line["Options_name"];
$difference=$line["Price_diff"];
echo "<tr><td></td><td bgcolor=\"#DFE8EC\">$name</td><td>$options</td> <td bgcolor=\"#DFE8EC\">£$price</td><td>£$difference</td><td bgcolor=\"#DFE8EC\"></td></tr>\n";
}
echo "</table>\n";
?>
My table works but it shows duplicate entries for product_name and I do not know how to remove them.
You have to use a GROUP BY clause in your query like this:
$sql = "SELECT a.product_id, a.Options_id, b.product_name, b.product_price, c.Options_name, c.Price_diff
FROM ProductOptions a
JOIN Products b ON a.product_id = b.product_id
JOIN Options c ON a.Options_id = c.Options_id
GROUP BY product_name
ORDER BY product_name DESC";
I have a very simple query but I need to make another query inside it. I have very little knowledge of SQL and PHP so I want to ask your help.
$result = mysql_query("SELECT * FROM img");
while ($row = mysql_fetch_assoc($result))
{
$imgName = $row['name'];
$catID = $row['catid'];
// Problem
// Need to get category NAME from category ID
$result2 = mysql_query("SELECT name FROM cat WHERE id = $catID");
while ($row2 = mysql_fetch_assoc($result2))
{
$catName = $row2['name'];
}
echo "Image: $imgName <br />";
echo "Category: $catName";
}
This looks to be a simple JOIN to get the category name. You can use a single query:
SELECT
img.id AS imgId,
img.name AS imgName,
cat.name AS catName
FROM img JOIN cat ON img.catid = cat.id
Replace your initial query with the above, and it will eliminate the need for the inner query.
You could do a simple subquery on this one:
$result = mysql_query("
SELECT name, catid, (SELECT name FROM cat WHERE id = img.catid) AS cat_name
FROM img
");
This should work. You'll see all the img properties of each table in each row and in addition the cat.name value.
SELECT *, cat.name as catname FROM img
INNER JOIN cat
ON cat.id = img.catid