Forum Moderators: coopster

Message Too Old, No Replies

while inside of a while?

         

ajs83

9:56 pm on Apr 23, 2005 (gmt 0)

10+ Year Member



I have a db driven site that has templates for each page and wanted to add a unique id for each table, but am not sure how to do it since there is already a while without interrupting the current flow.

I understand the process needed but it seems that I'm putting it in the wrong place as it does not work and the for line generates a unexpected ")", expecting ";" line.

Here's what I would like to add

$MyNumber = 1;

for ($MyNumber <= 10)
{

"$MyNumber" (it would be listed as the table id so I didn't echo or print it)

$MyNumber++;
}

Here's my existing code


$row_count = 0;
while($r=mysql_fetch_array($result))
{

$name=$r["name"];
$image=$r["image"];
$url=$r["url"];
$description=$r["description"];
$id=$r["id"];

echo "<TD><div align=\"center\">
<center><TABLE id=\"$MyNumber\" WIDTH=200 BORDER=0 CELLPADDING=0 CELLSPACING=0 style=\"border-collapse: collapse\" bordercolor=\"#111111\">
snip - rest of the code";
$count++;
if ($count==3) {
echo "</tr><tr>";
$count=0;
}
$row_count++;
}
echo "</tr></table>";

jd01

10:29 pm on Apr 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not sure I quite understand what you are doing/have tried, because the way it is set now, every time you go through the loop you are creating a new table, but you don't close the table, and only have table rows defined every third time through... and then close a single table after the loop is complete?

You also say you want to create a new table with the row number inserted as an id, but this table is inside of the loop, so haw many results do you actually want before it is closed and a new one is started?

So, I guess to help I would need to know, when should a new table actually start? Should it be after every 3rd time through, like your <tr>? Or, is it based on some other factor?

The way it is now, you would need multiple loops, individually, not nested, because you are using all of your results from the DB in this loop, while($r=mysql_fetch_array($result)) , so even if you had the nesting correct, you would have to have another connection string to get anything, except blank tables/rows to display.

Maybe I am missing something?

Justin

ajs83

10:41 pm on Apr 23, 2005 (gmt 0)

10+ Year Member



What I posted above is only the top part that is looped and the looping, not the entire code. I figured I'd snip that because it is somewhat long.

Each table is closed before the $count part comes into play.

What I would like is to have the code +1 every table every time this code is looped to the bolded part

<center><TABLE id=\"$MyNumber\" WIDTH=200 BORDER=0 CELLPADDING=0 CELLSPACING=0 style=\"border-collapse: collapse\" bordercolor=\"#111111\">

jd01

11:18 pm on Apr 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be able to do that fairly easily... at the close of each table you could add $MyNumber++;
EG

{
echo "</table>";
$MyNumber++;
}

as long as this line is only read when a table is actually closed, the count would only increase when a table is closed. Then if you want to stop the while loop, at 10 tables, regardless of the DB results you could add an 'if... break'

EG
{
$MyNumber++;
if($MyNumber == 11)
break;
echo "</table>";
}

Since the ++ is at the end of the loop, the break has to be one higher than the # of tables you want, because you will not complete the last one.

It looks like you have a </table> out of the loop to close the last table, so you would want to 'break', before the close or you will have double close tags in your html.

* You should be able to put the table first if this is not the case, then you will close the table before you 'break'.

This should work if I am understanding what you are asking correctly.

Hope so.

Justin

Added: You can actually add the 'if...break' anywhere it best suits your purposes, maybe right at the end of the full loop would be better?

ajs83

12:31 am on Apr 24, 2005 (gmt 0)

10+ Year Member



I used method one and it worked great! Just have one issue

The first box id is output as Table id="" while the rest work (and start with 2).

I tried putting $MyNumber = 1; but of course, that set all table id's to 1.

jd01

12:53 am on Apr 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try setting it to one outside of the loop, like this:
$MyNumber = 1;
$row_count = 0;
while (blah)
{
more blah;
}

Justin