Alright, here's an example that I think will work. Took some of your code and added some of my own.
PHP Code:
$result = mysql_query("SELECT * FROM categories");
$numCols = 2; $numPerCol = ceil(mysql_num_rows($result) / $numCols);
// I'm just gonna put the results into an array, so we can access rows by index $categories = array(); while($row = mysql_fetch_array($result)) { $categories[] = $row['category_name']; }
echo '<table>'; for ($i = 0; $i < $numPerCol; $i++) { echo '<tr>'; // new row for ($j = 0; $j < $numCols; $j++) { $index = ($i * numCols) + $j; // if the number of rows is not evenly dividable by $numPerCol, // the last row will not be full. Then just output an empty cell if (isset($categories[$index])) { echo '<td>' . $categories[$index] . '</td>'; } else { echo '<td>-</td>'; } } } echo '</table>';
And if you want to add in pagination, you'd better look into the SQL LIMIT clause. You'll find plenty of resources if you search for "mysql limit" or similar. I can also recommend the tutorials on tizag.com.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
Last edited by lizciz; 07-02-2011 at 04:13 PM..
Reason: found small mistake in code
|