How To Control Looping HTML Table PHP - php

So, i've table like in picture
I want put looping row person on left side to singgle row select box on right side, like by color. And this is a code table right side.
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1 ; foreach($query as $row) { ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
</tr>
<?php }} ?>
</table>
How to control looping iteration, to make this is happen?
Tanks a lot.

My friend just help to fix my problem,
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1;$count=0; foreach($query as $row) { ?>
<?php if($count % 3 == 0){ ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<?php } ?>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
<?php $count++; if($count % 3 == 0){ ?>
</tr>
<?php }}} ?>
</table>

Related

Codeigniter Print Domp PDF Page Break Limit 5 records per page In table foreach

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>

How to find the sum of column of a table in frontend

I want to calculate the sum of column on frontend and appear the sum in tfoot. Please help me out I'm new to PHP!
<?php
if(is_array($finalArr) && count($finalArr) > 0){
foreach($finalArr as $key => $finalAr){
?>
<tbody>
<tr>
<td> <?php echo $finalAr['date']; ?></td>
<td>$<?php echo $finalAr['deposit'] ?></td>
<td>$ <?php echo $finalAr['fees'] ?></td>
</tr>
<tfoot>
<tr>
<th>Total</th>
<th>$<?php echo 0; ?></th>
<th>$<?php echo 0; ?></th>
</tr>
</tfoot>
You can do like this:
<tbody>
<?php $totalFee = $totalDeposit = 0;
if(is_array($finalArr) && count($finalArr) > 0){
foreach($finalArr as $key => $finalAr){
$totalFee+= $finalAr['fees'];
$totalDeposit+= $finalAr['deposit'];
?>
<tr>
<td> <?php echo $finalAr['date']; ?></td>
<td>$<?php echo $finalAr['deposit'] ?></td>
<td>$ <?php echo $finalAr['fees'] ?></td>
</tr>
<?php }?>
<tfoot>
<tr>
<th>Total</th>
<th>$<?php echo $totalFee; ?></th>
<th>$<?php echo $totalDeposit; ?></th>
</tr>
</tfoot>
<?php } ?>
Hope this will help.

Echoing nested JSON into PHP

I have a JSON feed which I am parsing via PHP. I am having issues getting some nested elements to echo which I would appreciate some assistance on.. I've looked at loads of related posts here but cant seem to get the logic to work on my specific JSON feed. Could someone advise what I am doing wrong?
JSON feed is here > https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json
The elements, I am struggling to parse are the "categories" parent and child nodes of "team", "location" and "commitment".
I was thinking this would work - but it does not...
<?php
$url = 'feed.json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php foreach ($characters as $character) : ?>
<tr>
<td> <?php echo $character['text']; ?> </td>
<td> <?php echo $character['categories'][2]['team'] ?></td>
<td> <?php echo $character['categories'][2]->team ?></td>
<td> <?php echo $character['categories'][1]->location ?></td>
<td> <?php echo $character['categories'][0]->commitment ?></td>
<td> <?php echo $character['descriptionPlain']; ?> </td>
<td> <?php echo $character['applyUrl']; ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
Note, its just the categories children that fail to echo? Also noticed that if I use the full url in the $url variable it all fails? But from local it works??
Any ideas??? Thanks!
It should be:
<td> <?php echo $character['categories']["team"] ?></td>
<td> <?php echo $character['categories']["location"] ?></td>
<td> <?php echo $character['categories']["commitment"] ?></td>
instead. The numeric keys are not present on the array in the data. Also "categories" is not an object, so you cannot use the arrow (->) notation.
EDIT
You get an error because you are trying to access an object, actually you have an array, here is the solution hope it helps :
<?php
$url = 'https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
$nb = count($characters);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php while($nb > 0){
$nb--;
$nb_lists = count($characters[$nb]['lists']);
?>
<tr>
<?php
while($nb_lists > 0){
$nb_lists--;
?>
<td> <?php if(isset($characters[$nb]['lists'][$nb_lists]['text'])){ echo $characters[$nb]['lists'][$nb_lists]['text'];} ?> </td>
<?php } ?>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['location'])) {echo $characters[$nb]['categories']['location'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['commitment'])){ echo $characters[$nb]['categories']['commitment'];} ?></td>
<td> <?php if(isset($characters[$nb]['descriptionPlain'])){echo $characters[$nb]['descriptionPlain']; }?> </td>
<td> <?php if(isset($characters[$nb]['applyUrl'])){echo $characters[$nb]['applyUrl'];} ?> </td>
</tr>
<?php } ?>
</tbody>

Showing data in table row wise by using codeigniter php

How can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>

How to add a class to last table row

I want to add a class to every last <tr> of a main category. I don't want to add the classname to every <tr>. What can I change in my script?
Like this:
<table>
<tr>
<th></th>
</tr>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
<th></th>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
</table>
<table class="data forum">
<? foreach ($this->mainCategories as $mainCategory): ?>
<tr>
<th><strong><?= $this->escape($mainCategory->fcName) ?></strong></th>
<th> </th>
<th><strong>Topics</strong></th>
<th><strong>Laatste topic</strong></th>
</tr>
<? foreach ($mainCategory->getSubCategories() as $category): ?>
<tr>
<td><?= $this->escape($category->getName()) ?></td>
<td><?= $this->escape($category->fcDescription) ?></td>
<? if ($this->escape($category->numTopics) > 0): ?>
<td><?= $this->escape($category->numTopics) ?></td>
<td><?= date_create($this->escape($category->last_topic))->format('d-m-Y H:i') ?></td>
<? else: ?>
<td> </td>
<td> </td>
<? endif ?>
</tr>
<? endforeach ?>
<tr>
<td class="split"></td>
</tr>
<? endforeach ?>
</table>
Grab the count of your $this->mainCategories variable and test for the last one against a counter variable...
$count = count($this->mainCategories;
$current = 0;
<?
foreach ($this->mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
Update:
Let's look at a simplified version as a proof of concept:
<?php
$mainCategories = array("Eins", "Zwei", "Drei");
$count = count($mainCategories);
$current = 0;
foreach ($mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
<td><?= $mainCategory ?></td>
</tr>
<?php endforeach ?>
This produces the following HTML:
<tr>
<td>Eins</td>
</tr>
<tr>
<td>Zwei</td>
</tr>
<tr class="someClass">
<td>Drei</td>
</tr>

Categories