I currently use 2 arrays to place values in my table like so:
echo "<table>";
foreach ($products as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
if (!$number){
echo "";
} else {
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
The $product and $number are showing just fine, but my problem is that i need a third column with the $prices in them. Looking like this:
echo "<td>";
echo $price;
echo "</td>";
I made an array for $prices looking like this:
$prices = array(7, 8, 9, 11, 13, 5, 5.50, 6, 0.50, 0.50);
But it gets more complicated.
It needs to be so that $price = $number * $prices.
Meaning that if $number = 5 and $prices = 8 it should place 40 in the $price.
I don't know if this is easy to do or not. But i'd like some help with this since im not really familiar with arrays.
Thanks in advance!
echo "<table>";
$count = 0;
foreach ($products as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
if (!$number){
$count++;
echo "";
} else {
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "<td>";
$price = $prices[$count] * $number;
echo $price;
echo "</td>";
echo "</tr>";
$count++;
}
}
echo "</table>";
Related
I'm basically trying to perform a calculation with the first row and first column however I can't seem to get it working correctly.
function tableCreator($height_arrayy, $weight_arrayy) {
echo "<table><tr><th></th>";
foreach($height_arrayy as $value1) {
echo "<th>$value1</th>";
}
echo "</tr>";
foreach($weight_arrayy as $value2) {
echo "<tr>";
echo "<td>$value2</td>";
foreach($weight_arrayy as $value3) {
foreach($height_arrayy as $value4) {
echo "<td>" . ($value3) / ( pow( ($value4 / 100), 2 ) ) . "</td>";
}
}
echo "</tr>";
}
echo "</table>";
}
Result I am getting:
Desired Result:
you have added an un-necessary foreach loop, update your second foreach loop as below
foreach($weight_arrayy as $value2) {
echo "<tr>";
echo "<th>$value2</th>";
foreach($height_arrayy as $value4) {
echo "<td>" . ($value2) / ( pow( ($value4 / 100), 2 ) ) . "</td>";
}
echo "</tr>";
}
your code will return desired output.
use round() if you want to round upto 2 or 3 decimal points.
I have the Assoc_arr and the values in the array is:-
Array
(
[SIZE] => 8.5x11
[FOLDING] => HalfFold To 4.25x11
[PAPER] => 100lb Gloss Book with Aqueous Coating (C2S)
[COLOR] => full/full
[TURNAROUND] => Standard
)
When i print this it prints all the values with this code:-
echo "<table>";
foreach ($final_array as $key => $value)
{
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table><br><br>";
How can i only fetch the [SIZE] => 8.5x11 record from the assoc_arr.
Try this code
echo "<table>";
echo "<tr>";
echo "<td>";
echo key($arr);
echo "</td>";
echo "<td>";
echo $arr[key($arr)];
echo "</td>";
echo "</tr>";
echo "</table><br><br>";
You can try the following code
echo "<table>";
foreach ($array as $key => $value)
{
if('SIZE' == $key) {
echo "<tr><td>";
echo $key;
echo "</td><td>";
echo $value;
echo "</td></tr>";
}
}
echo "</table>";
You could use array_keys to get the first key and value:
$keys = array_keys($final_array);
$key = $keys[0];
$value = $final_array[$key];
Check with this:
echo "<table>";
foreach ($final_array as $key => $value)
{
if('SIZE' == $key) {
echo "<tr><td>";
echo $key;
echo "</td><td>";
echo $value;
echo "</td></tr>";
} else {
// do something else
}
}
echo "</table><br><br>";
UPDATE
If you want only the value for SIZE key, then no need of any loop. You can get it $final_array['SIZE'], otherwise the above thing will help you.
This question already has answers here:
syntax error in case 'remove' when decrementing [closed]
(3 answers)
Closed 8 years ago.
I'm getting various errors (syntax) on the following php file. The current php file is about onlineshops product-to-cart adding function. I get various errors. The errors start from the first echo and are listed as syntax error, unexpected 'echo'.
If I remove it, I get a syntax error, unexpected ';' on the next line.
Any help will be great. Thanks.
/* display cart */
if ( isset( $_SESSION['cart'] ) )
(
echo "<table border=0 cellspacing=0 cellpadding=0 width='500'>";
$total = 0;
foreach($_SESSION['cart'] as $id => $x)
(
$result = mysql_query("SELECT * from onlineshop WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
$name = $myrow['name'];
$name = substr($name,0,40);
$price = $myrow['price'];
$line_cost = $price * $x;
$total = $total + $line_cost;
echo "<tr>";
echo "<td allign='left'> $name </td>";
echo "<td align = 'right'>X $x <a href='cart.php?id=".$id."&action=remove'> reduce </a></td>";
echo "<td align = 'right'>= $line_cost £";
echo "</tr>";
)
echo "<tr>";
echo "<td align ='right'><br>Total=</td>";
echo "<td align ='right'><b><br> $total £</b></td>";
echo "</tr>";
echo "</table>";
)
else
echo "Cart is empty";
You use wrong braces. Replace () with {} in if statement and foreach loop :
if (isset($_SESSION['cart'])){
echo "<table border=0 cellspacing=0 cellpadding=0 width='500'>";
$total = 0;
foreach($_SESSION['cart'] as $id => $x){
$result = mysql_query("SELECT * from onlineshop WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
$name = $myrow['name'];
$name = substr($name,0,40);
$price = $myrow['price'];
$line_cost = $price * $x;
$total = $total + $line_cost;
echo "<tr>";
echo "<td allign='left'> $name </td>";
echo "<td align = 'right'>X $x <a href='cart.php?id=".$id."&action=remove'> reduce </a></td>";
echo "<td align = 'right'>= $line_cost £";
echo "</tr>";
}
echo "<tr>";
echo "<td align ='right'><br>Total=</td>";
echo "<td align ='right'><b><br> $total £</b></td>";
echo "</tr>";
echo "</table>";
}else
echo "Cart is empty";
You should use {} braces instead of () for if's or loop's content block.
if (isset($_SESSION['cart'])) {
//....
}
instead of
if (isset($_SESSION['cart'])) (
//....
)
And the same to foreach loop.
I'm trying to parse the arrivals table from here [1] and put in into an array to be able to format it and put it into a table.
I did some research here and there, I've got some code from other questions, but I can't make the array and table look as I'd like.
Anyone can help me out?
<?php
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$rowData = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$rowData[] = $cell->innertext;
}
echo "<table>";
echo "<td>";
echo $rowData[0]. " ";
echo "</td>";
echo "<td>";
echo $rowData[1]. " ";
echo "</td>";
echo "<td>";
echo $rowData[2]. " ";
echo "</td>";
echo "<td>";
echo $rowData[3]. " ";
echo "</td>";
echo "<td>";
echo $rowData[4]. " ";
echo "</td>";
echo "<td>";
echo $rowData[5]. " ";
echo "</td>";
echo "<td>";
echo $rowData[6]. " ";
echo "</td>";
echo "<td>";
echo $rowData[7]. " ";
echo "</td>";
echo "<td>";
echo $rowData[8]. " ";
echo "</td>";
echo "</table>";
}
?>
Maybe try putting each row into an array and then each cell into another array. Hopefully, that will do what you want.
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
Note: this solution requires the simple_html_dom.php library. Get it here!
hey I make a simple php table using nested for loop ... and it will be like this...
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
but the problem is , i can not print this value using a loop inside the column .. so what will be the solution ??? please
my code :
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo "MY PROBLEM HERE...I cant print column numbers \n";
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
//echo "MY PROBLEM HERE...I cant print column numbers \n";
echo $col + ($row - 1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
To save several loops:
$rows = 3;
$cols = 5;
$table = '<table border="1">';
for($i=1;$i<=$rows;$i++){
$table .= '<tr><td>'.implode('</td><td>', range($cols*$i-$cols+1,$cols*$i)).'</td></tr>';
}
$table .= '</table>';
echo $table;
It's not $col + $row * 5 it has to be $row - 1
<?php
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo $col + ($row-1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
?>