Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/186999-echo-a-list-and-split-into-two-columns/
Share on other sites

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.

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;

}

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>';
?>

@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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.