0

I used the following to increment a "FOR" loop:

for($row2[0] = 15; $row2[0] <= $rownum3; $row2[0]++)

It worked.

I tried to use the following to decrement the "FOR" loop:

for($row2[0] <= $rownum3; $row2[0] = 15; $row2[0]--)

It did NOT work and went into an infinite loop for some reason.

Can someone please help?

3
Contributors
3
Replies
7
Views
6 Years
Discussion Span
Last Post by IIM
3

A for loop is set up as follows:

for ( inital step; condition to continue; step to execute after every loop )

So your second for loop, the one supposed to decrease, has the first two arguments mixed up.
This turns it into an infinite loop because $row2[0] = 15 will evaluate as if your condition said 15 which is always equivalent to true thus your loop never ends.
Also the 'fake' condition in your first argument would've produced an infinite loop since you want to terminate when something that is decreasing exceeds a certain value.

My guess at what you're looking for would be:

for ($row2[0] = 15; $row2[0] >= $rownum3; $row2[0]--)
Votes + Comments
helpful post
0

No, that will not work. This code dynamically produces links. I'm naming the files loan1.html, loan2.html, etc... And they're outputting to the page like that. But instead, I want them to output from the most recent to the least recent, like loan17.html, loan16.html, loan15.html, etc... So, that's why I tried the initial FOR loop:

for($row2[0] <= $rownum3; $row2[0] = 15; $row2[0]--)

Where $rownum3 represents the upper bound and $row2[0] = 15 would represent the lower bound, outputting an HTML link of <a href="loan15.html">Loan needed</a>.


Here's more code (this works, but produces the links in ascending rather than descending order, and I want it to be in descending order):


$query2 = <<< HERE
SELECT * FROM newcmsBlock9 WHERE title = '$title';
HERE;

$result3 = mysql_query($query2,$con);
$row2 = mysql_fetch_row($result3);

$rownum1 = <<< HERE
SELECT * FROM newcmsBlock9 WHERE pageID = 1;
HERE;

$rownum2 = mysql_query($rownum1, $con);
$rownum3 = mysql_num_rows($rownum2);

for($row2[0] = 15; $row2[0] <= $rownum3; $row2[0]++){
$str4 = "loan";
$str5 = $row2[0];
$str6 = ".html";
$filename2 = "{$str4}{$str5}{$str6}";

$linkquery1 = <<< HERE
SELECT * FROM newcmsBlock9 WHERE newcmsBlock9ID = '$str5';
HERE;

$linkresult1 = mysql_query($linkquery1, $con);
$linkrow1 = mysql_fetch_row($linkresult1);
$str7 = $linkrow1[6];

$output3 = <<< HERE
<p><a href="$filename2">$str7</a></p>
HERE;

fwrite($fp2, $output3);
}

0

Do you know what you are doing???
The syntax for for loop is:-

for(initialization;condition check;increment/decrement)

But in the first step you are using condition check,i.e. it will return either true or false....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.