I just want to create a table on HTML with a PHP loop. So, I try to do this:
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<?php endfor; ?>
<?php for ($j=10; $j >= 1 ; $j--) : ?>
<td><?php echo "Class ". $j . "\n" ;?></td>
<?php endfor; ?>
</tr>
</tbody>
</table>
But, why the output becomes this?
Assuming you have an array like this:
[0] => Array
(
[stud_id] => 1234
[name] => John Doe
[class] => Class 1
)
[1] => Array
(
[stud_id] => 2345
[name] => Jane Doe
[class] => Class 2
)
My table loop will be look like this:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
foreach($array as $data) {
?>
<tr>
<td><?=$data['stud_id']?></td>
<td><?=$data['name']?></td>
<td><?=$data['class']?></td>
</tr>
<?php } ?>
</tbody>
</table>
You must put the tbody contents (tr, td) inside the loop
It's because you've got a loop inside a loop.
Try this instead:
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<td><?php echo "Class ". 10-$i . "\n" ;?></td>
</tr>
<?php endfor; ?>
</tbody>
</table>
Assuming that you really need two nested loops.
You need to move the endfor to the end of the loop, otherwise there will be <tr> before the 2nd loop
So
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<?php for ($j=10; $j >= 1 ; $j--) : ?>
<td><?php echo "Class ". $j . "\n" ;?></td>
<?php endfor; ?>
<?php endfor; ?>
</tr>
</tbody>
</table>
Related
I want to show only 5 records data per page in print pdf. This is my code :
<table border="1" width="100%" cellpadding="10">
<thead>
<tr>
<th>No</th>
<th>Part No</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($items as $row):
?>
<tr>
<td><?php echo $i++ ?></td>
<td><?php echo $row->part_no?></td>
<td><?php echo $row->price ?></td>
</tr>
<?php if ($i % 5 === 1): ?>
<p style="page-break-before: always;"></p>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td><?php echo $total ?></td>
</tr>
</tfoot>
</table>
Data is show correctly but any zero in my table in second and next page, like this image below :
how to use the code below correctly, ?
<?php if ($i % 5 === 1): ?>
<p style="page-break-before: always;"></p>
<?php endif; ?>
$array = array(1,2,3,4,5,6,7,8,9,10,11,12);
foreach ($array as $item){
if ($item == 5) {
break;
}
echo $item;
}
I have solved this, my table is look good now. Thank you :
I changed code like this :
<tbody>
<?php
$i=1;
foreach($items as $row):
?>
<tr>
<td><?php echo $i++ ?></td>
<td><?php echo $row->part_no?></td>
<?php if ($i % 5 == 1) {
echo '<tr><td><div style="page-break-before: always;"></div></td></tr>';
}?>
</tr>
<?php endforeach; ?>
</tbody>
I am using DataTables and I want to add new TR at the end of while loop.
I know we can add <tfoot></tfoot>, but I don't want to add '' because I am filtering data with custom Ajax.
I have tried below code but it's not working:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
Also, when I use <tfoot></tfoot> it's not printable.
Probably your problem is in $numRows which is not defined.
So you can try this:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
$numRows = mysqli_num_rows($Itesres);
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
I have the array of data like:
Array
(
[0] => Array
(
[lot_no] => ["A001","A001","B002"]
[qty] => ["4","5","6"]
[weight] => ["4","5","6"]
[particular] => ["100% cashmere","100% cashmere","20% silk 80% cashmere"]
[remarks] => ["4","5","6"]
)
)
i want to throw this data in table shown in pic table no 1 like of the second pic. How can i do that?
You can iterate the loop of array in view
<?php if (count($detailListDatas ) > 0): ?>
<table>
<thead>
<tr>
<th>Sno</th>
<th>Particular</th>
<th>Lot no</th>
<th>Total Qty</th>
<th>Total Weight</th>
<th>Remark</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i=0;
foreach ($detailListDatas as $row) ?>
<tr>
<td><?php echo $i++?></td>
<td><?php echo $row['particular']; ?></td>
... similarly all the element
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
I tried your answer which didn't gave me the output i needed. Eventually i did it by myself like this
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>S.No</th>
<th>Particulars</th>
<th>Lot no</th>
<th>Total Qty</th>
<th>Total Weight</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php $sn =1;
foreach ($detailListDatas as $key ) {
$json_array['lot_no'] = json_decode($key['lot_no'], true);
$lot_no = $json_array['lot_no'];
$i = count($lot_no);
$json_array['qty'] = json_decode($key['qty'],true);
$qty = $json_array['qty'];
$json_array['weight'] = json_decode($key['weight'],true);
$weight = $json_array['weight'];
$json_array['particular'] = json_decode($key['particular'],true);
$particular = $json_array['particular'];
$json_array['remarks'] = json_decode($key['remarks'],true);
$remarks = $json_array['remarks'];
for ($j=0; $j < $i ; $j++) { ?>
<tr>
<td><?php echo $sn?></td>
<td><?php echo $particular[$j];?></td>
<td><?php echo $lot_no[$j];?></td>
<td><?php echo $qty[$j];?></td>
<td><?php echo $weight[$j];?></td>
<td><?php echo $remarks[$j];?></td>
</tr>
<?php $sn++; } ?>
<?php } ?>
</tbody>
</table>
I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.
I have this code in data.php:
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And this is the desired output:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
Your problem is this line:
<?php foreach ($data as $key=>$row):
You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:
if ($row == 'class1')
You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.
Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
<tr class="<?=$class?>">
<td><?=implode("</td><td>", $row)?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm not sure I understood everything but you can try this
<?php foreach ($data as $key => $row):
$class = '';
if(isset($row['special'])){
$class = 'class="'.$row['special'].'"';
unset($row['special']);
}
?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
If I understand your question correctly, this should do the job.
The result is as you desire, see: Online PHP shell
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if (isset($row["special"])) {
$class = " class='" . $row["special"]. "'";
unset($row["special"]);
} else {
$class='';
}?>
<tr<?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I have a table called subject_scores which has the following fields:
candidateNumber, paperCode, paperNumber, question1, question2, ... , question20
The issue is displaying it in the view (CodeIgniter), here's my code:
<table class="table table-striped table-condensed" id="example">
<thead>
<tr>
<th>Candidate Number</th>
<th>Paper Code</th>
<th>Paper Number</th>
<?php for ($i = 1; $i <= 20; $i++) : ?>
<th><?php echo 'Q'.$i; ?></th>
<?php endfor; ?>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php if (isset($scores)) : foreach ($scores as $row) : ?>
<tr>
<td><?php echo $row->candidateNumber; ?></td>
<td><?php echo $row->paperCode; ?></td>
<td><?php echo $row->paperNumber; ?></td>
<td><?php echo $row->status; ?></td>
<?php for ($i = 1; $i <= 20; $i++) : ?>
<?php if ($row->question.$i == NULL) : ?>
<td><span class="red">NA</span></td>
<?php else : ?>
<td><?php echo $row->question.$i; ?></td>
<?php endif; ?>
<?php endfor; ?>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
From the above code you'll notice I've used something like this $row->question.$i to combine the array value with the increment to create fields from question1 to question20.
How do I do this correctly either in PHP or CodeIgniter?