How can I make this foreach line only go through the first result?
<?php foreach ($collection as $product) : ?>
I tried the following, but that does not work. It than does not display any results:
<?php foreach (array_slice($collection, 0, 1) as $product) : ?>
EDIT:
The following works perfect:
<?php
$i = 0;
foreach ($collection as $product) :
if($i < 1) {
Inner content
}
$i++;
endforeach;
?>
Total current code:
<tbody>
<tr>
<td class="image" rowspan="3">
<?php $i = 0; foreach ($collection as $product) : if($i < 1) {?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75) ?>" alt="<?php echo $this->htmlEscape($product->getName()) ?>" width="75" height="75" />
<?php } $i++; endforeach ?>
</td>
<td class="order" colspan="2">order</td>
<td class="exclTax">excl. vat</td>
<td class="inclTax">incl. vat</td>
</tr>
<?php foreach ($collection as $product) : ?>
<tr>
<td class="select"><input type="radio" name="featured_1807" id="featured_1807_1349567899" value="3071895, IM" data-product-id="3071895" data-product-sup="IM"></td>
<td class="title"><?php echo $this->htmlEscape($product->getName()) ?></td>
<td class="price"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
<td class="priceIncl"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
</tr>
<?php endforeach ?>
</tbody>
How can I achieve that?
Why bother with a loop if you are only interested in the first occurance of $collection
You could do this instead and probably not have to change any code you currently have inside your loop
<?php
$product = $collection[0];
Code you had in the loop
?>
If its not a numeric key then you can use
<?php
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];
... html stuff
// and then do again for your second loop
// if you only want the first occ of that as well
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];
reset($collection); // Resets the array's internal pointer to the first element
$product = current($collection); // Returns the current element which is now first element
As far as I'm concerned, this is the best solution I would ever go for if I need to only extract the first element from an array.
Related
I'm wanna to add hyperlink on every emails and phone numbers that appear in the table like shown below
This is the code that I'm writing right now.
<tbody>
<?php
$grand_total_amount = 0;
foreach ($processed_sales as $key => $sale):
?>
<tr class="sale" data-id="<?= $sale['id']; ?>">
<td><?= $pagination->offset + $key + 1; ?></td>
<?php
foreach ($columns as $key => $value):
if ($checked_columns[$key]):
?>
<td><?= $sale[$key]; ?>
<?php echo $value['email']; ?></td>
<?php
endif;
endforeach;
?>
</tr>
<?php
$grand_total_amount = bcadd($grand_total_amount, $sale['total_amount'], 2);
endforeach;
?>
</tbody>
I only want the data in column email and phone number to have hyperlink that will direct them to other pages.
It's quite unclear what you're doing but what I could figure out is, your <a> tags should be within <td> tags, like this:
<td>
<a href="../Jualan.php?email=<?php echo $value['email']; ?>" class="style1">
<?php echo $value['email']; ?>
</a>
</td>
Otherwise it would messup the table.
Your should be inside your maybe that's why it doesnt works
echo "$value['email'];";
I'm working on this code and trying to get it to only pull 4 entries from the repository and it keeps pulling all of my entries.
<?php
while ( $i < 4) {
foreach ($churchEvents->getResults() as $churchEvent){
?>
<tbody>
<tr>
<td> <?php echo $churchEvent->getStructuredText('church-events.event-title')->asText(); ?>
</td>
<td class="cuntd" style="vertical-align: middle;">
<?php echo $churchEvent->getDate('church-events.event-start-date')->asDateTime()->format('l, F j, Y'); ?>
</td>
</tr>
</tbody>
<?php $i++; } } ?>
You have two loops, but there is no reason to have two. The inner one (foreach) already iterates through all the $churchEvents, so the outer one (while) will not make it do less.
Remove the outer while loop and the $i++;, and make the inner loop only loop over the first four elements.
For this, you can use the => syntax in the foreach statement to get the value of $i, and then add an if to exit the loop:
<tbody>
<?php
foreach ($churchEvents->getResults() as $i => $churchEvent){
if ($i >= 4) break;
?>
<tr>
<td> <?= $churchEvent->getStructuredText('church-events.event-title')->asText() ?>
</td>
<td class="cuntd" style="vertical-align: middle;">
<?= $churchEvent->getDate('church-events.event-start-date')->asDateTime()->format('l, F j, Y') ?>
</td>
</tr>
<?php
}
?>
</tbody>
Note that tbody should not be generated more than once, so it should stay out of the loop.
Because while is outside foreach.
while ( $i < 4) {
foreach ($churchEvents->getResults() as $churchEvent){
At the start of foreach try if combine with break.
//you don't need while here anymore
foreach ($churchEvents->getResults() as $churchEvent){
if ($i >= 4) {
break;
}
The method names are distinctive enough to make a guess what library/api you're using: (http://prismicio.github.io/php-kit/namespaces/Prismic.html), right?
The ->getResults() method returns an array. So, there are several options; here are some of them:
Using an SPL LimitIterator:
<?php
// only fetching the first four entires
$iter = new LimitIterator( new ArrayIterator($churchEvents->getResults()), 0, 4);
foreach ($iter as $churchEvent) { ?>
<tbody>
<tr>
<td> <?php echo $churchEvent->getStructuredText('church-events.event-title')->asText(); ?></td>
<td class="cuntd" style="vertical-align: middle;"><?php echo $churchEvent->getDate('church-events.event-start-date')->asDateTime()->format('l, F j, Y'); ?></td>
</tr>
</tbody>
<?php
}
Using array_slice() to get the first four elements and then iterate over those four:
<?php
// copying the first four element of the array
$arr = array_slice($churchEvents->getResults(), 0, 4);
foreach ($arr as $churchEvent) { ?>
<tbody>
<tr>
<td> <?php echo $churchEvent->getStructuredText('church-events.event-title')->asText(); ?></td>
<td class="cuntd" style="vertical-align: middle;"><?php echo $churchEvent->getDate('church-events.event-start-date')->asDateTime()->format('l, F j, Y'); ?></td>
</tr>
</tbody>
<?php
}
(prefered option:) Only request four items/fragements:
I'm not familiar with the api but at the very least there's the method pageSize(integer $pageSize) : \Prismic\SearchForm which sets the resultset size for the pagination. Maybe you can even limit the number of results in the response with some predicates or a limit-clause or something, i.e. only request what you really need.
I got a foreach which loops through my database for each user.
<?php
foreach($users as $user) {
?>
<tr>
<td>
<?php
for ($i=1; $i < 52; $i++) {
echo $i;
}
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
This loop through database and echos the username, now I tried to build a for loop inside this so that every user has a number, I took as an example a very basic for loop ( it will be changed later).
The problem is that I get all numbers printed out for each user, but I just want to print out the number once for a user. How do I solve this.
If you want unique index number for user, you do not need extra loop.
Just add an increment variable:
And increment it in existing loop.
<?php
$i=0;
foreach($users as $user) {
++$i; // For printing first user, it will be 1 not 0.
// Therefore, increment is placed before printing name.
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php
}
?>
This should be enough to achieve what you're trying to do :
Your for() isn't needed since foreach() already create a loop, you just have to use this loop to increment a value (here called $i) then display it.
Also you should avoid to open your php tags ten thousands times for a better visibility into your code :)
<?php
$i = 0;
foreach($users as $user) {
$i++;
echo'<tr>
<td>'.$i.'</td>
<td>'.$user['firstname'].'</td>
</tr>';
?>
<?php
$i=1;
foreach($users as $user) {
?>
<tr>
<td>
<?php
echo $i++;
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
Try to use the key in foreach
<?php foreach ($users as $key => $user) : ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php endforeach; ?>
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 have this block that prints some description and some product details.
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
I want to turn this into printing the description and only a number of product details so what I did is this
<?php
for ($i = 0; $i <=4; $i++) {
print_r($_additional[$i]);
}
?>
This prints only the description though. How can I also print the product details?
Thank you
You can access the details of the array with
<?php for ($i = 0; $i <=4; $i++) {
echo $_additional[$i]['label'];
echo $_helper->productAttribute($_product, $_additional[$i]['value'], $_additional[$i]['code']);
}