Remove repeating data in a column - php

I am currently developing an ordering system where a customer can order many items. I also have an admin where he/she can see all the orders on that day. The Admin can view the name of the customer, the total payable, the products and the quantity of the product the customer have ordered.
I am currently seeing this results using my query.
Name | Payable | Product | Quantity
Test | 165 | keychain | 3
Test | 165 | Tumbler | 1
Miguel | 525 | Keychain | 3
Miguel | 525 | Magic Mug | 3
Dandel | 1010 | keychain | 3
Dandel | 1010 | T-shirt | 2
Dandel | 1010 | Keychain | 3
Dandel | 1010 | Mug | 5
This is my query.
$result = mysql_query("
SELECT reservation.firstname, reservation.lastname, reservation.payable, reservation.city, orders.product, orders.qty, reservation.date
FROM orders
INNER JOIN reservation
ON orders.confirmation = reservation.confirmation
WHERE reservation.date = CURDATE() && reservation.city = '24th Floor'
");
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td style="border: 1px solid black;">'.$row['firstname'].'</td>';
echo '<td>'.$row['payable'].'</td>';
echo '<td>'.$row['product'].'</td>';
echo '<td>'.$row['qty'].'</td>';
echo '</tr>';
}
I want to get results like this. How can I do it?
Name | Payable | Product | Quantity
Test | 165 | keychain | 3
| | Tumbler | 1
Miguel | 525 | Keychain | 3
| | Magic Mug | 3
Dandel | 1010 | keychain | 3
| | T-shirt | 2
| | Keychain | 3
| | Mug | 5

You can maintain some state while iterating which keeps track of whether the current row is a new name/payable:
$last_name_seen = NULL;
while ($row = mysql_fetch_array($result)) {
$firstname = "";
$payable = "";
if ($last_name_seen === NULL || $row['firstname'] != $last_name_seen) {
$last_name_seen = $row['firstname'];
$firstname = $row['firstname'];
$payable = $row['payable'];
}
echo '<tr>';
echo '<td style="border: 1px solid black;">'.$firstname.'</td>';
echo '<td>'.$payable.'</td>';
echo '<td>'.$row['product'].'</td>';
echo '<td>'.$row['qty'].'</td>';
echo '</tr>';
}
Note that your MySQL query should have some ORDER BY clause, to generate the ordering you want, e.g.
ORDER BY Name, Product;
As the comments above also suggest, if you are using a deprecated PHP API, you should consider upgrading to something more modern. But, the logic used in the above loop would not change much with changing the MySQL API.

You have to set flag for name & payable column & use array_column(),array_unique() to get unique column values :
<?php
$result = array(array("Name"=>"Test","Payable"=>'165',"Product"=>"keychain","Quantity"=>3),array("Name"=>"Test","Payable"=>'165',"Product"=>"Tumbler","Quantity"=>1));
$name_arr = array_unique(array_column($result, 'Name'));
$payable_arr = array_unique(array_column($result, 'Payable'));
$name_flag = 0;
$pay_flag = 0;
echo "<table border='1'>";
for($i=0;$i<count($name_arr);$i++)
{
for($j=0;$j<count($payable_arr);$j++)
{
foreach($result as $list)
{
if($list['Name'] == $name_arr[$i] && $list['Payable'] == $payable_arr[$j])
{
if($name_flag==0 && $pay_flag==0)
{
echo "<tr>";
echo "<td>".$name_arr[$i]."</td>";
echo "<td>".$payable_arr[$j]."</td>";
echo "<td>".$list['Product']."</td>";
echo "<td>".$list['Quantity']."</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td>".$list['Product']."</td>";
echo "<td>".$list['Quantity']."</td>";
echo "<tr>";
}
$name_flag++;
$pay_flag++;
}
}
}
$name_flag=0;
$pay_flag=0;
}
echo "</table>";
?>

Related

Displays the data array in the default table with looping

