Displaying MYSQL database table into PHP table |
Displaying MYSQL database table into PHP table |
joyful |
Mar 30 2011, 11:43 PM
Post
#1
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Hey,
I have set up a MYSQL database and added a table with some data. I want to display this in a table in PHP that is 3 wide, like this: CODE <table width="100" border="0" cellspacing="0" cellpadding="0"> <tr> <td>item1</td> <td>item2</td> <td>item3</td> </tr> <tr> <td>item4</td> <td>item5</td> <td>item6</td> </tr> <tr> <td>item7</td> <td>item8</td> <td>item9</td> </tr> </table> and so on... Now, I have been using this PHP: CODE <?php //connect to the server $link = mysql_connect('localhost', 'root', ''); if (!$link) { die('Could not connect: ' . mysql_error()); } //connect to the database mysql_select_db(product_index); //query the database $query = mysql_query("SELECT * FROM products WHERE type = 'bracelets'"); //fetch the results / convert results into an array WHILE($rows = mysql_fetch_array($query)): $product_name = $rows['product_name']; $id = $rows['id']; $description = $rows['description']; $price = $rows['price']; $image_large = $rows['image_large']; $image_thumb = $rows['image_thumb']; $page_link = $rows['page_link']; $purchase_link = $rows['purchase_link']; echo "$product_name<br>$description<br>$price<br>$image_large<br>$image_thumb<br>$page_link<br>$purchase_link<br><br><br>"; endwhile; ?> This works great, but I what to display this on a table that is 3 wide and repeats displaying on to a another line after 3 and so on. I found (what I want) this forum post but could not figure out how to apply it to mine: http://php.bigresource.com/Track/php-C1yROxlq/ Please note, obviously I do not want the checkbox in the forum, I just the table they use. Thanks in advance. -- This post has been edited by joyful: Mar 30 2011, 11:46 PM |
Darin McGrew |
Apr 3 2011, 10:36 PM
Post
#2
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
Move the "$col is incremented" step after the "test $col" step.
|
joyful |
Apr 4 2011, 04:14 PM
Post
#3
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Hey, Thanks so much, this worked.
Sorry it took me a little to understand. Here is the code" CODE define('COLS', 3); // number of columns $col = 0; // number of the last column filled echo '<table class="inner_table">'; echo '<tr>'; // start first row while ($rows = mysql_fetch_array($result)) { $col++; echo '<td class="sub_table"><center>'; echo '<img src="', $rows[7], '" class="index_image" /><br><span class="index_list_bold">', $rows[2], '<span><br><span class="index_list"><br>', $rows[0],'<br>', $rows[4],'<br><a href="', $rows[8], '" class="index_list">More info</a></span><br>', $col, '</center></td>'; if ($col == COLS) // have filled the last row { $col = 0; echo '</tr><tr>'; // start a new one } } echo '</tr>'; // end last row echo "</table>"; Thanks again. -- |
Lo-Fi Version | Time is now: 25th April 2024 - 12:41 PM |