how to show last row of SQL query result in (php) <tfoot> and exclude in <tbody>? - php

My database colleague built a couple of views and SP's for me so I can focus on html/php.
One is a query that shows a "total" row at the bottom of the table.
I want to:
Exclude this last row on my <tbody>
Show this single row in my <tfoot>
Why don't I just show everything in <tbody> since the totals row will always show at the bottom anyway? Because I want my <tfoot>'s <td>'s colspan to be different, for presentation purposes.
How can I do this using php alone?
I'm using basic PDO:
foreach($table as $row) {
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}

This should work as long as the last row is not exactly the same as any other row.
$last_row = end($table);
echo '<tbody>';
foreach ($table as $row) {
if ($row === $last_row) continue;
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}
echo '</tbody>';
echo '<tfoot>
<tr>
<td>'.$last_row['Item'].'</td>
<td>'.$last_row['Amount'].'</td>
</tr>
</tfoot>';
If it's possible that the last row is not unique you can use the key instead:
$last_key = count($table) - 1;
echo '<tbody>';
foreach ($table as $key => $row) {
if ($key === $last_key) continue;
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}
echo '</tbody>';
echo '<tfoot>
<tr>
<td>'.$table[$last_key]['Item'].'</td>
<td>'.$table[$last_key]['Amount'].'</td>
</tr>
</tfoot>';

$total = 0;
foreach($table as $row) {
$total += $row['Amount'];
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}
echo '<tr>
<td>Total:</td>
<td>'.$total.'</td>
</tr>';
You don't need to use thead or tfoot if you do not intend to use them to style your content or in some cases SEO purposes.

Related

Quantity of every product outputs like 123. How can i put this in a table row table data

I am having a problem to convert my $quantity_total which is as example (113) from 3 different products.
I want it to be in a table like below.
I have been trying to use chunk_split and explode but if i was able to succeed in that. I wouldn't be able to make it dynamic.
<table>
<tr>
<th>Quantity</th>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
$total=0;
$item_count=0;
$arr = array();
$quantity_all = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$quantity_all .=$quantity;
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4></div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;
$_SESSION['total'] = $total;
$_SESSION['item-count'] = $item_count;
}
$_SESSION['quantity-all'] = $quantity_all;
Is this possible? And i need it to be dynamic. So if it were 10 different quantities. It would make 10 table rows.
I hope someone can help me, would really appreciate it a lot! It's the last thing to finish my e-commerce webshop.
As mentioned in my comment, I don't think this is good solution... but the function you're looking for is str_split. https://stackoverflow.com/a/9814389/296555
http://sandbox.onlinephpfunctions.com/code/0bbee53cafafc0d5e8954e07d0abc2c86c6c89a8
<?php
$rows = '156165165489465131';
echo '<table>';
echo '<tr><th>Quantity</th></tr>';
foreach (str_split($rows) as $row) {
echo "<tr><td>$row</td></tr>";
}
echo '</table>';
I dont know if this Is what you want, but you can try something like:
<table>
<tr>
<td>Quantity</td>
</tr>
<?php
$characters = str_split( (string) $quantity_total);
foreach($characters as $char){
echo "
<tr>
<td> $char </td>
</tr>
";
}
?>
</table>

Display data from SQL query in an HTML table in PHP