I have 1 array data may also be more and want to present it on the table, I tried to make the default table by 5 column, but when I try the same result 5 column all its data. should have 3 column 2 column is empty, what is lacking in my script?
This myscript
<?php
$no = 1;
for($x=1; $x<=5; $x++) {
foreach ($mydata as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
echo '</tr>';
$no++;
}
}
?>
the results of the script
NO | ID | NAME | ClASS |
____|____ |______|_______|
1 | 001 | Paul | x |
2 | 001 | Paul | x |
3 | 001 | Paul | x |
4 | 001 | Paul | x |
5 | 001 | Paul | x |
I expected
NO | ID | NAME | ClASS |
____|____ |______|_______|
1 | 001 | Paul | x |
2 | | | |
3 | | | |
4 | | | |
5 | | | |
Try this:
<?php
$no = 1;
for ($x=1; $x <= 5; $x++) {
if (isset($mydata[$x-1])) {
$row = $mydata[$x-1];
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
echo '</tr>';
} else {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
}
$no++;
}
?>
You need to check if the data is set based off $x var and then render empty row or populated.
You don't need nested loops. Just a single loop from 1 to 5, and each iteration shows the corresponding element of the array, or empty cells if there's no such element.
for ($x = 1; $x <= 5; $x++) {
echo "<tr>";
echo "<td>" . $x . "<td>";
if (isset($mydata[$x-1])) {
$row = $mydata[$x-1];
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
} else { // show empty fields
echo "<td></td><td></td><td></td>";
}
echo "</tr>";
}
There's also no need for separate variables $x and $no.

how to display data in two column with php with header

I have database
---------------------------------------------
no | name | code | grade
---------------------------------------------
1 | john | A1 | C
2 | john | A2 | D
3 | john | A3 | B
4 | tom | A1 | A
5 | john | A4 | A
6 | alice | A1 | C
7 | alice | A2 | D
8 | john | A5 | D
9 | john | A6 | C
---------------------------------------------
when I want show data with name john, I want result with 2 column:
---------------------------------------------------------------
no | name | code | grade | no | name | code | grade |
---------------------------------------------------------------
1 | john | A1 | C | 5 | john | A5 | D |
2 | john | A2 | D | 6 | john | A6 | C |
3 | john | A3 | B | | | | |
4 | john | A4 | A | | | | |
---------------------------------------------------------------
this my code that I already try
$result = mysql_query("select * from grade ");
echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>";
$no = 1;
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($row);
echo "<td>$no</td>";
echo "<td>$row[name]</td>";
echo "<td>$row[code]</td>";
echo "<td>$row[grade]</td>";
if ($count++ % 2 == 0)
{
echo "</tr><tr>";
$no++;
}
}
echo "</tr></table>";
But the result not what I want, this is the result when I run my code
no | name | code | grade | | | | |
---------------------------------------------------------------
1 | john | A1 | C | 1 | john | A4 | A |
2 | john | A2 | D | 2 | john | A5 | D |
3 | john | A3 | B | 3 | john | A6 | C |
---------------------------------------------------------------
Can somebody help me...thank you
This is representation problem, so it can be solved it two ways.
1. You can make this columns using modern HTML:
<section style="-webkit-column-count:2; -webkit-column-gap:15;">
<div class='columns'>
<table>
<tr>
<th>no</th>
<th>name</th>
<th>code</th>
<th>grade</th>
</tr>
<?php
$result = mysql_query("select * from grade ");
$no = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>';
echo "<td>{$row['no']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['code']}</td>";
echo "<td>{$row['grade']}</td>";
echo '</tr>';
$no++;
}
?>
</table>
</div>
</section>
2. If you want to stick to plain tables, you have to do some calculations:
First, you need to know full amount of grades you gonna show. You can get it with SQL query(more efficient way), I'll do it by preloading all data to array. HTML footer and header are omitted for brevity.
<?php
$data = array();
$result = mysql_query('SELECT * FROM grade;');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$data[] = $row;
}
$rows = (int) ceil(count($data)/2);
for ($i=0; $i < $rows; $i++) {
$no = $i + 1;
echo '<tr>';
echo "<td>{$no}</td>";
echo "<td>{$data[$i]['name']}</td>";
echo "<td>{$data[$i]['code']}</td>";
echo "<td>{$data[$i]['grade']}</td>";
if (array_key_exists($i + $rows, $data)) {
$no = $i + $rows + 1;
echo "<td>{$no}</td>";
echo "<td>{$data[$i + $rows]['name']}</td>";
echo "<td>{$data[$i + $rows]['code']}</td>";
echo "<td>{$data[$i + $rows]['grade']}</td>";
} else {
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
}
echo "</tr>\n";
}
I have not tested this nor checked for errors, but I think it is on the lines of what you are trying to accomplish.
$result = mysql_query("select * from grade ");
$orig_count = mysql_num_rows($result);
if ( $orig_count % 2 != 0 )
$count = $orig_count + 1;
$cols = $count / 2;
//draw header
echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>"
for ( $i = 0; $i < $cols; i++ )
{
if ( $orig_count == 0 )
break; //zero results
//first column
echo "<td>$i</td>";
echo "<td>".mysql_result( $result, $i, "name" )."</td>";
echo "<td>".mysql_result( $result, $i, "code" )."</td>";
echo "<td>".mysql_result( $result, $i, "grade" )."</td>";
//second column
if ( $i > $orig_count )
{
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
}
else
{
echo "<td>".$i+($count/2)."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "name" )."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "code" )."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "grade" )."</td>";
}
}
//finish off
echo "</tr></table>";

