PHP generate rowspan dynamically - php

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.

Related

How to group a column and skip repeating elements

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.

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>';
}

PHP foreach while loop how to sum output

Query returns sum of elements group by operator (selected by checkbox).
How to sum up all that values? I tried using array_sum() but didn't worked or maybe i am not using this function correct.
Thank you
<?php
if(isset($_POST['delete']))
{
$ziua=$_POST["date"];
}
else
{
$ziua=date('Y-m-d');
}
if(isset($_POST['delete']))
{//check to see if the delete button has been pressed
if(isset($_POST['box']))
{ //check to see if any boxes have been checked
$num = 0;//used to count the number of rows that were deleted
$box = $_POST['box'];
foreach ($box as $key =>$val)
{ //loop through all the checkboxes
$num++;
$sqldel=" SELECT U.username , SUM(L.geometrie1) A from list L,users U where L.user_id='$val'
and L.date_posted like '%$ziua%' AND L.user_id=U.id group by U.username ";//delete any that match id
$resdel=mysql_query($sqldel);//send the query to mysql
while($row = mysql_fetch_array($resdel))
{
Print "<tr>";
Print '<td align="center">'. $row['0']. "</td>";
Print '<td align="center">'. $row['1']. "</td>";
Print "</tr>";
}
}
}
}
?>
<!-- end snippet -->
Before you loop add
$total=0;
In your loop add
$total += $row[1];
Then echo it after the loop

SQL Image Database assembling rows into HTML cells and rows

I have a database of images which I want to output as a gallery ...
I want to display each image in its own cell <td>image1</td> and limit the number of cells to 7 per row.
<tr>
<td>image1</td><td>image2</td><td>image3</td><td>image4</td><td>image5</td><td>image6</td><td>image7</td>
</tr>
Then a new row would be created and continue to output all the images.
<tr>
<td>image8</td>
and so on ..
I know how to do a query, but I am lost as to how to assemble the rows into the format I am looking for.
Can anyone please help me it would be greatly appreciated. Thanks.
First run your query, then get the mysql_fetch_assoc in a variable. Then echo your beginning tags for the table and tr. Then create a foreach loop of the query assoc, as another variable such as row, and echo $row['image'] in a td. This will add a new td with the image for every entry in the database. Then echo your closing tags for the tr and table.
$query = mysql_query("SELECT image FROM table_name");
$query_a = mysql_fetch_assoc($query);
echo "<table><tr>"
foreach ($query_a as $row)) {
echo "
<td>" . $row['image'] . "</td>
";
}
echo "</tr></table>";
Not tested, but try something like this
<table>
<?php
// make sure there is at least 1 row first
$cnt = 0;
$while($row = mysql_fetch_assoc($query))
{
if(++$cnt == 1) {
echo '<tr>';
} elseif($cnt % 7 == 0) {
echo '</tr><tr>';
}
// show images in <td>'s
echo "<td>" . $row['image']. "</td>";
}
echo '</tr>';
?>
</table>
Keep it simple:
Query the database to get the desired information, loop through the results, construct a new row come each new loop.
$sql = "Select * from table";
mysql_query($sql) or die(mysql_error());
if(mysql_num_rows !=0)
{
// Found at least one record, create table
echo "<table>";
echo "<tr>";
while($row = mysql_fetch_assoc($sql)
{
$cell_count = 0;
// Create a new row for each record found
echo "<td>" . $row['image_column']. "</td>";
$cell_count++;
if($cell_count == 7)
{
echo "</tr>";
echo "<tr>";
$cell_count = 0;
}
}
// Close the table
echo "</tr>";
echo "</table>";
}

PHP/MySQL Output Data in TD's

How would I go about imposing a restriction on the number of HTML table columns when querying the database.
For example, I have a MySQL table with the following values:
myTable:
id color
1 red
2 blue
3 green
4 pink
5 purple
And when I run my query, instead of showing all rows in traditional table rows, e.g.
<table>
<?php
while {
echo "<tr><td>$row['color']</td></tr>;
}
?>
</table>
Instead, I would like to impose something where, every three td's, a new tr is created.
For example, it would output something like this:
<table>
<tr>
<td>red</td>
<td>blue</td>
<td>green</td>
</tr> <-- Notice how after 3 columns, a new table row is created.
<tr>
<td>pink</td>
<td>purple</td>
</tr>
</table>
Any way to achieve this?
In order to achieve this, you can use a combination of a counter and a modulus (%) operator:
<table>
<?php
$count = 0;
while($row = mysql_fetch_array($results)) {
$outputTr = ($count % 3) == 0;
if($outputTr) echo '<tr>';
echo '<td>' . $row['color'] . '</td>';
if($outputTr) echo '</tr>';
$count++;
}
?>
</table>
To achive this, put in a simple counter that resets every 3 table datas.
<?php
echo "<table><tr>";
$count = 0;
foreach($data as $key => $color)
{
if($count == 3)
{
$count = 0;
echo "</tr><tr>";
}
$count++;
echo "<td>".$color."</td>";
}
echo "</tr></table>";
?>

Categories