The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Displaying MYSQL database table into PHP table
joyful
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
joyful
post Apr 9 2011, 02:26 PM
Post #2


Advanced Member
****

Group: Members
Posts: 239
Joined: 15-November 10
Member No.: 13,147



Thanks Brian,
Yes when I try 7 items it made a line with 4.
So, I changed $col to 1 (like you said):
CODE
$col = 1;

Now it works great!

Thanks again!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic
joyful   Displaying MYSQL database table into PHP table   Mar 30 2011, 11:43 PM
Frederiek   The following article may be of help: http://devzo...   Mar 31 2011, 01:47 AM
joyful   Hey, Thanks Frederiek that page was very helpful....   Mar 31 2011, 02:06 PM
Brian Chandler   I really think you need to go and read some tutori...   Mar 31 2011, 10:49 PM
joyful   Hey, Thanks a lot for the help/code Brian. Though...   Apr 1 2011, 01:19 AM
Brian Chandler   Probably I made a mistake. So the thing to do is d...   Apr 1 2011, 11:17 AM
joyful   I know this a very lame question... How do you do ...   Apr 1 2011, 12:07 PM
Darin McGrew   Here's what I see, based on the what the print...   Apr 1 2011, 12:56 PM
joyful   Here's what I see, based on the what the prin...   Apr 1 2011, 01:01 PM
Brian Chandler   (Thanks Darin!) Yes, this is a very common so...   Apr 1 2011, 02:19 PM
Darin McGrew   As Brian pointed out, this is an off-by-one error,...   Apr 1 2011, 04:04 PM
joyful   Hey, Thanks a lot. I now see the problem and have...   Apr 1 2011, 04:53 PM
Brian Chandler   That is Not Good! Obviously "COLS...   Apr 1 2011, 11:57 PM
joyful   Hey, I do not quite under stand were I should put ...   Apr 2 2011, 10:50 AM
Brian Chandler   You seem to have copied the "wrong" vers...   Apr 2 2011, 12:10 PM
joyful   Hey, Tell me if I am completely misunderstanding h...   Apr 3 2011, 02:47 PM
Darin McGrew   The first time through the loop, $col starts ...   Apr 3 2011, 07:12 PM
joyful   Yes, I (now) understand, But what can I do to fix ...   Apr 3 2011, 09:16 PM
Darin McGrew   Move the "$col is incremented" step...   Apr 3 2011, 10:36 PM
joyful   Hey, Thanks so much, this worked. Sorry it took me...   Apr 4 2011, 04:14 PM
Brian Chandler   Hey, Thanks so much, this worked. Well, you st...   Apr 5 2011, 12:22 AM
joyful   Hey, Is this right (I do not think so): define...   Apr 5 2011, 12:06 PM
Brian Chandler   I can't understand what you can't understa...   Apr 5 2011, 12:55 PM
joyful   Sorry, I meant to say "a extra <tr>...   Apr 6 2011, 12:15 AM
Brian Chandler   Sorry, I'm not going to write your program for...   Apr 6 2011, 10:58 AM
joyful   Um, perhaps by putting it in the right place... We...   Apr 6 2011, 12:54 PM
Darin McGrew   That looks like a different off-by-one error. With...   Apr 6 2011, 08:32 PM
joyful   Here is the current php: define('COLS...   Apr 6 2011, 09:38 PM
Darin McGrew   You're incrementing $col in two places. T...   Apr 6 2011, 10:59 PM
joyful   Hey, If I remove "else $col++;" fr...   Apr 6 2011, 11:11 PM
Brian Chandler   Hey, If I remove "else $col++;" f...   Apr 7 2011, 12:57 AM
joyful   Sorry if I am being repetitive. Let me try to unde...   Apr 7 2011, 01:18 AM
Brian Chandler   We seem to be making slight progress now... ...   Apr 8 2011, 02:24 AM
joyful   Hey, Thanks so much Brian (and Darin) for your hel...   Apr 8 2011, 01:11 PM
Brian Chandler   Have you tested this with seven items being output...   Apr 9 2011, 01:38 AM
joyful   Thanks Brian, Yes when I try 7 items it made a lin...   Apr 9 2011, 02:26 PM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th March 2024 - 02:47 AM