Querying 3 mysql tables to create an HTML table

Goal
I'm trying to draw up a restaurant menu in HTML using MySQL queries.
MySQL Tables
Categories
+-----------------------------------+
| id | nom_categorie | dimensions |
+-----------------------------------+
| 1 | pâtes | 1-2 |
| 2 | ailes de poulet | 3-4-5 |
+-----------------------------------+
Dimensions
+---------------------+
| id | dimension |
+---------------------+
| 1 | Petit |
| 2 | Gros |
| 3 | Unité |
| 4 | Repas (6) |
| 5 | Repas (12) |
+---------------------+
Repas
+-----------------------------------+
| id | repas | prix |
+-----------------------------------+
| 1 | spaghetti | 8.75,11.75 |
| 2 | lasagne | 9.95,13.25 |
| 3 | régulières | 0.95,9.50,11.95 |
| 4 | piquantes | 0.95,9.50,11.95 |
+-----------------------------------+
Desired outcome
+--------------------------------------+
| Pâtes |
+--------------------------------------+
| | petit |  gros |
+--------------------------------------+
| spaghetti | 8.75 | 11.75 |
| lasagne | 9.95 | 13.25 |
+--------------------------------------+
+-----------------------------------------------+
| Ailes de poulet |
+-----------------------------------------------+
| | Unité | Repas (6) | Repas (12) |
+-----------------------------------------------+
| Régulières | 0.95 | 9.50 | 11.95 |
| Piquantes  | 0.95 | 9.50 | 11.95 |
+-----------------------------------------------+
Current outcome
+--------------------------------------+
| Pâtes |
+--------------------------------------+
| | 1 | 2 |
+--------------------------------------+
| spaghetti | 8.75 | 11.75 |
| lasagne | 9.95 | 13.25 |
+--------------------------------------+
+--------------------------------------+
| Ailes de poulet |
+--------------------------------------+
| | 3 | 4 | 5 |
+--------------------------------------+
| Régulières | 0.95 | 9.50 | 11.95 |
| Piquantes | 0.95 | 9.50 | 11.95 |
+--------------------------------------+
Current code
function catalogue_complet($mysqli) {
$categories = mysqli_query($mysqli, "SELECT * FROM categories ORDER BY nom_categorie ASC");
$produits = mysqli_query($mysqli, "SELECT * FROM produits ORDER BY id, prix ASC");
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
while($categorie = mysqli_fetch_array($categories))
{
echo "<h2>".$categorie[1]."</h2>";
echo "<table class='catalogue'>";
if(!empty($categorie[2]))
{
echo "<tr>";
echo "<th> </th>";
$array_categorie = explode("-",$categorie[2]);
foreach($array_categorie as $categorie)
{
echo "<th>".$categorie."</th>";
}
echo "</tr>";
}
while($produit = mysqli_fetch_array($produits))
{
if($categorie[0] == $produit[2])
{
echo "<tr>";
echo "<td>".$produit[1]."</td>";
$array_prix = explode(",",$produit[2]);
foreach($array_prix as $prix)
{
echo "<td class='prix'>".$prix."</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
}
Other details
I may have a few errors slipped in with keys (example: $produit[2]) as I've simplified the tables, but I do have the proper information in my output. As I've mentioned above, my problem lies in the fact that I get the dimension ID as entered in my table repas. I can't figure out how to get the proper information (Ailes de poulet and Pâtes) to show up ?
I think you basically need to get the column names of your tables correctly.
In your current code, you are not using the dimensions table at all, although you have queried it.
Keep the names of all dimensions in an array, and refer them whenever the id is known.
Storing the dimensions in an array.
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
$dim_arr = Array();
if($dimensions){
while($d = $mysqli_fetch_assoc($dimensions)){
$dim_arr[$d['id']] = $d['dimension'];
}
}else{
die(mysqli_error($mysqli));
}
Using them inside the foreach loop.
foreach($array_categorie as $categorie)
{
//Perform error checking here - if(isset($dim_arr[$categorie]))
$dim_name = $dim_arr[$categorie];
echo "<th>".$dim_name."</th>";
}
This should display the headings.
Ok, so I've found a working solution. Please excuse the length of the code, and as mentioned in the original post, some differences may occur when looking at the tables in the question, but it is simplified to focus on the desired effect.
Bottom line, the code below produces the table with title (ex: Pâtes), and each product and price line (ex: Lasagne | 9,95 | 13,25)
function catalogue_complet($mysqli) {
$categories = mysqli_query($mysqli, "SELECT * FROM catalogue_categories ORDER BY nom_categorie ASC");
$produits = mysqli_query($mysqli, "SELECT * FROM produits ORDER BY id, prix ASC");
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
while($categorie = mysqli_fetch_array($categories))
{
echo "<h2>".$categorie[1]."</h2>";
echo "<table class='catalogue'>";
mysqli_data_seek($produits, 0);
if(empty($produits))
{
echo "<tr><td>".AUCUN_PRODUIT."</td></tr>";
}
else
{
while($produit = mysqli_fetch_array($produits))
{
// Dimensions -----------------------------------------------------------
if(!empty($categorie[3]))
{
echo "<tr>";
while($dimension = mysqli_fetch_array($dimensions))
{
$array_dimensions = explode("-",$categorie[3]);
foreach($array_dimensions as $dim)
{
if($dim == $dimension[0])
{
if($i == 0)
{
echo "<th> </th>";
}
echo "<th>".$dimension[1]."</th>";
$i++;
}
}
}
echo "</tr>";
}
// Produits -------------------------------------------------------------
if($categorie[0] == $produit[4])
{
echo "<tr>";
echo "<td>".$produit[1]."</td>";
$array_prix = explode(",",$produit[3]);
foreach($array_prix as $prix)
{
if( empty($prix) or ($prix == "0"))
{
echo "<td> </td>";
}
else
{
echo "<td class='prix'>".str_replace(".",",",$prix)." $</td>";
}
}
echo "</tr>";
}
}
}
echo "</table>";
}
}

PHP display images from column value

Table
+-----+--------+---------+
| ID | Name | Images |
+-----+--------+---------+
| 001 | John | 5 |
| 002 | Mark | 3 |
+-----+--------+---------+
i would like to display like this
Jon, 001-1.jpg | 001-2.jpg | 001-2.jpg | 001-3.jpg | 001-4.jpg | 001-5.jpg |
Mark, 002-1.jpg | 002-2.jpg | 002-2.jpg | 002-3.jpg |
the images value on database table will be the number of images return to create images link
You can use this though. Didn't know why you found it difficult.
$c = 0
while (false !== ($data = fetch_array_as_row_function()))
{
echo "<tr>";
echo "<td>", $data["name"], "</td>";
for ($i = 0; $i < $data["images"]; $i++)
echo "<td>00", $c,"-", $i, "</td>";
echo "</tr>";
}
Here, the function fetch_array_as_row_function() is something equivalent to what mysql_fetch_array() does.

How to echo specific data in loop with a condition specific to the last row

For an accounting system, I'm using PHP & MySQL. I've two tables "GROUP" and "ACHEADS".
In the GROUP table, I have:
---------------------
| id (AI) | group |
---------------------
| 1 | Group 1 |
| 2 | Group 2 |
---------------------
In the ACHEADS table, I have:
-----------------------------------------
| id (AI) | ac_head | amount | j_id |
-----------------------------------------
| 1 | Something 1 | 2000 | 1 |
| 2 | Something 2 | 1000 | 1 |
| 3 | Something 3 | 5000 | 2 |
| 4 | Something 4 | 4000 | 2 |
| 5 | Something 5 | 8000 | 2 |
-----------------------------------------
I've joined the two tables as GROUP.id <<->> ACHEADS.j_id
Now I need to preview the data like this:
----------------------------------------------
Particulars | Details | Total |
----------------------------------------------
Group 1 | | |
Something 1 | 2000 | |
Something 2 | 1000 | 3000 |
----------------------------------------------
Group 2 | | |
Something 3 | 5000 | |
Something 4 | 4000 | |
Something 5 | 8000 | 17000 |
----------------------------------------------
GRAND TOTAL | | 20000 |
------------------------------------==========
Challenges
The table will be dynamic and will generate within a PHP loop (I'm
using a WHILE loop)
Remember: it's a table and if I miss echoing a td, then the table will break up
Problems
When I'm using the loop it's echoing the data on the Details td
accurately. But the sum of the details row according to j_id is also
echoing in each td
Preview here:
----------------------------------------------
Particulars | Details | Total |
----------------------------------------------
Group 1 | | |
Something 1 | 2000 | 3000 |
Something 2 | 1000 | 3000 |
----------------------------------------------
Group 2 | | |
Something 3 | 5000 | 17000 |
Something 4 | 4000 | 17000 |
Something 5 | 8000 | 17000 |
----------------------------------------------
My thoughts
If I can check whether it is the last data of the query, if isset,
then echo the total amount with it's td. (But remember the
Challenge#2)
Does it require a foreach loop?
I failed
I tried checking max(id), it works fine in SQL, but can't use it in
condition within a loop.
(If you still can't understand me, then on the second phase, I'll post my code.)
I would do 2 loops:
Fetch id from GROUP
Fetch amount from ACHEADS based on j_id
This would look something like (non-tested code):
echo '<table><tr><td>Particulars</td><td>Details</td><td>Total</td></tr>';
$total = 0;
$q1 = "SELECT id FROM `GROUP`";
$res1 = mysqli_query($q1);
while($row1 = mysqli_fetch_assoc($res1)) {
echo
$group_total = 0;
$j_id = $row1[id];
$q2 = "SELECT ac_head, amount FROM ACHEADS WHERE j_id = $j_id";
$res2 = mysqli_query($q2);
while($row2 = mysqli_fetch_assoc($res1)) {
echo '<tr><td>' . $row2[ac_head] . '</td>';
echo '<td>' . $row2[amount] . '</td></tr>';
$group_total = $group_total + $row2[amount];
$total = $total + $row[amount];
}
echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}
echo '<tr><td>GRAND TOTAL</td>';
echo '<td colspan="2" align="right">' . $total . '</td></tr>';
echo "</table>";
njk rockz!
It worked nicely. Thanks a lot, brother - it helped me a lot, I can't explain.
Here is my final code:
<tr style="background: #000; color:#fff;">
<th style="width:150px;">Particulars</th>
<th>Details</th>
<th>Amount</th>
</tr>
<tr>
<td>Opening Balance</td>
<td></td>
<td>500000</td> <!-- till not dynamic -->
</tr>
<?php
$total = 0;
$se = "SELECT * FROM group";
$res = mysql_query($se) or die (mysql_error());
while ($row = mysql_fetch_array($res))
{
?>
<tr>
<td colspan="3" style="font-weight:bold;"><?php echo $row['group']; ?></td>
</tr>
<tr>
<?php
$group_total = 0;
$se1 = "SELECT ac_head, amount FROM `acheads` WHERE `j_Id` = '".$row['id']."'";
$res1 = mysql_query($se1) or die (mysql_error());
while ($row1 = mysql_fetch_array($res1))
{
$group_total = $group_total + $row1['amount'];
?>
<td><?php echo $row1['ac_head']; ?></td>
<td><?php echo $row1['amount']; ?></td>
<td> </td>
</tr>
<?php
}
echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}
?>
</table>
</code>

Categories