How to group a column and skip repeating elements - php

I want to group a category column from my table which have repeating rows for related items.With the following code i tried, Its repeating last category for all items.
I tried this code:
$category = '';
while($row = $query->fetch(PDO:: FETCH_ASSOC)){
$allitems[] = $row;
if($row['category'] != $category){
$cat = '<tr>
<td>'.$row['category'].'</td>
</tr>';
$category = $row['category'];
}
}
foreach($allitems as $item){
echo $cat;
echo '<tr>
<td>'.$item['item'].'</td>
</tr>';
}
Sample data from table:
$sample_data = [{"category":"Fruits","item":"Apple"},{"category":"Fruits","item":"Banana"},
{"category":"Fruits","item":"Orange"},{"category":"Vegetables","item":"Tomato"},
{"category":"Vegetables","item":"Onion"},{"category":"Vegetables","item":"Pumpkin"}]
Desired output:

You just need to compare the category of the previous item, to that of the current one - and if they differ, you output the category first. (Your data needs to be properly sorted by category to begin with.)
$sample_data = json_decode('[{"category":"Fruits","item":"Apple"},{"category":"Fruits","item":"Banana"},
{"category":"Fruits","item":"Orange"},{"category":"Vegetables","item":"Tomato"},
{"category":"Vegetables","item":"Onion"},{"category":"Vegetables","item":"Pumpkin"}]', true);
echo '<table>';
$previous_category = null;
foreach($sample_data as $item) {
if($previous_category !== $item['category']) {
echo '<tr><td><b>' . $item['category'] . '</b></td></tr>';
}
echo '<tr><td>' . $item['item'] . '</td></tr>';
$previous_category = $item['category'];
}
echo '</table>';

You can do that in your SQL query with using GROUP BY:
SELECT category, item FROM table_name GROUP BY category, item;
And you will get exactly what you want. A list of items grouped by category and avoid repeating items.

Related

Populate 2 column HTML table with a single PHP result set

Please forgive me as I am beginner in PHP.
I am trying to populate 2 two column table with inventory information. With my current code, I have the below image:
I would like to have two stores next to each other in this table. i.e Store Number 2 and Store Number 7 on the table row in the table, then Store 10 and 11 on the same row, and so on. Below is the code I am using so far to achieve this:
global $wpdb;
$result = $wpdb->get_results(
$wpdb->prepare( "
SELECT STORE_NAME,REPLACE(STORE_NAME, ' ', '-') as STOREURL, INVENTORY, STORE_NUMBER FROM StoreInventory
WHERE SKU = %s",
$product_sku
)
);
if ($result){
echo '<table class=\'inventory\'>';
foreach($result as $row) {
echo '<tr><td><div><a href=\'https://mystore.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>Store Number: ' . $row->STORE_NUMBER . '</a><br/>' .$row->INVENTORY. ' on hand. </div></td></tr>';
}
echo '</table>';
} else {
echo '<table class=\'nostock\'><td>This item is out of stock, check back later for updated information!</td></table>';
}
The problem I am having is that I am looping through the record set row by row, and adding the data to each row. I have attempted to have a loop inside a loop, which will not give me the correct results.
Is it possible to split the result set into two multi-dimensional arrays and loop through each one separately then add them to the table? Can I call out a specific row with a counter within the loop?
Any advise or direction would be a great help.
A relatively simple approach would be to create a dummy variable to store the column. EX:
$column_number = 0;
echo '<table class=\'inventory\'><tr>';
foreach($result as $row) {
echo '<td><div><a href=\'https://mystore.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>Store Number: ' . $row->STORE_NUMBER . '</a><br/>' .$row->INVENTORY. ' on hand. </div></td>';
$column_number += 1;
if ($column_number == 2) {
echo '</tr><tr>';
$column_number = 0;
}
}
echo '</tr></table>';
Final answer based on ARubiksCube's answer:
if ($result){
$column_number = 0;
echo '<table class=\'inventory\'><tr>';
foreach($result as $row) {
echo '<td \' width=\'50%\' ><div><a href=\'https://shopliquornl.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>' . $row->STORE_NAME . '</a><br/>' .$row->INVENTORY. ' on hand</div></td>';
$column_number += 1;
if ($column_number == 2) {
echo '</tr><tr>';
$column_number = 0;
}
}
echo '</table>';
} else {
echo '<table class=\'nostock\'><td>This item is out of stock, check back later for updated information!</td></table>';
}

How to do an inner loop of records in php

