denoteone Posted January 3, 2010 Share Posted January 3, 2010 This would fall under PHP and MySQL. I need to get names out of a database and echo them into a list. I am trying to break the list into two columns Below is what I normally use. <?PHP $query = "SELECT name, FROM contact WHERE location = ohio"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_NUM)) { ?><li><?PHP echo $row[0]; ?></li> } The above code should print the list in a straight line. Below I have started what I think needs to be done to split it into two columns. Any help solving this would be great. $total = total number of $row[]; $half = $total / .5; for (i = 0; i < $total; i ++){ if(i < $half){ ?> <li><?PHP echo $row[i]; ?> </li> <?PHP else { ?> <li><?PHP echo $row[i]; ?> </li> <?PHP Quote Link to comment https://forums.phpfreaks.com/topic/186999-echo-a-list-and-split-into-two-columns/ Share on other sites More sharing options...
$Three3 Posted January 3, 2010 Share Posted January 3, 2010 Ok on this script here: $total = total number of $row[]; $half = $total / .5; for (i = 0; i < $total; i ++){ if(i < $half){ ?> <li><?PHP echo $row[i]; ?> </li> <?PHP else { ?> <li><?PHP echo $row[i]; ?> </li> <?PHP Make it this: $total = total number of $row[]; $half = $total / .5; for ($i = 0; $i < $total; $i++){ if($i < $half){ ?> <li><?PHP echo $row[$i]; ?> </li> <?PHP else { ?> <li><?PHP echo $row[$i]; ?> </li> <?PHP Try adding the dollar $ symbol in front of the i's and it should work now man. Quote Link to comment https://forums.phpfreaks.com/topic/186999-echo-a-list-and-split-into-two-columns/#findComment-987473 Share on other sites More sharing options...
RussellReal Posted January 3, 2010 Share Posted January 3, 2010 you know.. you could probably take what you made last time.. and just modify the css.. for example.. ul { width: 200px; margin: 0; padding: 0; } ul li { width: 100px; float: left; display: block; } Quote Link to comment https://forums.phpfreaks.com/topic/186999-echo-a-list-and-split-into-two-columns/#findComment-987474 Share on other sites More sharing options...
The Little Guy Posted January 3, 2010 Share Posted January 3, 2010 I believe something like this should work ($r may need to be changed to 1): <?php $total = mysql_num_rows($sql); $per_row = ceil($total / 2); $r = 0; echo '<ul style="float:left;">'; while($row = mysql_fetch_assoc($sql)){ echo '<li>'.$row['column'].'</li>'; if($r == $per_row){ echo '</ul><ul style="float:left;">'; } $r++; } echo '</ul>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186999-echo-a-list-and-split-into-two-columns/#findComment-987476 Share on other sites More sharing options...
denoteone Posted January 3, 2010 Author Share Posted January 3, 2010 @RussellReal but I want to do it so that if there is a long name there will not be any issues. @The Little Guy I will try this method and let you know what i come up with. Quote Link to comment https://forums.phpfreaks.com/topic/186999-echo-a-list-and-split-into-two-columns/#findComment-987478 Share on other sites More sharing options...
RussellReal Posted January 3, 2010 Share Posted January 3, 2010 @RussellReal but I want to do it so that if there is a long name there will not be any issues. @The Little Guy I will try this method and let you know what i come up with. well.. what would be the difference with a table?.. why don't you just make the lis 350px wide then and the ul 700px wide.. it makes no sense to fill your html up with a million redundant tags Quote Link to comment https://forums.phpfreaks.com/topic/186999-echo-a-list-and-split-into-two-columns/#findComment-987492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.