2 Lists from DB
Moderator: General Moderators
2 Lists from DB
I have some links in the DB and I want to make 2 ul lists with them. The only issue is basically separating the array returned from the DB into 2 ul lists. I think I would need somekind of loop for that but not sure.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
My query returns a list of links and urls from the DB in alphabetical order based on links description. I want to display what is returned by the DB into 2 equal columns.
This is my query:
This is what I currently display in 1 list:
This is my query:
Code: Select all
$query = "SELECT LinkID, SiteAddress, SiteName FROM links ORDER BY SiteName ASC";Code: Select all
if($result) {
echo '<ul class="colleft">';
//Fetch and print all the records.
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<li><a href="http://' . $row['SiteAddress'] . '">' . $row['SiteName'] . '</a></li>';
}
//Free up the ressources
mysql_free_result($result);
echo '</ul>';
}- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
mysql_num_rows
You first need to query the result to see the number of rows using the function above. Then simple divide by the number of columns (rounded up) (this assuming only one line in use per list item).
You first need to query the result to see the number of rows using the function above. Then simple divide by the number of columns (rounded up) (this assuming only one line in use per list item).
Code: Select all
$col_length=ceil(mysql_num_rows($result);
f/php]
You can then use this to loop through you results. Of course you also have to check that you have more than 1 column (1 item) before you start your second column.