Jump to content

Break MySQL records into CSS columns automatically


furiousweebee

Recommended Posts

Hi,

 

I'm making a page of links, each assigned a category. There are two tables - "favourites_links" and "favourites_categories".

 

This is how the links currently appear:

 

Category 1 Name

Link 1

Link 2

Link 3

 

Category 2 Name

Link 1

Link 2

Link 3

 

What I want to do is break my page into columns. Basically, set a number of columns (e.g. 3), count the total records, and work out how many records to show per column. Each column should be enclosed in a <div class="column"> wrapper, which floats to the left. This is how my page should appear once this is done:

 

Category 1Category 4Category 7

Category 2Category 5Category 8

Category 3Category 6Category 9

 

Here is my current code:

 

$resource = mysql_query("SELECT l.id id, l.cat_id cat_id, c.title cat_title, l.title title, l.link link, l.home home, l.work work FROM favourites_links l INNER JOIN favourites_categories c ON l.cat_id = c.id ORDER BY cat_id") or die(mysql_error());

	// count the rows
	$num_rows = mysql_num_rows($resource);

	// maximum number of rows to show per column
	$max_rows = ceil($num_rows / 3);

	// starting row count
	$row_count = 1;

	echo '<div class="column">';

	while ($current_page = mysql_fetch_assoc($resource)) {	

		if ($row_count < $max_rows) {

			if (!isset($category) || $category != $current_page['cat_title']) {						
				$category = $current_page['cat_title'];
				echo '<h2>'.$category.'</h2>';			
			}

			echo '<a href="http://'.$current_page['link'].'">'.$current_page['title'].'</a>'."\n";

		}

		else {

			echo '</div><div class="wrapper">';
			$row_count++;

		}


	}

	echo '</div>';

 

Any help would be greatly appreciated!

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.