I am learning PHP and I want to know how to do an inner loop or nested loop with records in PHP. I will appreciate if somebody explains me how to do this type of loop in PHP.
I have this table in MySQL
[]
I just want to create a report table in PHP that looks like this:
So far, I know how to do a current loop with the code below but how could I do in the same loop and inner loop to show dishes like table above. I know how to create the format (table, spaces, etc) I just need the PHP logic to do this in the best way.
<?php
do { ?>
Table in here
<?php } while ($selecc = mysqli_fetch_assoc($conmenu)); ?>
The clean/direct approach should be something like this:
SELECT Client, Option, GROUP_CONCAT(Dish SEPARATOR ', ')
FROM table_name
GROUP BY Client, Option
Then with your data already grouped and glued together, just print your strings in a single loop of the result set.
Using a do {} while () is of no benefit here.
Here's an untested snippet...
if ($conmenu) {
echo '<table>';
echo '<tr><td>Client</td><td>Option</td><td>Dishes</td></tr>';
while ($row = mysqli_fetch_assoc($conmenu)) {
echo '<tr><td>' , implode('</td><td>', $row) , '</td></tr>';
}
echo '</table>';
}
For the record:
The maximum permitted result length in bytes for the GROUP_CONCAT() function. The default is 1024.
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_group_concat_max_len
Also, $conmenu is iterable, so you could use a foreach instead of a while loop.
foreach ($conmenu as $row) { ...
If you want to do it the hard way...
SELECT Client, Option, Dish
FROM table_name
ORDER BY Client, Option
Then... (untested)
if ($conmenu) {
echo '<table>';
echo '<tr><td>Client</td><td>Option</td><td>Dishes</td></tr>';
$group = null;
foreach ($conmenu as $row) {
if ($group !== $row['Client'] . '_' . $row['Option']) {
if ($group !== null) {
echo '</td></tr>';
}
echo '<tr><td>' , implode('</td><td>', $row);
} else {
echo ", " , $row['Dish'];
}
$group = $row['Client'] . '_' . $row['Option']; // update group
}
echo '</td></tr>'; // close final iteration
echo '</table>';
}

PHP generate rowspan dynamically

I have following item list fetch from table, I need to add dynamic rowspan at the end of the row if item is from same supplier, but I have no idea how to work with this.
I tried:
foreach($items as $item){
/*get total num for rowspan*/
$group = $buyer->get_total_rowspan($obj->id, $obj->supplier);
echo '<tr>
<td>$item->id</td>
<td>$item->name</td>
<td>$item->supplier</td>';
if($group->countRow > 1){
<td rowspan="$group->countRow"><a>Manage</a></td>
}
if($group->countRow > 1){
echo '<td rowspan="'.$group->countRow.'"><a>manage</a></td>';
}else{
echo '<td><a>test</a></td>';
}
echo '</tr>';
}
but cell Manage will always appear at every row with mess format.
the idea results that I want:
You can try something like that:
$lastId = null;
foreach($items as $item){
/*get total num for rowspan*/
$group = $buyer->get_total_rowspan($obj->id, $obj->supplier);
echo '<tr>
<td>$item->id</td>
<td>$item->name</td>
<td>$item->supplier</td>';
if($lastId != $group->Id){
<td rowspan="$group->countRow"><a>Manage</a></td>
}
echo '</tr>'
$lastId = $group->Id;
}
Everytime there is a new group you can set the $i back to 0.

create dropdown menu on each loop assign database value to option element

Im working on a page for a sports team where the coach can select his team. What im trying to do is:
1) Print different positions
2) Assign next to position name, players who are ONLY relevant to the position. (i.e if the positions name is flanker only flankers should be displayed in the drop-down menu)
My logic for the above problem is:
Assign position names to array called $position Loop over array
querying database, with position having a different value after each
loop.
Selected players that match the position gets assigned to an array
called $playerName
Print the $position variable
Create dropdown menu for each position
assign value from $playername array to the option element in the drop
down menu.
Now there should be different positions with dropdown menus, next to
them, containing player names relevant to position.
//create position array
$position = array(
"THP",
"HKR",
"LH",
"LK4",
"LK5",
"FLH"
);
echo '<form name="slectPlayers" method="post">';
foreach ($position as $pposition) {
$result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info` WHERE `position` = '$pposition'") or die(mysql_error());
while ($row = mysql_fetch_array($result)) { //create arrays
$id[] = $row['player_id'];
$playerName[] = $row['name'];
$playerLastName[] = $row['surname'];
// print position and open select element
foreach ($position as $playerPosition) {
print $playerPosition;
echo '<select>';
foreach ($playerName as $name) { //assign playername to position element
echo '<option>' . $name;
'</option>';
echo '</select>';
echo '<br />';
} //close assign player nae to position loop
} //end print position and open select loop
} //end while loop
} //end opening for each loop
echo '</form>';
unfortunately for me either my logic is wrong or my code is incorrect. This is the ouput I get: (Note only the name Tendai is displayed in all dropdown meus no other names appear)
Ive been struggling with this one all morning if someone could point me in the right direction it would be greatly appreciated
(note to modirators the picure above does not contain real names and is only a fictional database)
This could be your problem
foreach ($position as $pposition) {
$result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info` WHERE `position` = '$pposition'") or die(mysql_error());
while ($row = mysql_fetch_array($result)) { //create arrays
$id[] = $row['player_id'];
$playerName[] = $row['name'];
$playerLastName[] = $row['surname'];
// print position and open select element
print $pposition;
echo '<select>';
foreach ($playerName as $name) { //assign playername to position element
echo '<option>' . $name;
'</option>';
echo '</select>';
echo '<br />';
} //close assign player nae to position loop
} //end while loop
} //end opening for each lo
I have removed foreach ($position as $playerPosition) {
You can do it like this. the logic is something different
function get_players_by_position($position)
{
$query = "SELECT
`player_id`,
`name`,
`surname`
FROM `player_info`
WHERE `position` = $position
ORDER BY name ";
$result = mysql_query($query);
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}
return $data;
}
foreach($position as $pposition){
$data = get_players_by_position($pposition);
echo $pposition;
echo '<select>';
foreach($data as $row){
echo '<option '.$row['id'].'>' . $row['name'].'</option>';
}
echo '</select>';
echo '<br />';
unset($data);
}

