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++;
}
Related
This question already has answers here:
PHP FizzBuzz Logic
(3 answers)
Another FizzBuzz solution [closed]
(3 answers)
PHP Fizzbuzz Challenge [closed]
(3 answers)
PHP loop increment quirk - (FizzBuzz in one line)
(4 answers)
Closed 2 months ago.
I came across this php interview question while researching pre-interview test questions.
Given an array of integers, compute a total score based on the following rules:
Add 1 point for every even number in the array
Add 3 points for every odd number in the array
Add 5 points for every time you encounter an 8 in the array
Examples:
Input: my_numbers = [1, 2, 3, 4, 5]
Output: 11
Input: my_numbers=[15, 25, 35]
Output: 9
Input: my_numbers=[8, 8]
Output: 10
How to approach this particular task?
my attempt
<?php function find_total( $my_numbers ) {
//Insert your code here \
$total = 0;
foreach($my_numbers as $val) {
if ($val % 2 == 0) {
echo (++$val);
} else
echo ($val += 3);
}
if ($val == 8) {
echo($val += 5);}
}
?>
I am not sure your sample answers are correct (8 is an even number and should attract 1 for that as well as the 5), but to give the answer you asked for use this (PHP):
(EDITED in response to mickmackusa's and Robin Bastiaan's comments)
$total = 0;
foreach($myarray as $value) {
if ($value === 8) {
$total += 5;
} elseif (!($value % 2)) {
++$total;
} else
$total += 3;
}
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 9 months ago.
I want know how i can extract the next 'team_name' from an array (30 difrent names) i know its probably simple, but I am beginer
when i using .$row0[$p]['team_name']. im geting this Warning: Trying to access array offset on value of type null in
$row0 = mysqli_fetch_array($sth0);
for ($i=1; $i <= $row['number_of_grups']; $i++)
{
echo "<div class='grup'> <p> GRUPA ".$i." </p>";
for ($a=0; $a < $teams_for_grup; $a++)
{
$team_input_name = "Druzyna" . $team_number_for_grups;
$team_number_for_grups++;
echo "<div class='teams'><a>".$row0['team_name']."</a><input type='number' class='points-box' name='".$team_input_name."' value='".$row0['points']."'</div>";
}
echo "</div>";
}
Without seeing the array for sure it will be hard to tell, but based off what I see here it looks like you would want to be doing:
$row[$a]['team_name']
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)
This question already has answers here:
Sum values in foreach loop php [closed]
(5 answers)
Closed 8 years ago.
I have no idea how can I display a sum of numbers from array. I work inside wordpress loop. Here is my code:
<?php
$a = array($apples);
foreach ($a as $b)
{
$totalapples = $b . " ";
echo "$totalapples";
}
?>
This code display (I have numbers like 4,5 etc.):
4,5 13,5 10 13,5 14 12,3 7,5 5,4 9,5 5,4 4,5 4 3,3 5,7
To add numbers, use +=.
$totalapples = 0;
foreach ($apples as $apple) {
$totalapples += $apple;
}
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();
}
}
?>