Table creation in php and html - php

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.

Related

join in if loop to join to values

trying to join to 2 xml files with the same value , but when i echo the result it separates the value .
foreach(glob("tarea/*.{assemble.xml,report.xml}", GLOB_BRACE) as $filename) {
$Keys =simplexml_load_file($filename);
foreach($Keys as $Key) {
$key1 = $Keys->ProductKeyID;
$pk = $Keys->ProductKey;
$key2 = $Keys->ProductKeyID;
$serial = $Keys->SerialNumber;
if ($key1 == $key2) {
echo "<tr>";
echo "<td>" .$pk."</td>";
echo "<td>" .$key1."</td>";
echo "<td>" .$key2."</td>";
echo "<td>" . $serial."</td>";
echo "</tr>";
break;
}
}
}
is there any way of putting something like
if key1 = key2 join the together
this is my result im hoping to put all in one line .
ProductKey ProductKeyID ProductKeyID SerialNumber
1234 3305492290985 3305492290985
3305492290985 3305492290985 23

Fetch First record in $key as $value assoc_arr

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.

Show specific fields in array in php?

echo "<table>";
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
echo "<tr>\n";
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
I want to show only the first 4 fields in the table. I am decoding a multidimensional array in php. I
Try like
$cnt = 0;
foreach($arr['chart_data'][$x] as $key=>$val) {
if($cnt++ < 4) {
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
} else {
break;
}
}

Placing another array in my table

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>";

Print query with loops in it and condition

I have a foreach function which permits me to print fields in a database,
I want to do possible that if $row[value]="photo" don't just print the $row[value] but another thing.
Can I do it?
The code is this:
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
echo "<td>" . $row[$value] . "</td>";
}
I need to have a condition that if $row[value]=photo to echo another thing not: echo "<td>" . $row[$photo] . "</td>";
Any example?
Since i could not get what to print for if($row['value'] == photo) check, so add that code yourself in the code written below.
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}
This doesn't need to be complex.
foreach ($result as $row) {
if ($row['value'] === 'photo') {
// print "another thing"
} else {
// print "something"
}
}
Sure, just include an if statement with that. Your code will be similar to the code below:
$arr = array("one", "two", "three");
foreach ($arr as $value) {
if($value == "one")
echo "Not gonna print one.";
else
echo "Value: $value<br />\n";
}
Using your code, you can see if the value is not photo, echo it.
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
if($row[value] == "photo") {
echo "some other thing";
} else {
echo "<td>" . $row[$value] . "</td>";
}
}
}
If you want to print img src if the field is "photo" field (key) then
Try this :
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($key == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}

Categories