Why won't $i count up one each time with the following code?
<?php if(get_field('staff_member')) { ?>
<div class="row-fluid">
<?php while(has_sub_field('staff_member'))
{
for($i = 0; $i <= 1; $i++)
echo '<div class="span3 mobile_width' . $i . '">
.....etc...
}
echo '</div>';
}
?>
The output has 4 items and they all return with the the class mobile_width0.
And it also outputs 2 of each item.
Because you are resetting it to 0 each time. You don't need for loop, that's why you had double output. You can do it like this:
<?php $i = 0;
while(has_sub_field('staff_member')) {
echo '<div class="span3 mobile_width' . $i . '">';
$i++;
}
Try
for($i = 0; $i < 1; $i++)
< instead of <=
Each time your while cicle iterates, $i gets reset to 0 and in this line for($i = 0; $i <= 1; $i++) you are saying that $i just can take 0 and 1 values
because your resetting "i" to 0 in your for loop.
Related
I need your help to solve this problem, I am trying to write the necessary code so that the output is as follows:
PHP! PHP!! PHP!!! PHP!!!! PHP!!!!!
<?php
$y = ['!'];
for($i = 1; $i <= 5; $i++)
{
print "PHP!"."$y\t";
if($y+1 <5)
$y="!";
$y+'!';
}
?>
Until the second word, it adds one more exclamation point and in the rest of the repetitions it has the same number, that is two exclamation points.
How can I solve this problem?
Try this:
<?php
for ($i = 0; $i < 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
You can try it:
<?php
for($i = 1; $i <= 5; $i++)
{
echo "PHP";
for($j = 1; $j <= $i; $j++){
echo "!";
}
}
?>
You can also use str_repeat():
<?php
for ($i = 1; $i <= 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
Output will be: PHP!PHP!!PHP!!!PHP!!!!PHP!!!!!
My niece is trying to create one for-loop (php), that results in this:
* 12345678910987654321
example for loop she tried:
for ($i = 1; $i <= 10; $i++ , $i = 10; $i <= 1; $i--) {
echo $i . ' ';
}
She can only use if's and elseif's. I'm not a programmer and can't really help her. Any ideas how this could be achieved in php?
Any information would be greatly appreciated.
The key is to add a variable instead of a number, then reverse that number when $i hits 10.
for($i = 1, $j = 1; $i> 0; $i+=$j) // Start i at 1, and j at 1
{
echo $i;
if($i == 10)
$j = -1; // i has hit 10, so use -1 to start subtracting
}
Another possibility is to loop up to 20, printing $i for the ascending part and 20 - $i for the descending.
for ($i = 1; $i < 20; $i++) {
if ($i <= 10) {
echo $i;
} else {
echo 20 - $i;
}
}
I am trying to dynamically populate a select tag in php, by printing values from an array of objects.
this is my code
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[i]->getId().'">'.$z[i]->getDescription().'</option>');
}
the array is populated, in fact if i try to print only the fields i get a result, but in the combo-box nothing appears
Replace i with $i
<select name="name">
<?php
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>');
}?>
</select>
Add select tag and replace i with $i
echo "<select>";
for($i = 0; $i < count($z); $i++) {
echo '<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>';
}
echo "</select>";
ok, i resolved by creating two support arrays
$ids = array();
$descs = array();
for ($i = 0; $i < count($z); $i++) {
$ids[$i] = $z[$i]->getId();
$descs[$i] = $z[$i]->getDescritpion();
}
and using them instead of the objects
for ($i = 0; $i < count($z); $i++) {
print('<option value ="' . $ids[$i] . '">' . $descs[$i] . '</option>');
}
not very efficient, but working.
thanks for the answer :)
As you see I use this code to increment photos in a file, one by one :
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . '0' . $i . '.jpg"/></a></div>';
}
else
{
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . $i . '.jpg"/></a></div>';
}
}
?>
I would like to do the same but with php pages reading them from a specific directory like : ../phppages/
Any idea?
I tried but I missed something I guess and I would like to do it same way but with .php instead of .jpg.
Thank you very much.
Try this:
<?php
for ($i = $photonumber; $i >= 1; $i--) {
if($i < 10){
$i = "0".$i;
}
include("../phppages/page".$i.".php");
}
?>
Update: output by 3
<?php
$photonumber = 12;
for ($i = 0; $i < $photonumber; $i++) {
$j = $photonumber - $i;
if($j < 10){
$j = "0".$j;
}
if ($i % 3 === 0)
{
if ($i !== 0)
{
echo '</div>';
}
echo '<div>';
}
include("../phppages/page". $j.".php");
}
echo '</div>';
Update 2
To find the amount of files, that look like "page".$j.".php" in "../phppages" directory try this:
$files = glob("../phppages/page*.php");
$photonumber = count($files);
That would just be this:
for ($i = 1; $i <= $photonumber; $i++) {
include ('php-file-' . $i . '.php');
}
About having it 3 by 3, to be more precise, i want to create a fore each 3 .php file. Like
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div>';
include("../phppages/page".$i.".php") x3;
echo '</div>';
}
?>
Then it become something like this :
div : 12.php + 11.php + 10.php , div : 09.php + 08.php + 07.php , div
:
06.php + 05.php + 04.php , div : 03.php + 02.php + 01.php
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++)