how to limit this loop ..just thee loops..thanks for helping
<?php
foreach($section['Article'] as $article) :
?>
<tr>
<td>
<?php
if ($article['status'] == 1) {
echo $article['title'];
}
?>
</td>
<td>
<?php
if($article['status']== 1) {
echo ' '.$html->link('View', '/articles/view/'.$article['id']);
}
?>
</td>
</tr>
<?php
endforeach;
?>
Slice the array.
foreach(array_slice($section['Article'], 0, 3) as $article ):
first, prepare your data
$i = 1;
$data = array();
foreach($section['Article'] as $article ) {
if($article['status']== 1) {
$article['link'] = $html->link('View', '/articles/view/'.$article['id']);
$data[] = $article;
if ($i++ == 3) break;
}
}
$section['Article'] = $data;
then display it
<?php foreach($section['Article'] as $article ): ?>
<tr>
<td><?php echo $article['title'] ?></td>
<td> <?php echo $article['link']?></td>
</tr>
<?php endforeach ?>
This will help if your array is numerically indexed
foreach($section['Article'] as $i => $article ):
if ($i > 3) break;
Otherwise - manually increment the counter:
$i = 0;
foreach($section['Article'] as $article ):
if ($i++ > 3) break;
It'd be easier to use a for() loop to do this, but to answer the question:
<?
$i = 0;
foreach ($section['Article'] AS $article):
if ($i == 3) { break; }
?>
...
<?
$i++;
endforeach
?>
A foreach loop wouldn't be the best if you need to limit it. Try using a for loop.
<?php
for(i=1; i<=3; i++)
{
$article = $section['Article'];
?>
<tr>
<td><?php if($article['status']== 1){echo $article['title'];} ?></td>
<td><?php if($article['status']== 1){echo ' '.$html->link('View', '/articles/view/'.$article['id']);}?></td>
</tr>
<?php } ?>
This code will make the text loop 3 times.
Awesome one must try this one
<?php $count = 0; $pages = get_pages('child_of=1119&sort_column=post_date&sort_order=desc'); foreach($pages as $page) {
$count++;
if ( $count < 50) { // only process 10 ?>
<div class="main_post_listing"> <?php echo $page->post_title ?><br /></div>
<?php
} } ?>
Related
I have a problem with my current "Attendance Project", so I have 2 arrays.
1st array is to show a "workdays"
the 1st array show only workdays in current month ex:April, so the result in my 1st array is (3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28)
2nd array is showing Employee Attendance in current month ex:April, so the result in my 2nd array is (17, 19)
here is my current code :
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th style="width: 200px">Siswa</th>
<!-- <?php for($i = 1; $i < 31; ++$i){?>
<th><?= $i ?></th>
<?php } ?> -->
<?php foreach($workdays as $w){ ?>
<th><?=$w;?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
// for($x = 1; $x < 27; ++$x){
foreach($records as $r){
?>
<tr>
<td style="width: 200px"><?=$r->StudentName;?></td>
<?php
?>
<?php
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } } ?>
</tr>
<?php } ?>
</tbody>
</table>
It will produce :
I want value (17 and 19) will markup the data with yellow background, and the table is NOT out of range.
Any help will appreciate..
Your code seems messy and I'm not gonna try to fix it on what you have, but I'll suggest solution:
1st - run foreach ($workdays as $w) and make header
2nd - run foreach ($workdays as $w) and make table-body like:
foreach ($workdays as $w) {
if (in_array($w, $tanggale)) //if tanggle is the one with 17 and 19
{
//code
}
else
{
//code
}
}
Simply what u can do is combine the 2 arrays in to one and then iterate the combine array as per your requirment.
Check below code for combining the array
<?php
$working_days = array(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28);
$present_days = array(17.19);
$combine_attendence_array = array();
foreach($working_days as $day) {
$combine_attendence_array[$day] = 'Absent';
if(in_array($day, $present_days)) {
$combine_attendence_array[$day] = 'Present';
}
}
?>
This code will create combine array with key as day and value is present or Absent.
Now you can iterate as per your requirement below is the iteration code.
foreach($combine_attendence_array as $day => $value){
if($value == 'Present'){ ?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } ?>
<?php } ?>
I hope this answers solves your question.
Do it in this way
<?php
foreach($tanggale as $t){
if(in_array($t,$workdays)){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } ?>
</tr>
<?php } ?>
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
$color = "#FFF000";
$text = "M";
} else {
$color = "#48C9A9";
$text = "O";
}
}
?>
<td style="background: <?php echo $color; ?>"><?php echo $text; ?></td>
<?php }?>
Glad you are here. I need solution and I am kinda newbie in these things.
Right now I have page looking like this
1.text 2.text 3.text 4.text
but I need it to be like this
1.text 2.text
3.text 4.text
Code I have
<table >
<?php
for ($x = 1; $x <= 2; $x++): ?>
<tr>
<?php while($row = $choices->fetch_assoc()): ?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php endwhile; ?>
</tr>
<?php endfor; ?>
</table>
The for() loop is useless in this, because the while() loop inside it will process every row returned by the query.
You should just use the while loop, and then use a counter to tell whether to start a new <tr>.
<table>
<?php
$counter = 0;
while ($row = $choices->fetch_assoc()) {
if ($counter % 2 == 0) { // Start a new row before event elements
echo "<tr>";
}
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if ($counter % 2 == 1) { // End row after odd elements
echo "</tr>";
}
$counter++;
}
if ($counter % 2 == 1) { // End last row if it only had 1 column
echo "</tr>";
}
?>
</table>
<table >
<?php while($row = $choices->fetch_assoc()):
if(($row['id_var'] % 2) == 1) echo '<tr>';
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if(($row['id_var'] % 2) == 0) echo '</tr>';
endwhile; ?>
</table>
Im using tables to store content that's dynamically loaded. It's for a reservation form which will be responsive. What I'm looking to do is break each table row into two if there are more than 5 columns in order for the mobile version to fit on screen.
I'm sure this can be achieved by extending what I already have but can't get it to work.
Here's my current code:
<table>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach ?>
</tr>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach ?>
</tr>
</table>
I'd need to break the loop for each row tr after 5 loops, then add a new row underneath.
I've been experimenting with
$max_loop = 5;
$count = 0;
But no luck so far.
I prefer to reorganize data:
<?php
$availDates = array();
foreach ($hostel->getAvailableDates() as $date) {
$availDates[] = $date;
}
$maxCols = 5;
$chunked = array_chunk( $availDates, $maxCols );
?>
<table>
<?php
foreach ($chunked as $chunk) {
?><tr>
<?php foreach ($chunk as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach ($chunk as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach; ?>
</tr><?php
}
?>
</table>
Look at the mod operator. It should give you what you need.
if($count % $max_loop == 0)
I hope this may help you. thanks.
<?php
$avDates = $hostel->getAvailableDates();
echo "<table><tr>";
foreach($avDates as $i=>$date){ {
if ($i == $max_loop) {
echo "</tr><tr>";
}
echo "<td>".($date->getAvailable() ? '<b class="avail tick">Available</b>' : '<b class="avail cross">Unavailable</b>')."</td>";
}
echo "</tr></table>";
?>
If the value returned by getAvailableDates is an array, you could use a for loop instead of a foreach, and check if the current index is a multiple of five, so you don't have to keep track of the count variable
$avDates = $hostel->getAvailableDates();
for ($i = 0; $i < count($avDates); $i++) {
$date = $avDates[$i];
//do your staff
//if multiple of five add another tr
if ($i % 5 == 0) {
}
}
I am trying to make a table with 4 rows from a foreach-call.
My problem is, that in the result I get each ID twenty times in the same column.
I'm using this code:
<table width="80%" border="0" cellpadding="10px">
<?php foreach (array_chunk($items, 4) as $row) { ?>
<?php
$i = 0;
foreach ($items as $item):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
} ?>
<tr
<?php echo $class;?>
>
<?php foreach ($row as $item){ ?>
<td>
<?php echo htmlentities ($item['Item']['id']); ?>
</td>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
</table>
Any idea how I could get each ID just once?
You are incrememnting $i at every $item as opposed to every $row
Is this the fix you are looking for?
Edit: Mikel has your fix, add this to fix the row bug (Typical of me to notice that first eck!)
<table width="80%" border="0" cellpadding="10px">
<?php
$i = 0;
$chunkedarray = array_chunk($items, 4);
foreach ($chunkedarray as $row) {
$class = null;
if ($i++ % 2 == 0)
$class = ' class="altrow"';
echo "<tr ".$class.">";
foreach ($row as $item){
echo "<td>";
echo htmlentities ($item['Item']['id']);
echo "</td>";
}
echo "</tr>";
}?>
</table>
i have this multi dimentional array that i want to print into a table having each record/item go into its own row but it goes column wise. this is the output that im getting: http://mypetshopping.com/product.php
ps: the value of $product will by dynamic based on what product is being viewed.
<?php
session_start();
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Hash</th>
<th>Quantity</th>
<th>Size</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<?php
function addCart($product, $quantity, $size,$color) {
$hash = md5($product);
$_SESSION['cart'][$product]['name'] = $product;
$_SESSION['cart'][$product]['hash'] = $hash;
$_SESSION['cart'][$product]['quantity'] = $quantity;
$_SESSION['cart'][$product]['size'] = $size;
$_SESSION['cart'][$product]['color'] = $color;
}
addCart('Red Dress',1,'XL','red');
addCart('Blue Dress',1,'XL','blue');
addCart('Slippers',1,'XL','orange');
addCart('Green Hat',1,'XXXL','green');
$cart = $_SESSION['cart'];
foreach($cart as $product => $array) {
foreach($array as $key => $value) {
?>
<tr>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
</tr>
<?php
}
}
?>
I think your looping code should be written as:
<?php foreach( $cart as $product => $array ) { ?>
<tr>
<?php foreach( $array as $key => $value ) { ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php } ?>
try altering your code to:
foreach($cart as $product => $array) { ?>
<tr>
<?php
foreach($array as $key => $value) {
?>
<td><?=$value;?></td>
<?php
}
?>
</tr>
<?php
}
<?php
echo "<ul>";
for ( $layer = 0; $layer < 3; $layer++ )
{
echo "<li>The layer number $layer";
echo "<ul>";
for ( $row = 0; $row < 3; $row++ )
{
echo "<li>The row number $row";
echo "<ul>";
for ( $col = 0; $col < 3; $col++ )
{
echo "<li>".$shop[$layer][$row][$col]."</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
?>
http://www.webcheatsheet.com/php/multidimensional_arrays.php