2 Lists from DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

2 Lists from DB

Post by vchris »

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You gotta give more information. What is the structure of the data? Where do you want the list split? Are these lists coming from the same array? Please post more info.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

what is the criteria by which you would like to split them?
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

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:

Code: Select all

$query = "SELECT LinkID, SiteAddress, SiteName FROM links ORDER BY SiteName ASC";
This is what I currently display in 1 list:

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>';
		}
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

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).

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.
Post Reply