This question already has answers here:
PHP loop: Add a div around every three items syntax [duplicate]
(4 answers)
Closed 9 years ago.
I want to wrap every 10-groups of items in a while-loop inside a wrapper.
Visualized:
echo "<ul class='wrapper'>";
while(get_field('items'))
{
echo "<li>item</li>";
}
echo "</ul>";
In this case every element would be inside this one wrapper, but I have to wrap at max ten elements and then start a new wrapper.
What would be the best way to accomplish this?
You can try this
$count=1;
echo "<ul class='wrapper'>";
while(get_field('items'))
{
if($count % 10 == 0) {echo '</ul><ul class='wrapper'>';}
echo "<li>item</li>";
$count++;
}
echo "</ul>";
A different way to do this is:
<?php
$count = 0;
$group = array();
while(get_field('items'))
{
array_push($group, "<li>$val</li>");
if(++$count % 10 == 0)
{
echo "<ul class='wrapper'>".implode("", $group)."</ul>";
$group = array();
}
}
?>
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 3 years ago.
Summary: Trying to add +1 each time I go through my foreach loop, but it keeps going up even though I would only expect a maximum of 11 results in the loop
I tried to move around on the $i++; but without luck, I also check that I made sure the $i = 0; was outside of the foreach loop.
<?php
$facade = new NewsArticleFacade();
$news = $facade->getAll();
unset($facade);
$i = 0;
foreach($news as $new)
{
$i++;
if ($i = 5000000000000000)
{
echo "if statement: " . $i;
break;
}
$content = substr($new->getContent(), 0, 69);
echo "<div class='news'>";
echo "<h4><a href='/news/news.php?id=".$new->getNewsID()."'>".$new->getTitle()."</a></h4>";
echo "<span>by <a href='#'>".$new->getUser()->getUsername()."</a> - ".$new->getDate()->format("d-M-Y")."</span>";
echo "<p>".$content." ...</p>";
echo "<div class='gradient'></div>";
echo "<img src='img/news/0000000001.jpg'>";
echo "</div>";
}
?>
What really confuses me is if I remove the if statement for the loop, and just echo $i each time, it will echo out 11 in total, what am i doing wrong with my if?
I would expect the loop to go through 11 times since I have 11 entries in the database for this selection, but it keeps going up, if I echo within the if statement i can see the value will just be whatever i limit the if statement to be.
Thanks in advance!
This
if ($i = 5000000000000000)
Is assignment and it makes little sense as such in your code. You probably wanted to have comparison operation which is expressed with ==
if ($i == 5000000000000000)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to print like this using loop without nested loop:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
first line start from 1 and print only one integer
second line start from 2 and print two integer
third line start from 4 and print four integer
fourth line start from 8 and print eight integer
(same condition in other line if exist)
A pair of counters should do the trick;
$threshold = 1;
$x = 1;
for($i = 1; $i <= 15; $i++) {
echo $i . ' ';
if($x == $threshold) {
echo "<br>\n";
$threshold = $threshold * 2;
$x = 0;
}
$x++;
}
Will keep on working till infinity, or your script runs out of memory. Whatever comes first :)
You can achieve with single while and some array functions. Try this.
<?php
$start=1;
while($start<=10){
$array = range($start,($start+$start-1));
echo implode(' ',$array)."<br>";
$start=$start*2;
}
?>
something like this?
$i=1;
$t=1;
while($i<1000)
{
if($i==$t)
{
if($t!=1) echo "<br />";
$t=$t*2;
}
echo $i." ";
$i++;
}
same thing but with log($i,2).
<?php
$i=1;
while($i<20)
{
echo $i;
$i++;
if(filter_var(log($i,2), FILTER_VALIDATE_INT))
echo PHP_EOL;
else
echo "\t";
}
You can even do it without any for and some recursion ;-)
<?php
$elements = range(1,15);
display($elements);
function display(array $parts, $step = 1) {
if(! count($parts)) {
return;
}
$elements = array_splice($parts, 0, $step);
echo implode("\t", $elements)."\n";
display($parts, $step * 2);
}
See the working code on https://eval.in/755277
This question already has answers here:
MySQL/SQL retrieve first 40 characters of a text field?
(5 answers)
Closed 8 years ago.
table name - animals
column name id, story
I am inserting certain amount of data in story column.
But I need to display only first 20 characters in row. plz suggest me how to display only specific amount of characters.
below is my code... to echo rows.
<?php foreach($rows as $row){ ?>
<?php echo htmlspecialchars_decode($row['story']); ?>
<?php } ?>
You can use the explode function, and itarate thorugh words:
foreach ($rows as $row) {
$line = htmlspecialchars_decode($row['story']);
$words = explode(" ", $line);
$newWords = array();
for ($i = 0; $i < 20; $i++) {
if (isset($words[$i])) {
$newWords[] = $words[$i];
}
}
echo join(" ", $newWords)."<br>";
}
This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed 9 years ago.
So I am trying to use PHP to generate some content with the criteria of the nth loop needs to either open or close a div element. So my goal is to display 8 elements on the page and on the 9th element, I would like to close the div for the 8th element and open a new one for the 9th if there are any more image. Here is my code.
$images; // object
$i = 1;
echo '<div class="outer-container">';
while ( $images->have_images() ) {
if ( $i === 1 ) {
echo '<div class="inner-container">';
}
echo 'content here';
if ( $i === 8 || $images->total === $images->current + 1 ) {
echo '</div><!--.inner-container-->';
}
$i++;
}
echo '</div><!--.outer-container-->';
End result should look something like this:
<div class="outer-container">
<div class="inner-container">
content here
content here
content here
content here
content here
content here
content here
content here
</div><!--.inner-container-->
<div class="inner-container">
content here
content here
content here
</div><!--.inner-container-->
</div><!--.outer-container-->
I searched around and came to a conclusion that I have to probably use modulus but I am not certain how I can incorporate that into my code.
Thanks for looking.
if ( $i % 8 == 0) {
echo '</div><!--.container--><div class="container">';
}
Mod operator returns the remainder. You can simply state $i % 8 or $i % 8 == 0 or $i % 8 === 0 as john stated
But you will need to also open the next div in your conditional depending on how you structure this. An example:
<div class="outer-container">
<div class="inner-container">
<?php
$images; // object
$i = 1;
while ( $images->have_images() ) {
echo 'content here';
//print_r($i % 8);
if ( $i % 8 == 0 && $images->total > 8) {
echo '</div><!--.inner-container--><div class="inner-container">';
}
$i++;
}
?>
</div>
</div>
Added conditional in case your images total happens to be 8 it won't create a rogue empty div.
I'm trying to understand your comment. Are you saying you have an outer container, then an inner container, and inside this inner container you want the loop you defined in the question where each group of 8 is contained in a container div?
I added a print r in the loop you can uncomment to verify $i is doing what you want it to. By this code and the fact that $i begins at 1 you should not experience the closing div conditional prematurely.
Modulus is indeed what you're looking for:
if ( $i % 8 === 0 ) {
echo '</div><!--.container-->';
}
You forgot to open a new div
modify the following code:
if ( $i === 8 || $images->total === $images->current + 1 ) {
echo '</div><!--.container-->';
echo '<div class="container">'; //ADD THIS LINE!!!
}
and after you get out of the while add the closing </div> tag
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
On every third iteration in PHP
Using PHP foreach to get a series of results,how would I add the following echo '<div class="clear"><div>' every 4 results
Any ideas?
Using modulos can do the trick :
$i = 1;
foreach($data as $result) {
if($i % 4 === 0) {
echo '<div class="clear"></div>';
}
$i++;
}