querying database with array variable [duplicate]

This question already has answers here:
create dropdown menu on each loop assign database value to option element
(2 answers)
Closed 9 years ago.
Im working on a page for a sports team where the coach can select his team. What im trying to do is:
1) Print different positions
2) Assign next to position name, players who are ONLY relevant to the
position. (i.e if the positions name is flanker only flankers should
be displayed in the drop-down menu)
My logic for the above problem is:
Assign position names to array called $position Loop over array
querying database, with position having a different value after each loop.
Selected players that match the position gets assigned to an
array called $playerName
Print the $position variable
Create dropdown menu for each position
assign value
from $playername array to the option element in the drop down
menu.
Now there should be different positions with dropdown menus,
next to them, containing player names relevant to position.
//create position array
$position = array(
"THP",
"HKR",
"LH",
"LK4",
"LK5",
"FLH"
);
echo '<form name="slectPlayers" method="post">';
foreach ($position as $pposition) {
$result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info` WHERE `position` = '$pposition'") or die(mysql_error());
while ($row = mysql_fetch_array($result)) { //create arrays
$id[] = $row['player_id'];
$playerName[] = $row['name'];
$playerLastName[] = $row['surname'];
// print position and open select element
foreach ($position as $playerPosition) {
print $playerPosition;
echo '<select>';
foreach ($playerName as $name) { //assign playername to position element
echo '<option>' . $name;
'</option>';
echo '</select>';
echo '<br />';
} //close assign player nae to position loop
} //end print position and open select loop
} //end while loop
} //end opening for each loop
echo '</form>';
unfortunately for me either my logic is wrong or my code is incorrect. This is the ouput I get: (Note only the name Tendai is displayed in all dropdown meus no other names appear)
If been struggling with this one all morning if someone could point me in the right direction it would be greatly appreciated
(note to modirators the picure above does not contain real names and is only a fictional database)
You can do it like this. the logic is something different
function get_players_by_position($position)
{
$query = "SELECT
`player_id`,
`name`,
`surname`
FROM `player_info`
WHERE `position` = $position
ORDER BY name ";
$result = mysql_query($query);
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}
return $data;
}
foreach($position as $pposition){
$data = get_players_by_position($pposition);
echo $pposition;
echo '<select>';
foreach($data as $row){
echo '<option '.$row['id'].'>' . $row['name'].'</option>';
}
echo '</select>';
echo '<br />';
unset($data);
}
In your original code, you could remove the two foreach loops in the while loop. Use $pposition in place of $playerPosition and your code should work as expected. Here's your code, with my suggested changes, in your procedural style:
$position = array(
"THP",
"HKR",
"LH",
"LK4",
"LK5",
"FLH"
);
echo '<form name="selectPlayers" method="post">';
foreach ($position as $pposition) {
$result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info`
WHERE `position` = '$pposition'") or die(mysql_error());
echo '<select name="'. $pposition . '">';
while ($row = mysql_fetch_array($result)) {
print $pposition;
echo '<option value="' . $row['player_id'] . '">'.
$row['name'] .' '. $row['surname'].
'</option>';
} //end while loop
echo '</select>';
echo '<br />';
} //end opening for each loop
echo '</form>';

Categories