dani0999 Posted September 6, 2007 Share Posted September 6, 2007 HI Guys, i found this forum very helpful in and hope somebody will be able to help me out. i have the below code which displays all result per row on one column. however, i want it such that it displays results per 4 columns per row. for example if my query returns 8 results you'd have something like this: column1 column2 column3 column4 1 2 3 4 5 6 7 8 instead of this: 1 2 3 4 5 etc... here is my current code: // get the book info out from db $book_array = get_books($catid); display_books($book_array); function display_books($book_array) { //display all products in the array passed in if (!is_array($book_array)) { echo '<br />Coming soon. Please check again<br />'; } else { //create table echo '<table width = \"100%\" border = 1 cellpadding = 0>'; //create a table row for each book foreach ($book_array as $row) { $cattype = $_GET['cattype']; $psection = $_GET['psection']; //$cattype = $HTTP_GET_VARS['cattype']; $url = 'show_book.php?prdnumber='.($row['prdnumber']).'&show_cat.php?catid='.($row['catid']).'&cattype='.$cattype.'&psection='.$psection; echo '<tr>'; echo'<td>'; if (@file_exists('images/'.$row['prdnumber'].'.jpg')) { $title = '<img src=\'images/'.($row['prdnumber']).'.jpg\' border=0>'; do_html_url($url, $title); } else { echo ' '; } echo'<br>'; $title = $row['title'].' by '.$row['make'].' price '.number_format($row['price'], 2); do_html_url($url, $title); echo '</td></tr>'; } echo '</table>'; } //echo '<hr />'; } my select query function get_books($catid) { // query database for the books in a category if (!$catid || $catid=='') return false; $conn = db_connect(); $psection = $_GET['psection']; $query = "SELECT products.prdnumber, products.make, products.title, products.price, products.description, products.catid, products.supplier_id FROM parent_cat, categories, products WHERE products.catid = '$catid' AND parent_cat.psection='$psection' AND products.catid=categories.catid AND categories.pid=parent_cat.catid"; $result = @mysql_query($query); if (!$result) return false; $num_books = @mysql_num_rows($result); if ($num_books ==0) return false; $result = db_result_to_array($result); return $result; } thanks dan Quote Link to comment https://forums.phpfreaks.com/topic/68198-solved-display-result-in-4-columns-per-row-help/ Share on other sites More sharing options...
AdRock Posted September 6, 2007 Share Posted September 6, 2007 I have something that does what you want for my thumbnails gallery. I hope this helps <?php define ("NUMCOLS",4); $count = 0; $counter= 1; echo "<table border='0'>"; while (list($id,$thumb,$cat) = mysql_fetch_row($res)) { if ($count % NUMCOLS == 0) echo "<tr>\n"; # new row echo "<td>Whatever you want in here</td>\n"; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } # end row if not already ended if ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</tr>\n"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68198-solved-display-result-in-4-columns-per-row-help/#findComment-342874 Share on other sites More sharing options...
dani0999 Posted September 6, 2007 Author Share Posted September 6, 2007 thanks i'll give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/68198-solved-display-result-in-4-columns-per-row-help/#findComment-342881 Share on other sites More sharing options...
supanoob Posted September 6, 2007 Share Posted September 6, 2007 i have something similar to this all i did was used a while loop along with $i++; anyway i aint too good at explaining so ill show you my code and you can see what i did $i=1; while ($row=mysql_fetch_array($result)){ echo "<td style=\"border: 1px solid #808080\">"; echo "</a></td>"; $i++; if ($i == 10 || $i == 19 || $i == 28) { echo "</tr><tr>"; } thats the basic of what i have, every time the loop goes round it checks to see if it is one of those numbers if it is it add a new row, and continues till i have the full 27 results echoed. Quote Link to comment https://forums.phpfreaks.com/topic/68198-solved-display-result-in-4-columns-per-row-help/#findComment-342888 Share on other sites More sharing options...
dani0999 Posted September 6, 2007 Author Share Posted September 6, 2007 thanks AdRock got it working. here is the code (if of any use) define('NUMCOLS', 4); $count = 0; $counter = 1; //create table echo '<table width = \"100%\" border = 1 cellpadding = 0>'; //create a table row for each book foreach ($book_array as $row) { $cattype = $_GET['cattype']; $psection = $_GET['psection']; //$cattype = $HTTP_GET_VARS['cattype']; $url = 'show_book.php?prdnumber='.($row['prdnumber']).'&show_cat.php?catid='.($row['catid']).'&cattype='.$cattype.'&psection='.$psection; if($count % NUMCOLS == 0) //new row echo '<tr>'; echo'<td>'; if (@file_exists('images/'.$row['prdnumber'].'.jpg')) { $title = '<img src=\'images/'.($row['prdnumber']).'.jpg\' border=0>'; do_html_url($url, $title); } else { echo ' '; } echo'<br>'; $title = $row['title'].' by '.$row['make'].' price '.number_format($row['price'], 2); do_html_url($url, $title); echo '</td>'; $count++; $counter++; if($count % NUMCOLS ==0) echo '</tr>'; //end row } echo '</table>'; } //echo '<hr />'; } thanks dan Quote Link to comment https://forums.phpfreaks.com/topic/68198-solved-display-result-in-4-columns-per-row-help/#findComment-342894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.