while($array = mysql_fetch_array($queryresults) && $i <= '9' ){ echo "stuff" } - php

So I have a query, and though my query can possibly return 20 results, I only want it to show the first 9 results. There is a dumb reason I'm not just limiting the query results to 9, for this purpose I need to know how to stop the while function if $i reaches 9.
Code is
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i <= '9')
{
echo $array['id'];
$i++;
}
How do I get it to stop putting out more echoes after 9th result? Thank you!

Putting the limiting condition first will also make sure you only fetch 9 rows, not and 10 and one discard because && is called lazy (if the first part is false, it wont even look at the next part).
$i = 0;
while($i < 9 && $array = mysql_fetch_array($queryresults)){
echo $array['id'];
$i += 1;
}
or if you want to be fancy:
$i = 9;
while($i-- > 0 && $array = mysql_fetch_array($queryresults)){
echo $array['id'];
}
But I find this error prone ..

$i = 0;
while($array = mysql_fetch_array($queryresults) && $i++ < 9)
echo $array['id'];
}
Notes:
No need to put 9 in quotes
You can increment-after-compare by doing $i++ in the while loop condition (although you don't have to, you could also put it after echo $array['id'] in the while loop body).
Careful! You want < 9 and not <= 9 since starting at zero, or you'll get 10 iterations of the loop.
Cheers

Your $i++ needs to be inside the while loop
You should also have $i <= 9 instead of $i <= '9' but it should still work as is

'9' needs to be 9
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i < 9){
echo $array['id'];
$i += 1;
}

Related

Can a PHP for loop with number array start with NULL and also have 0?

I am working with some legacy PHP code so re-writing this isn't an option at this point but I have a dropdown for number of years and months of employment and currently they go from 0 - 11, 0 - 65. Can a PHP loop array numbers starting at NULL, which adds -Select- as the default forcing user to make a selection, but also have 0 as the starting number?
I've tried:
for ($i = NULL; $i <= 11; $i++) {
echo $i;
}
But 0 is no loner an option
This is what I have currently:
for ($i = 0; $i <= 11; $i++) {
echo $i;
}
I need it to display as:
-Select-
0
1
2
3
4
etc
Echo the text before echoing the numbers using a loop.
<?php
echo '-Select-';
for($i = 0; $i <= 11; ++$i) {
echo $i;
}
NULL++ would not increase the value of NULL, which is essentially nothing. Why not start at -1, and if ($i == -1) then echo select?
It would look like this:
for ($i = -1; $i <= 11; $i++) {
if ($i == -1) {
echo '-Select-';
} else {
echo $i;
}
}

PHP: How to loop from 100 to 0 by -2 without letting it go infinitely?

what I am trying to achieve here is to be able to loop from 0 to 100 (100, 98, 96, 94 ...) but has to stop at 0. What is doing right now is it passes 0 and -2 -4 which crashes the server. What am I doing wrong?
for ($i = 100; $i <= 100; $i--){
echo $i--;
echo "<br>";
}
Maybe a little explanation would be useful.
The middle part of the for loop $i <= 100 is what makes it infinite. That expression is checked before each iteration of the loop, and the loop will continue as long as that expression evaluates to true.
Since you set $ito 100 in the first section of the loop, and you're doing nothing except making it smaller, it will always be <= 100, forever.
The loop will work fine just the way you have it written if you change the continuation condition.
for ($i = 100; $i >= 0; $i--){
echo $i--;
echo "<br>";
}
That way it will continue until $i is reduced to less than zero, then $i >= 0 will be false, and the loop will end.
The third argument in for loop is what will be executed at the end of the loop. So:
for ($i = 100; $i >= 0; $i -= 2){
echo "$i<br>";
}
Will do the trick
As you can read here https://secure.php.net/manual/en/control-structures.for.php
At the end of each iteration, expr3 is evaluated (executed).
Alternatively:
<?php
foreach(range(100, 0, -2) as $n) {
echo $n;
}
for ($i = 100; $i >= 0; $i-=1){
echo $i--;
echo "<br>";
}
I figured it out somehow. Has been studying JavaScript for a year; Loop is still confusing to me

Exam Qn: Convert do while loop to for loop (PHP)

Recently, my exams got over. My last exam was based on PHP. I got the following question for my exam:
"Convert the following script using for loop without affecting the output:-"
<?php
//Convert into for loop
$x = 0;
$count = 10;
do
{
echo ($count. "<BR>");
$count = $count - 2;
$x = $x + $count;
}
while($count < 1)
echo ($x);
?>
Please help me as my computer sir is out of station and I am really puzzled by it.
Well, If I understand well, You have to use "for" loop, instead of "do...while", but the printed text must not change.
Try:
$count = 10;
$x = 0;
$firstRun = true;
for(; $count > 1 || $firstRun;) {
$firstRun = false;
echo ($count . "<BR>");
$count -= 2;
$x = $x + $count;
}
echo ($x);
By the way loop is unnecessary, because $count will be greater than 1 after the first loop, so the while will get false.
EDIT
$firstRun to avoid infiniteLoop
$count in loop
EDIT
Fixed code for new requirement
Removed unnecessary code
Hmmm I don't know if your teacher wanted to own you... but the do{} will execute only once since $count is never < 1.
The output of your teacher's code is:
10
8
I presume there was a mistake in the code and the while would be while($count > 1) which would make more sense (since it's weird to ask for a loop to output only 10 8) and would result in this output:
10
8
6
4
2
20
Then a good for() loop would have been :
$x = 0;
$count = 10;
for($i = $count; $i > 1; $i -= 2)
{
$count -= 2;
echo $i . "<br>";
$x += $count;
}
echo $x;
Which will output the same values.
If you can, ask your teacher for this, and comment the answer ^^ ahahah

PHP even numbers generating

I'm somewhat new to PHP, been reading a few books and I've never seen a loop where it gets you all the even numbers(for example from 1 to 10), so I decided to try it myself:
for($i=0;$i<10 && $i % 2===0;$i++)
echo $i;
Tried with only double == as well.
And this,
$i=0;
do echo $i; while($i++<10 && $i % 2 ==0);
Can't seem to figure out how to use 2 conditions in the same statement.
Would appreciate the help!
Thanks.
Try to use this code
for( $i=0; $i<=10; $i++ )
{
if( $i%2 == 0 ){
echo $i;
}
}
The loop is breaking entirely when the second condition fails the first time. On the first iteration: 0 is less than 10, and it is even, so the loop iterates. On the second iteration: 1 is less than 10, but is odd, so the loop breaks.
Your code is the equivalent of this:
for($i=0; $i<10; $i++) {
if ($i % 2 !==0 ) {
break;
}
echo $i;
}
0
You can eliminate the second condition of your for loop to prevent the breakage and rely exclusive on a third expression to increment $i by two each iteration.
for($i=0; $i<10; $i = $i + 2) {
echo $i;
}
02468
The second statement in a for-loop is/are the condition(s) which gets checked every loop. so if it fails your loop stops. what you need will look somewhat like this:
for ($i = 0; $i < 10; $i++)
if ($i % 2 == 0)
echo $i;
So the loop will run over every number but only print out the even ones.
You don't need to loop.
Range can create a range with third parameter step 2.
$arr = range(0,20,2);
Echo implode(" ", $arr);
https://3v4l.org/S3JWV
you can use also regular loop and get the evens by formula:
for($i=0; $i<10 ;$i++) {
$j = $i * 2;
// do somthing with $j witch loop over 10 first evens...
}

PHP Loop do action for each 10, 20, 30 etc [duplicate]

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed 3 years ago.
In php i have loop e.g.
for ($i = 0; $i <= 1000; $i++) {
if ($i = 10 || $i == 20 || $i == 30 || $i == 40 || $i == 50 || $i == 60) {
echo $i;
}
}
imagine i need to echo $i every 10,20,30,40,50,60,..970,980,990 there should be way to not write 100 conditions in if statement. Is there some logical way to see if $i increased by 10 then do something like:
if ($i == $i+10) {
...
}
P.S. if possible i dont want to introduce another variable to count i need to find solution with using only $i
Try:
if ($i % 10 == 0)
That will trigger whenever your index is exactly divisible by 10.
rewrite your for loop to:
for ($i = 0; $i <= 1000; $i+=10) {
And I don't know whether it worked for you with commas like this (as in your initial post):
for ($i = 0, $i <= 1000, $i++) {
Skip extra looping:
for ($i = 10; $i <= 1000; $i=$i+10) {
echo $i;
}
Or if you still want to loop every single digit between:
for ($i = 0; $i <= 1000; $i++) {
if( $i % 10 === 0 ) {
echo $i;
}
}
Test Here
Put this inside your main loop. '%', or 'mod', gives you the remainder of $i / 10. If the remainder is '0', then you want to print.
if(0 === ($i % 10)) {
echo $i;
}

Categories