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?

Dani AI

Generated

is right about why the loop never terminates, but there is a second gotcha here: you are reusing $row2[0] both as "first column from mysql_fetch_row" and as your loop counter. That makes the code hard to reason about and easy to break. Use a dedicated index variable for the loop, then treat $row2 strictly as a database row.

If you want links like loan17.html down to loan15.html, drive the loop from the upper bound down to your lower bound. Keep the bounds explicit and guard against cases where $rownum3 is less than 15.

$low  = 15;
$high = (int) $rownum3;

for ($i = $high; $i >= $low; $i--) {
    $filename = "loan{$i}.html";

    // Use $i wherever you previously used $row2[0]
    // e.g., query by ID $i, build the link text, and write output
}

Even better, push the ordering into SQL and avoid per-row queries. Let MySQL hand you the rows newest-first, then just iterate once and build filenames from the IDs you get back.

$sql = "SELECT newcmsBlock9ID, title
        FROM newcmsBlock9
        WHERE pageID = 1 AND newcmsBlock9ID >= 15
        ORDER BY newcmsBlock9ID DESC";
$res = mysql_query($sql, $con);

while ($row = mysql_fetch_assoc($res)) {
    $i = (int) $row['newcmsBlock9ID'];
    $filename = "loan{$i}.html";
    // write "<a href=\"$filename\">{$row['title']}</a>"
}

Notes:

  • As hinted, keep the loop pieces in the correct slots: initialize; condition; decrement. Do not put an assignment in the condition.
  • Check your bounds to avoid off-by-one errors and empty loops when $rownum3 < 15.
  • For modern PHP, replace mysql_* with mysqli or PDO and use prepared statements.

Recommended Answers

All 3 Replies

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.