Missing </TR> in WHILE loop


    $count = 0;
    while ($school = mysql_fetch_array($schoolquery)) {
        if ($count == 0 ) { $page .= "<tr>"; }
        $page .= "<td width=\\"150\\">".$school["school"]."</td>";
        if ($count % 3 ) { $page .= "</tr>\
"; }
        $count++;
    }

This is the source. How do I use the Mod correctly to add a </tr> if it gets to three OR if the While runs out. If the mod is 2 and while ends, it wont print the closing </tr>.

I just cant see where the problem is.


      [COLOR=#0000ff]<[/COLOR][COLOR=#800000]h3[/COLOR][COLOR=#0000ff]>[/COLOR]Show Team[COLOR=#0000ff]</[/COLOR][COLOR=#800000]h3[/COLOR][COLOR=#0000ff]>[/COLOR]
 
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]table [/COLOR][COLOR=#ff0000]id[/COLOR][COLOR=#0000ff]="info">[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR]California[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Eastside[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Westside[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR]Kentucky[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Oak Ridge[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>  <--MISSING TR[/COLOR]
[COLOR=#0000ff]</[/COLOR][COLOR=#800000]table[/COLOR][COLOR=#0000ff]>[/COLOR]

I am trying to get it to print out similiar to:

California
Eastside Westside

Hawaii
Ocean Front Mountainside Blue Lagoon
Coral Depths

Kentucky
Oak Ridge

If, in the example, it prints out three schools like after Blue Lagoon, it prints a </tr> and moves to the next line, to print three more, etc…

If there is only one team, like in Kentucky, it prints it and closes it with a </TR>

This is a common issue i have had with loops and tables and to fix it for myself i would simply use

if (substr($page, -5) != '</tr>') $page .= "</tr>\
";

I don’t understand how your reply works?
It just prints everything in one straight line down.

You add it after the loop so it would look like

    $count = 0;
    while ($school = mysql_fetch_array($schoolquery)) {
        if ($count == 0 ) { $page .= "<tr>"; }
        $page .= "<td width=\\"150\\">".$school["school"]."</td>";
        if ($count % 3 ) { $page .= "</tr>\
"; }
        $count++;
    }
    
    if (substr($page, -5) != '</tr>') $page .= "</tr>\
";
    echo $page;

After much tinkering and using your idea, I came up with this. It works, but I am not sure it is efficient.

Thanks for the help, SgtLegend

    $count = 0;
    $page .= "<tr>";
    while ($school = mysql_fetch_array($schoolquery)) {
        $page .= "<td width=\\"150\\">".$school["school"]."</td>";
        if ($count >= 2) { $page .= "</tr>\
<tr>"; $count = 0; }
        $count++;
    }
  }
  if (mysql_num_rows($statequery) == 0) { $page .= "<tr class=\\"alt\\"><td>No teams found.</td></tr>\
"; }
  $page .= "</table>";
  $page = str_replace("<tr><tr>", "<tr>", $page);
  $page = str_replace("</td></table>", "</td></tr>\
</table>", $page);

With this source output:


[COLOR=#0000ff]<[/COLOR][COLOR=#800000]h3[/COLOR][COLOR=#0000ff]>[/COLOR]Show Team[COLOR=#0000ff]</[/COLOR][COLOR=#800000]h3[/COLOR][COLOR=#0000ff]>[/COLOR]
 
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]table [/COLOR][COLOR=#ff0000]id[/COLOR][COLOR=#0000ff]="info">[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR]California[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Central[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Eastside[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Northside[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Southside[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Westside[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR]Kentucky[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]<[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]<[/COLOR][COLOR=#800000]td [/COLOR][COLOR=#ff0000]width[/COLOR][COLOR=#0000ff]="150">[/COLOR]Oak Ridge[COLOR=#0000ff]</[/COLOR][COLOR=#800000]td[/COLOR][COLOR=#0000ff]>[/COLOR][COLOR=#0000ff]</[/COLOR][COLOR=#800000]tr[/COLOR][COLOR=#0000ff]>[/COLOR]
[COLOR=#0000ff]</[/COLOR][COLOR=#800000]table[/COLOR][COLOR=#0000ff]>[/COLOR]