Jump to content

loop in columns


boxocats

Recommended Posts

Hi

 

I have drop down menus on a website lsiting records that come out of a database, problem is they're too long, so Im thinking about putting them in 2 or more columns, this is my code

 

  

<?php do { ?>
       <li>
<a href="page.php?ailmentId=<?php echo $row_xxxTopNav['id']; ?>">
	<?php echo $row_xxxTopNav['ailmentName']; ?>
</a>
     </li>
     <?php } while ($row_xxxTopNav = mysql_fetch_assoc($xxxTopNav));
	mysql_free_result($xxxTopNav); ?>

 

I realise its in <li> so I reckon it would need to be a table with 2 columns, is there a nifty way to put it into as equal height of columns automatically?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/189709-loop-in-columns/
Share on other sites

First, use a regular while loop.  Not do_while, because you need to define the $row array before the actual loop statments.

 

And to your question, its pretty simply, you can use a code like this:

 

$query = mysql_query("SELECT * FROM `tableName` ORDER BY `columnName`");
$i = 0;
echo "<table>";
echo "<tr>";
echo "<td><ul>";
while($row = mysql_fetch_assoc($query)){
$i++;
if($i >= (mysql_num_rows($query) - $i))
break;
echo "<li>" . $row['columnName'] . "</li>";
}
$query2 = mysql_query("SELECT * FROM `tableName` ORDER BY `columnName` LIMIT " . $i . ", " . mysql_num_rows($query) - $i);
echo "</ul></td><td><ul>";
while($row2 = mysql_fetch_assoc($query2)){
echo "<li>" . $row2['columnName'] . "</li>";
}
echo "</ul></td>";
echo "</tr>";
echo "</table>";

 

I didn't test that, but it might work.

Link to comment
https://forums.phpfreaks.com/topic/189709-loop-in-columns/#findComment-1001219
Share on other sites

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.