Splitting SQL Results Into Three Columns


I wrote a script to seperate the results from a sql query into three columns… This is what I wrote…

[php] $listArtists = mysql_query(“SELECT name2,id FROM er_artists ORDER BY name2 ASC”);
$listArtistsNum = mysql_query(“SELECT id FROM er_artists”);

//Number of Artists
$artistNum = mysql_num_rows($listArtistsNum);
$artistNum2 = ($artistNum / 3);
$artistNum3 = number_format($artistNum2, 0);

// There is more code in here but I took it out for this post because it doesn’t really apply to the question.

$count = 0;
while($row = mysql_fetch_array($listArtists)) {
$count ++;
$getAlbumNum = mysql_query(“SELECT id FROM er_reviews WHERE artistid=’{$row[‘id’]}’”);
$albumNum = mysql_num_rows($getAlbumNum);
echo “<a href=’$PHP_SELF?c=ad&id={$row[‘id’]}’>{$row[‘name2’]} ($albumNum)
if($count % $artistNum3 == 0) {
echo “


It normally works, but every once in a while it makes four columns, with the last result in the fourth column. I don’t want that fourth column problem to ever occur.


Nick Giancola

Not quite sure what you mean by “Columns” Are you talking RETURNED columns (as in from the SQL statement) or DISPLAYED columns as in the the HTML.

If it’s the SQL, then it’s in your querry. Somehow, however, I don’t think this is it.

If it’s the Format of the HTML it could be a couple of things. Although your code snippit doesn’t show it, you seem to be only ending your elements in the table (

) and not the Rows ( ) . You may often see only 3 columns because of the physical spacing and the way the browsers Wrap your text. Without seeing the acutal table setup, it’s difficult to tell for sure.

Good luck,

Sponsor our Newsletter | Privacy Policy | Terms of Service