I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.
I'm completely new to PHP, so any hints on how to solve this will be much appreciated!
echo '<table>';
echo '<tr>';
echo '<th>';
echo '<td>Word</td>';
echo '<td>Frequency</td>';
echo '</th>';
echo '</tr>';
$response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");
while ($row = $response->fetch())
{
echo '<tr>';
echo '<td>'.$row['word'].'</td>';
echo '<td>'.$row['freq'].'</td>';
echo '</tr>';
}
echo '</table>';
$response->closeCursor();
A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.
echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
I prefer combining php and html
<table >
<thead>
<tr>
<th >Word</th>
<th >Frequency</th>
</tr>
</thead>
<?php
$response = $db->query("SELECT * FROM frequencies WHERE freq
BETWEEN 900 AND 910 ORDER BY freq");
?>
<tbody>
<?php
while ( $row = $response->fetch()) {
?>
<tr>
<td><?php echo $row['word']; ?></td>
<td><?php echo $row['freq']; ?></td>
</tr>
<?php }
$response->closeCursor();
?>
</tbody>
</table>

Implement a multiple column table using foreach loop

I want to implement a logic for creating a three column table using foreach loop. A sample code will look like this.
$array = ['0','1','2','3','4','5','6'];
$table = '<table class="table"><tbody>';
foreach($array as $a=>$v){
//if 0th or 3rd???????????wht should be here?
$table .= '<tr>';
$table .= '<td>$v</td>';
//if 2nd or 5th??????????and here too???
$table .= '</tr>';
}
$table = '</tbody></table>';
Any ideas?
Expected output is a simple 3X3 table with the values from the array
Use this you may looking for this
<?php
echo('<table><tr>');
$i = 0;
foreach( $array as $product )
{
$i++;
echo '<td>'.$product .'</td>';
if($i % 3==0)
{
echo '</tr><tr>';
}
}
echo'</tr></table>';
?>
Result Here:
<table>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>
<tr>
<td>data4</td>
<td>data5</td>
<td>data6</td>
</tr>
</table>
This should work for you:
(See that I added a tr at the start and end before and after the foreach loop. Also I changed the quotes to double quotes and made sure you append the text everywhere.)
<?php
$array = ['0','1','2','3','4','5','6'];
$table = "<table class='table'><tbody><tr>";
//^^^^ See here the start of the first row
foreach($array as $a => $v) {
$table .= "<td>$v</td>";
//^ ^ double quotes for the variables
if(($a+1) % 3 == 0)
$table .= "</tr><tr>";
}
$table .= "</tr></tbody></table>";
//^ ^^^^^ end the row
//| append the text and don't overwrite it at the end
echo $table;
?>
output:
<table class='table'>
<tbody>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
</tr>
</tbody>
</table>
Here is an easy solution with array_chunk():
<?php
$array = array('0','1','2','3','4','5','6');
$d = array_chunk($array, 3);
$table = "<table border='1' class='table'><tbody>";
foreach($d as $v)
$table .= "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";
$table .= "</tbody></table>";
echo $table;
?>
Here is an easy solution with incrementing that works with a dynamically generated table like the one in Magento product view page to help display product attributes in two table columns preceded by attribute label -> practically we have 4 table columns together with attribute labels. This is useful for products with multiple attributes that take a long page to display.
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="table table-bordered table-hover" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php $i = 1; ?>
<tr>
<?php foreach ($_additional as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<th style="width: 20%;"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data" style="width:20%;"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
<?php $i++; if($i > 1 && ($i & 1) ) echo "</tr><tr>";?>
<?php } ?>
<?php endforeach; ?>
</tr>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>

mysql query to display results in repeating columns

I am trying to build a comparison table using mysql query and php.
I would like the result to be displayed in a columns, like this:
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="151" scope="col">product</td>
<td width="89" scope="col">product1</td>
<td width="78" scope="col">product2</td>
<td width="77" scope="col">product3</td>
</tr>
<tr>
<td>type</td>
<td>type2</td>
<td>type3</td>
<td>type5</td>
</tr>
<tr>
<td>size</td>
<td>size2</td>
<td>size1</td>
<td>size4</td>
</tr>
<tr>
<td>price</td>
<td>4.99</td>
<td>3.99</td>
<td>3.59</td>
</tr>
</table>
but I can only get the table to show the results - not a row title too (i.e. I want the first column to show 'product', 'type', 'size', 'price'.
The code I have so far is
<?php
// query the database
$result = mysql_query($query_getproducts);
// cols we are interested in (from the SQL query)
$cols = array(
'product',
'type',
'size',
'price',
);
// initialize rotated result using cols
$rotated = array();
foreach($cols as $col) {
$rotated[$col] = array();
}
// fill rotated array
while(($row = mysql_fetch_assoc($result)) !== false) {
foreach($cols as $col) {
$rotated[$col][] = $row[$col];
}
}
// echo html
echo "<table border=1 width=473>";
echo "<tr>";
echo "</tr>";
foreach($rotated as $col => $values) {
echo "<tr>";
foreach($values as $value) {
echo "<td> " . htmlentities($value) . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
hope someone can help.
First of all mysql_* functions are deprecated. You should use PDO or Mysqli.
If you want table headers static ie you want to show table header as "Product,Type,Size, Price"
Then use
<tr>
<th>Product</th>
<th>Type</th>
<th>Size</th>
<th>Price</th>
</tr>
Then if your should use mysql_fetch_assoc which returns associative array with column name as there key. You can use that array and print the result using loop.
eg:
<?php
$rs=mysql_query($query);
while($row=mysql_fetch_assoc($rs) ){
?>
<tr>
<td><?php echo $row['keyname']?></td>
.....
.....
</tr>
<?php
}
?>
try like this ,
echo "<table border=1 width=473>";
echo " <tr>
<th>Product Name</th>
<th>Description</th>
<th>Product Size</th>
<th>Price</th>
</tr>";
foreach($rotated as $col => $values) {
echo "<tr>";
foreach($values as $value) {
echo "<td> " . htmlentities($value) . "</td>";
}
echo "</tr>";
}
echo "</table>";

Mysql fetch array, table results 2

I've recently posted a similar question but that was to create one row of cells across and after it reaches 3 columns, it creates a new row, so I can have 3 columns of infinite rows. That was solved.
Now what I need is this (GIVEN A and B should be records from the database using $row = mysql_fetch_array($results) so basically it would be something like $row['username']; for A and B ).
<tr>
<td><img src="images/ava/A.png" /></td>
<td>A</td>
<td width="2px" rowspan="3"></td>
<td><img src="images/ava/B.png" /></td>
<td>B</td>
</tr>
<tr>
<td><div class="gauge"><div class="innergauge"></div>A</div></td>
<td><div class="gauge"><div class="innergauge"></div>B</div></td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
As what you can see, the record I got from my database has to fill in to this format, where A is one record and B is another, then if there is record C, this whole thing should repeat again to form a second row.
Build each section as you go, combine them at the end. Here's how you do sections 1 and 3, the middle section is left as an exercise:
$section1 = '<tr>';
$section2 = // start $section2
$section3 = '<tr>';
$i = 0;
while($row = mysql_fetch_array(...)){
if($i > 0){
// won't happen the first time through the loop
$section1.= '<td width="2px" rowspan="3"></td>';
}
$section1 .= '<td><img src="images/ava/'.$row['username'].'"/></td>';
$section1 .= '<td>'.$row['username'].'</td>';
// now do $section2
$section3 .= '<td>'.$row['username'].'</td>';
$i++;
}
$section1 .= '</tr>';
// finish $section 2
$section3 .= '</tr>';
// now output
echo $section1 . $section2 . $section3;

Categories