Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-25-2011, 08:48 AM   PM User | #1
bngari
New Coder

 
Join Date: Nov 2010
Location: Kenya
Posts: 38
Thanks: 3
Thanked 1 Time in 1 Post
bngari is an unknown quantity at this point
Display mysql data in a dynamically generated table

Hi, i want to display data fetched from the database in a 3x3 table or a 4x3 table but i'm having trouble looping the rows correctly. Here's 2 sample code

PHP Code:
echo "<table border=\"0\">";
//...some code
$query mysql_query($sql);
while (
$row mysql_fetch_array($query) {
//loop table rows and columns here
}
echo 
"</table>"

Thanks in advance.
__________________
Write an Essay, Get all your best web articles.
bngari is offline   Reply With Quote
Old 03-25-2011, 09:26 AM   PM User | #2
cyrus709
Regular Coder

 
cyrus709's Avatar
 
Join Date: Dec 2008
Posts: 102
Thanks: 22
Thanked 5 Times in 5 Posts
cyrus709 is an unknown quantity at this point
PHP Code:
echo "<table border=\"0\">"
//...some code 
$query mysql_query($sql); 
while (
$row mysql_fetch_array($query) { 
//loop table rows and columns here 
echo "<tr><td>" $row['my_row_name_here'] . "</td></tr>";

echo 
"</table>"
Basically just add your html for the table data every time you loop.
__________________
Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.
cyrus709 is offline   Reply With Quote
Old 03-25-2011, 10:48 AM   PM User | #3
bngari
New Coder

 
Join Date: Nov 2010
Location: Kenya
Posts: 38
Thanks: 3
Thanked 1 Time in 1 Post
bngari is an unknown quantity at this point
Here's a sample of how i want data to be displayed
[HTML]
<table border="0">
<tr><td>item1</td><td>item2</td><td>item3</td></tr>
<tr><td>item4</td><td>item5</td><td>item6</td></tr></table>[/HTML]
__________________
Write an Essay, Get all your best web articles.
bngari is offline   Reply With Quote
Old 03-25-2011, 10:55 AM   PM User | #4
bngari
New Coder

 
Join Date: Nov 2010
Location: Kenya
Posts: 38
Thanks: 3
Thanked 1 Time in 1 Post
bngari is an unknown quantity at this point
Quote:
Originally Posted by cyrus709 View Post
PHP Code:
echo "<table border=\"0\">"
//...some code 
$query mysql_query($sql); 
while (
$row mysql_fetch_array($query) { 
//loop table rows and columns here 
echo "<tr><td>" $row['my_row_name_here'] . "</td></tr>";

echo 
"</table>"
Basically just add your html for the table data every time you loop.
the loop you stated will loop a pre-determined number of rows or columns. I want a loop statement that will allow each item to be in its own cell. Just like how online catalogues display their products. Here's a sample of how i want data to be displayed

<table border="0">
<tr>
<td>item1</td>
<td>item2</td>
<td>item3</td>
</tr>
<tr>
<td>item4</td>
<td>item5</td>
<td>item6</td>
</tr>
</table>
__________________
Write an Essay, Get all your best web articles.
bngari is offline   Reply With Quote
Old 03-25-2011, 11:03 AM   PM User | #5
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Assuming each row in $query contains just 1 field, then one option is to set a counter going from 1 to the number of items you want on each html table row.

So in your case you want 3 items per <tr>. When the counter reaches 3, close the <tr>, reset the counter to 1, open another <tr> and continue looping through $query
bullant is offline   Reply With Quote
Old 03-25-2011, 05:31 PM   PM User | #6
bngari
New Coder

 
Join Date: Nov 2010
Location: Kenya
Posts: 38
Thanks: 3
Thanked 1 Time in 1 Post
bngari is an unknown quantity at this point
Quote:
Originally Posted by bullant View Post
Assuming each row in $query contains just 1 field, then one option is to set a counter going from 1 to the number of items you want on each html table row.

So in your case you want 3 items per <tr>. When the counter reaches 3, close the <tr>, reset the counter to 1, open another <tr> and continue looping through $query

no, each query has more details relating to each item such as photo n price
__________________
Write an Essay, Get all your best web articles.
bngari is offline   Reply With Quote
Old 03-25-2011, 06:00 PM   PM User | #7
cyrus709
Regular Coder

 
cyrus709's Avatar
 
Join Date: Dec 2008
Posts: 102
Thanks: 22
Thanked 5 Times in 5 Posts
cyrus709 is an unknown quantity at this point
Quote:
Originally Posted by bngari View Post
no, each query has more details relating to each item such as photo n price

Does each row that you are retrieving contain more then one detail? or are there multiple rows each one retrieving a different detail? ex a row for price another row for photo
If it is the latter do what bullant said (as that was a repeat of his question). Otherwise we will need more information about your script. such as how your splitting up the information from a row and where it needs to go.
__________________
Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.
cyrus709 is offline   Reply With Quote
Old 03-25-2011, 10:47 PM   PM User | #8
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by bngari View Post
no, each query has more details relating to each item such as photo n price
ok, then it looks like you're only drip feeding information about what your data looks like and since my crystal ball isn't back from its 1000 year service yet, I can't use it to see exactly what you have in your code and data. Hopefully someone else will be able to help.
bullant is offline   Reply With Quote
Reply

Bookmarks

Tags
mysql, table

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:41 PM.


Advertisement
Log in to turn off these ads.