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?

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]--)
commented: helpful post +6

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);
}

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....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.