hello there, I want to echo my result from mysql database, i know how to echo it out in 1 column, but this time i want it to be in 3 or 4 columns, how do i do this. regards
What is it you are trying to output? The following will output a 4 column table with each column populated for every row.... assuming you have already prepare your sql statement. <?php print "<table>"; print "<tr><td>heading 1</td><td>heading 2</td><td>heading 3</td><td>heading 4</td></tr>"; # Or use mysql_fetch_assoc ...... while ($res = mysql_fetch_array($yourSQLresource)) { print "<tr>"; print "<td>".$res[0]."</td>"; print "<td>".$res[1]."</td>"; print "<td>".$res[2]."</td>"; print "<td>".$res[3]."</td>"; print "</tr>"; } print "</table>"; ?> PHP:
If it's one type of data and you don't know how many rows there are but you want to split it across two columns (useful for category pages etc.), you can use something like this: <?php $numOfColumns = 2; $myArray = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); // split that array/data across two columns $columns = partition($myArray, $numOfColumns); // display $index = 0; echo '<table border="1">'; for ($i = round(count($myArray) / $numOfColumns); $i != 0; $i--) { echo '<tr>'; for ($j = $numOfColumns; $j != 0; $j--) { echo '<td>'.$myArray[$index].'</td>'; $index++; } echo '</tr>'; } echo '</table>'; // this 'chunks' an array - good for splitting content across columns function partition($list, $p) { $listlen = count( $list ); $partlen = floor( $listlen / $p ); $partrem = $listlen % $p; $partition = array(); $mark = 0; for ($px = 0; $px < $p; $px++) { $incr = ($px < $partrem) ? $partlen + 1 : $partlen; $partition[$px] = array_slice( $list, $mark, $incr ); $mark += $incr; } return $partition; } ?> PHP: