Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My table is messed when I tried to create the HTML table . Here is my code.
$data['items'] = array(
array(
'des' => 'item #1',
'price' => 10,
'o' => 2,
't' => 15,
),
array(
'des' => 'item #3',
'price' => 11,
'o' => 4,
't' => 10,
),
);
Here is my proceed to create the table. But it seems totally wrong. Specially the condition checking in foreach in the tr and td...
<table><thead align="left" style="display: table-header-group"><tr><th>
<table><tr> <td>col 1 </td>
<td>col 2 </td>
<td>col 3 </td>
<td>col 4 </td>
<td>col 5 </td>
</tr></table>
</th></tr></thead>
<tbody>
<?php foreach ($data['items'] as $rows) :?>
<tr class="item_row">
<?php
$total = 0;
foreach ($rows as $key => $value) : ?>
<td><?php echo $key+1 ?></td>
<td> <?php echo $value['description'] ?></td>
<td> <?php echo $value['price'] ?></td>
<td> <?php echo $value['o'] ?></td>
<td> <?php echo $value['t'] ?></td>
<?php endforeach;?>
</tr>
<?php endforeach;?>
</tbody>
</table>
The problem I am facing firstly the condition is not right, so at every row it just priting the first value every time.
secondly, obviously the table broke.
Need some help to solve this problem.
You have a loop too many, the inner loop is not necessary and causes warnings as $value is not an array.
Apart from that, you have messed up your table head, adding another table and th tags where they are not necessary.
You need something like:
<table>
<thead align="left" style="display: table-header-group">
<tr>
<th>col 1 </th>
<th>col 2 </th>
<th>col 3 </th>
<th>col 4 </th>
<th>col 5 </th>
</tr>
</thead>
<tbody>
<?php
$total = 0;
foreach ($data['items'] as $rows) :?>
<tr class="item_row">
<td><?php echo ++$total; ?></td>
<td> <?php echo $rows['description']; ?></td>
<td> <?php echo $rows['price']; ?></td>
<td> <?php echo $rows['o']; ?></td>
<td> <?php echo $rows['t']; ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
the code is expected to calculate the total amount of the "Net Earning" row. How do i write a code to do that. The $stmt variable is associated to a query from the database
<div class="block-card-body">
<div class="my-table table-responsive">
<table class="table align-items-center table-flush mb-0">
<thead class="thead-light">
<tr>
<th>Order ID</th>
<th>Amount</th>
<th>Fee</th>
<th>Net Earning</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach ($stmt as $val) {
?>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<td class="text-success">$<?php echo $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1) ; ?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div><!-- end block-card-body -->
Create a variable to use as an accumulator, initialise it to zero.
Then in the loop, calulate the net and add it to your accumulator. How you display it is up to you later.
Also note, I moved the <tr> inside the loop so you get a well formed table row
<tbody>
<?php
$netTotal = 0;
foreach ($stmt as $val) {
?>
<tr>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<?php
$t = $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1);
$netTotal += $t;
?>
<td class="text-success">$<?php echo $t;?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>
Here is my multidimensional array
<?php
$total = array (
array(
"prod_price" => 15,
"quantity" => 3
),
array(
"prod_price" => 8,
"quantity" => 2
)
?>
I'm trying to display all of these in the form of a table
<table>
<tr>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
<?php
foreach($total as $p){
?>
<tr>
<td><?php echo $p["prod_price"];?></td>
<td><?php echo $p["quantity"]; ?></td>
</tr>
<?php
}
?>
</table>
But I have no idea how to multiply quantity and price and display it according to their table like this....
|Price|Quantity|Sub Total|
|:----|:-------|:--------|
|15 | 3 | |
|8 | 2 | |
Table
I would love if anyone could give me suggestions to how I could do that by still using the multidimensional array.
You can simply do this
<table>
<tr>
<td>Price</td>
<td>Quantity</td>
<td>Total</td>
</tr>
<?php foreach($total as $data){?>
<tr>
<td><?php echo $data['prod_price'];?></td>
<td><?php echo $data['quantity'];?></td>
<td><?php echo $data['prod_price'] * $data['quantity'];?></td>
</tr>
<?php }?>
</table>
I want to create a table with rowspan using php and array of values but i am really struggling on to create the below:
<table>
<thead>
<tr>
<th>Month</th>
<th>Tests</th>
<th>Values</th>
<th>Month</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">January</td>
<td>Test A</td>
<td>VAL A</td>
<td rowspan="2">FEB</td>
</tr>
<tr>
<td>Test B</td>
<td>VAL B</td>
</tr>
</tbody>
</table>
using php arrays of values below i used print_r() function to display the array how can i achive to create the above table format using this array of values using php
Array ( [0] => Array ( [month] => January [tests] => Test A,Test B [values] => VAL A,VAL B [month2] => Feb ) )
Try this Code:
<?php //The Array
$arr=array(array(
'month' => "January",
'month2' => 'Feb',
'tests' => "Test A, Test B",
'values' => "VAL A, VAL B"
));
?>
<table>
<thead>
<tr>
<th>Month</th>
<th>Tests</th>
<th>Values</th>
<th>Month</th>
</tr>
</thead>
<tbody>
<?php foreach($arr as $item): ?>
<?php
$test_break=explode(',',$item['tests']);
$values_break=explode(',',$item['values']);
?>
<tr>
<td rowspan="2"><?php echo $item["month"] ?></td>
<td><?php echo $test_break[0]; ?></td>
<td><?php echo $values_break[0]; ?></td>
<td rowspan="2"><?php echo $item["month2"] ?></td>
</tr>
<tr>
<td><?php echo $test_break[1]; ?></td>
<td><?php echo $values_break[1]; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Here is the php code. The functions require, query and render are given to us.
<?php
// configuration
require("../includes/config.php");
$rows = CS50::query("SELECT `symbol`, `shares`, `cash` FROM `portfolios`, `users` WHERE ?", $_SESSION["id"]);
$positions = [];
foreach ($rows as $row)
{
$stock = lookup($row["symbol"]);
$total = ($stock["price"] * $row["shares"]);
if ($stock !== false)
{
$positions[] = [
"name" => $stock["name"],
"price" => $stock["price"],
"shares" => $row["shares"],
"symbol" => $row["symbol"],
"total" => $total,
"cash" => $row["cash"]
];
}
}
// render portfolio
render("portfolio.php", ["positions" => $positions, "title" => "Portfolio"]);
Here is my HTML output
<div id="middle">
<table class="table table-striped">
<thead>
<tr>
<th >Symbol</th>
<th >Name</th>
<th >Shares</th>
<th >Price</th>
<th >TOTAL</th>
</tr>
</thead>
<tbody>
<?php foreach ($positions as $position): ?>
<tr>
<td align="left" ><?= $position["symbol"] ?></td>
<td align="left" ><?= $position["name"] ?></td>
<td align="left" ><?= $position["shares"] ?></td>
<td align="left" ><?= number_format($position["price"], 2) ?></td>
<td align="left" ><?= number_format($position["total"], 2) ?></td>
</tr>
<?php endforeach ?>
<tr>
<td colspan="4" align="left">CASH</td>
<td align="left"><?= number_format($position["cash"], 2) ?></td>
</tr>
</tbody>
</table>
My guess is that there is something wrong with my foreach loop. But im not quite sure, there could also be some faults in my SQL database.
My mySQL database consist of 3 rows user_id, symbol, shares. And for my user_id I have 3 diffrent stocks with like 10 shares each.
Anyone know what could be wrong?
If it runs 7 times it means there are 7 rows returned, the foreach loop is fine. Most likely the issue is here:
$rows = CS50::query("SELECT `symbol`, `shares`, `cash` FROM `portfolios`, `users` WHERE ?", $_SESSION["id"]);
You aren't indicating which column should equal the id, so it's returning all of them likely. Need something like this:
$rows = CS50::query("SELECT `symbol`, `shares`, `cash` FROM `portfolios`, `users` WHERE portfolios.user_id = ?", $_SESSION["id"]);
I'm trying to do this in while loop PHP, number of assignments are unlimited so rowspan should also adjust itself with the number of rows, is there any proper way to do it with minimum numbers of line?
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Assignment No</th>
<th>Student Name</th>
<th>Assignment Marks</th>
<th>Overall Result</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">S1</td>
<td align="center">5</td>
<td rowspan="3" align="center">B Grade</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">S1</td>
<td align="center">8</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">S1</td>
<td align="center">7</td>
</tr>
</tbody>
</table>
Try This:
<? foreach($dataarray as $data)
{?>
<tbody>
<tr>
<td align="center"><? echo $data[0]; ?></td>
<td align="center"><? echo $data[1]; ?></td>
<td align="center"><? echo $data[2]; ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="3" align="center"><? echo $data[3]; ?></td>
<? } ?>
<? } ?>
and put if condition for last column
Assuming that data comes in a linear fashion (ie a plain one dimensional array), and that the grade is not in the array, I'd do something like this:
<?
$sizeofArray = count($data);
$rowspan = floor($sizeofArray/3);
for($arrCnt = 0; $arrCnt < $sizeofArray; $arrCnt +=3)
{?>
<tbody>
<tr>
<td align="center"><?=$data[$arrCnt]; ?></td>
<td align="center"><?=(($arrCnt+1 < $sizeofArray)?$data[$arrCnt+1]:" "); ?></td>
<td align="center"><?=(($arrCnt+2 < $sizeofArray)?$data[$arrCnt+2]:" "); ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="<?=$rowspan?>" align="center"><?=$grade; ?></td>
<? } ?>
<? } ?>
EDIT: added "padding" code, to avoid index out of bounds error
Based on your case, rowspan is different from one student to another. What you would want to do, is group data by student. The number of assignments per student is what will give you rowspan value.
While ... {
$data['student'][] = ['name' => $row -> name]; // all student inf
$data['student][]['assignments'][] = ['id' => $row -> assignement_id];//student assigments
}
Hope this help or put in the right direction to resolve your problem.
use foreach to loop
foreach ($data as $student){
$rowspan = count($student['assignments'];
foreach ($student['assigments'] as $assignment ){
// html table
}
}