get_column() can help, it will compute a column for each element that you want to show.
this example script shows how to print in two columns, but you can change it in two minutes to fix your needs if you need a different number of columns.
<?php // sample query, get members $query = mysql_query("select name from persons"); // count total number of items to show $total = mysql_num_rows($query); // initiate row counter $counter = 1; // initiate columns contents $column_1 = ''; $column_2 = ''; while($row = mysql_fetch_assoc($query)){ // caluculate column for current element from total items to be showed number of columns and current item $column = get_column($total, 2, $counter); if($column == 1){ $column_1 .= $row['name'].'<br>'; } if($column == 2){ $column_2 .= $row['name'].'<br>'; } $counter++; } // show content in two table comments echo "<table> <tr> <td>$column_1</td> <td>$column_2</td> </tr> </table>"; ?>
and function:
<?php /** * Calculate column number where an item should be displayed on a "newspaper style" * or "phoneguide style" report according its postion * used to put same number of items on each column * * receive 3 numbers: $vp_total_size: total number of items on report * $vp_columns : number of columns of report * $vp_element : element position for item (1 to $vp_total_size) * * by Marcos A. Botta <marcos DOT botta AT gmail DOT com> * 02/02/2007 * */ function get_column($vp_total_size, $vp_columns, $vp_element){ if($vp_element <= 0){ return 1; } if($vp_element < $vp_columns && $vp_columns >= $vp_total_size){ return $vp_element; } $vl_avg_items_by_column = $vp_total_size / $vp_columns; $vl_items_on_first_columns = ceil($vl_avg_items_by_column); $vl_items_on_last_columns = floor($vl_avg_items_by_column); $vl_column_limit = ($vl_avg_items_by_column - $vl_items_on_last_columns) * $vp_columns; $vl_allocated_items = 0; for($i=1;$i<$vp_columns;$i++){ if($i < $vl_column_limit || "$i" == "$vl_column_limit"){ $vl_items_on_current_column = $vl_items_on_first_columns; } else{ $vl_items_on_current_column = $vl_items_on_last_columns; } $vl_allocated_items += $vl_items_on_current_column; if($vp_element <= $vl_allocated_items){ return $i; } } return $vp_columns; } // get_column() ?>
Good luck
source share