I'm quite new to PHP. How can I change this foreach loop to only loop twice?
<?php
foreach($results as $row):
?>
Try this code, you just need a counter variable, initialize your counter variable from zero and increment it after each iteration.
<?php
$counter = 0;
foreach($results as $row) {
//your code
if($counter == 1)
{
break;
}
$counter++;
}
?>
You could iterate up to the size of the array or 2 - the smaller of the two.
<?php
$maxInd = min(2, count($results);
for ($i = 0; $i < $maxInd; ++$i) {
$row = $results[$i];
// Do something interesting with $row
}
?>
<?php
for ($i = 0, $count = count($results); $i < $count; ++$i) {
var_dump($results[$i]);
// or assign it to $result variable
$result = $results[$i];
}
?>
But this will work only for collection there are some examples for example generators where you must use foreach.
Related
I am having for loop in foreach loop
static $count=0;
for($i=$count;$i<$semesters_count;$i++)
{
echo $array_wam[$i];
$count++;
}
here $array_wam is array of some marks. I am printing multiple student marks
I getting first student marks
first student
50.6
second student
50.6 60.9
I want to show output like
first student
50.6
second student
60.9
Here loop again starts with 0 but I want loop starts with where it ends.
I dont know if i am understanding right what you are asking for but have you tried like this?
for($i=0;$i<$semesters_count;$i++){
echo $array_wam[$i];
}
For that you have to put static $count=0; before the foreach starts.
See an example below:
static $count=0;
foreach ($item)
{
for($i=$count;$i<$semesters_count;$i++)
{
echo $array_wam[$i];
$count++;
}
}
The problem is that you're not incrementing $count when you reach the end of the for loop.
Instead of incrementing $count inside the loop, set it to $i at the end of the loop:
static $count = 0;
for ($i = $count; $i < $semesters_count; $i++) {
echo $array_wam[$i];
}
$count = $i;
Try this :-
static $count = 0;
foreach ($item)
{
$semester_count_total = $semesters_count + $count;
for ($i = $count; $i < $semester_count_total; $i++) {
echo $array_wam[$i];
}
$count = $semester_count_total;
}
Try this:
for($i = 0; $i < $count_semester; $i++){
echo $array_wam[$i];
}
Why do you want static $count if it can loop trough for loop? And it still counters your $i variable is equal to the length of your $count_semester.
I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)
I currently have a loop:
foreach($this->rows as $row):
....
endforeach;
Is this possible to put into a while loop?
yes you can do like this
$arr = $this->rows;
$size = sizeof($arr);
$i=0;
while($i<$size)
{
$row = $arr[$i];
// your work
$i+=1;
}
I have code which pretty much does this.....
//get the row info
$Row1 = $FullTable->find('div[class=ismPitchRow1]',0);
$Row2 = $FullTable->find('div[class=ismPitchRow2]',0);
$Row3 = $FullTable->find('div[class=ismPitchRow3]',0);
$Row4 = $FullTable->find('div[class=ismPitchRow4]',0);
$Row5 = $FullTable->find('div[class=ismPitchRow5]',0);
//Loop 5 times. One for each row on the pitch.
for ($i=1; $i<=5; $i++)
{
if ($i = 1) { echo $Row1; }
if ($i = 2) { echo $Row2; }
if ($i = 3) { echo $Row3; }
if ($i = 4) { echo $Row4; }
if ($i = 5) { echo $Row5; }
}
It works, but as you can see it's not very efficient and badly designed. How would I simplify this? I know there are much smaller ways that these kind of loops can be done.
Thanks.
use the great invention of arrays:
//get the row info
$Row[1] = $FullTable->find('div[class=ismPitchRow1]',0);
$Row[2] = $FullTable->find('div[class=ismPitchRow2]',0);
$Row[3] = $FullTable->find('div[class=ismPitchRow3]',0);
or, even more clever...
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
$Row[$i] = $FullTable->find($find,0);
}
do the same for echoing:
for ($i = 1; $i <= 5; $i++) {
echo $Row[$i];
}
but why not do everything in 1 loop?
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
echo $FullTable->find($find,0);
}
I would store $Row1 through $Row5 in an array and iterate through it with a foreach loop.
<?php
$YourArray = array();
array_push($YourArray,$FullTable->find('div[class=ismPitchRow1]',0),$FullTable->find('div[class=ismPitchRow2]',0),$FullTable->find('div[class=ismPitchRow3]',0),$FullTable->find('div[class=ismPitchRow4]',0),$FullTable->find('div[class=ismPitchRow5]',0));
foreach($YourArray as $row){
echo $row;
}
?>
So, what à do is trying to display the 3 first element of an array. Always 3. But, there is not always at least 3 elements.
So, what I've done is using an if :
<?php for ($i = 0; $i <= 2; $i++) { ?>
<?php if($post["Project"]["Post"][$i]){ ?>
...
<?php } ?>
<?php } ?>
But, I keep having "undefined offset" error when there is not 3 entries at least. Anyone with a solution ?
foreach (array_slice($array, 0, 3) as $item) {
echo $item;
...
}
or:
$i = 1;
foreach ($array as $item) {
...
if ($i++ >= 3) {
break;
}
}
foreach is always preferable to iterate arrays, precisely because you cannot access anything that doesn't exist.
Try the following:
<?php for ($i = 0; $i <= 2; $i++) {
if(isset($post["Project"]["Post"][$i])){ ?>
...
<?php }} ?>
Use isset() to check whether the key exists.
<?php if(isset($post["Project"]["Post"][$i])){ ?>
Or you could use:
$posts = $post["Project"]["Post"];
foreach ($posts as $i => $post) {
//...
if ($i === 2) break;
}
change it to if (isset($post ...
Alternatively:
foreach (array_slice($post["Project"]["Post"], 0, 3)) { ...
<?php for ($i = 0; $i <= 2; $i++) {
if( isset($post["Project"]["Post"][$i] )){
...
}
} ?>
you do not need <?php...?> tags for every line.
try ifset.
<?php for ($i = 0; $i <= 2; $i++) { ?>
<?php if(isset($post["Project"]["Post"][$i])){ ?>
...
<?php } ?>
<?php } ?>
Or, do a count first could be a way too.
Calculate before, which condition is met first (the actual size of the array or the max number - in your case 3) and then just traverse those elements:
$min = min( 3, count( $post["Project"]["Post"] ) );
for ($i = 0; $i < $min; $i++) {
if($post["Project"]["Post"][$i]){
...
}
}