using a loop to display mysql results?

01-29-04, 05:15 AM
|
 |
Newbie Coder
|
|
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
using a loop to display mysql results?
Ok guys, this has been bugging me all day. What im trying to do is, pull information from a DB table, and display them in an html table that has more then one <td> per <tr>. The catch is, im using a template system, so it's kinda over my head. Basically this is how i want my info to look on the page :
http://www.sk4nk.org/images/loopexample.gif
Am i thinking right, that i have to use a loop? and if anyones willing to write up a simple code, that i can work with to get everything how i want it, i'd be so happy 
|

01-29-04, 05:36 AM
|
 |
Junior Code Guru
|
|
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi there,
Whenever I need to do this, I usually use 2 for loops (i.e. nested) - the outer keeps track of num_rows and the inner <td>'s per <tr>.
But with the given info it's hard to say if this fits your particular template engine.
Good night.
__________________
Blavv =|
|

01-29-04, 07:45 AM
|
 |
Newbie Coder
|
|
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
any tutorials? im only a php hobbiest 
|

01-29-04, 01:44 PM
|
 |
Newbie Coder
|
|
Join Date: Jan 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I got an error the first time I tried to see the image. But I'll take a guess at an answer.
If I'm just trying to display things and I don't need to know or care how many rows get shown I usually just place things inside of an open loop.
$query = "SELECT 1st_item, 2nd_item, 3rd_item FROM mydatabase";
$result=mysql_query($query) or die("Could not run the database query!");
while ($r = mysql_fetch_array($result)) {
extract ($r);
echo"<tr>\n";
echo"<td>".$1st_item."</td>\n";
echo"<td>".$2nd_item."</td>\n";
echo"<td>".$3rd_item."</td>\n";
echo"</tr>\n";
}
The "\n" acts like a typewriter carrige return starts a new line for the next item to be displayed in the source code. It makes it eaiser to read when debugging.
Hope it helps.
Goran
Added:
I just got in and saw your image. Sorry. What I passed would only work for a row to row match. The post after mine will give you what you want.
G
Last edited by Goran; 01-29-04 at 02:31 PM.
|

01-29-04, 02:12 PM
|
 |
Junior Code Guru
|
|
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi there,
Here's an example with while-for loop nest:
Please read the comments carefully.
Good luck!
__________________
Blavv =|
|

01-30-04, 06:40 AM
|
 |
Newbie Coder
|
|
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ohh thanks blaw! i'll go see if i can work with that 
|

01-30-04, 07:04 AM
|
 |
Junior Code Guru
|
|
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You're welcome. =)
Correction:
$i: Keeps track of the number of <tr>'s (rows)
$j: Keeps track of the number of <td>'s (columns)
I put them the other way around and told you to read it carefully... Excuse me.
__________________
Blavv =|
|

01-30-04, 08:35 AM
|
 |
Newbie Coder
|
|
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
mmkay, i finally got it to work.  ...but now i want to add to it ...i want 12 items to appear even if their isnt 12 rows in the database.
So just say there's 5 items in the databse ...i want them to pull up, and i also want another 7 items saying "submit yours" or whatever ..i just tried a few things then, but all it did was duplicate the items in the DB ..any advice?
|

01-30-04, 09:55 AM
|
 |
Newbie Coder
|
|
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hmmm ok, i just noticed, it's not limiting the amount of of <td>'s per column.
|

01-30-04, 02:43 PM
|
 |
Junior Code Guru
|
|
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi there, sorry about the limiting thing. You need to reset the fetched <td>s each time, which I forgot to add. But together with your request, here it is:
Note that I'm echoing right away instead of fetching it. Also, this time I tested it, so there should be no problem. If you still want to use the output-once-in-the-end approach, try reseting the fetched columns after echoing it.
Hope this helps. Sorry about the hassle.
__________________
Blavv =|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|