Jump to content

[SOLVED] How to echo in a loop


forcom

Recommended Posts

$ctryfrom = "SELECT DISTINCT country FROM table";

$ctry = mysql_query($ctryfrom);

 

while($row = mysql_fetch_assoc($ctry)){

$countrynames = $row['country'];

 

echo '<table><tr><td>';

echo "$countrynames";

 

//How can I set the loop to echo </td></tr><tr><td> ten loops to add another column in the table.

            }

 

Link to comment
https://forums.phpfreaks.com/topic/161289-solved-how-to-echo-in-a-loop/
Share on other sites

You'll need a counter.

 

<?php
$ctryfrom = "SELECT DISTINCT country FROM table";
$ctry = mysql_query($ctryfrom);

echo '<table><tr><td>';

$counter = 0;
while($row = mysql_fetch_assoc($ctry)) {
     echo $row['country'];
     if (++$counter % 10 === 0) echo '</td></tr><tr><td>';
}

echo '</td></tr></table>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.