PDA

View Full Version : populating a table from a database


Vitesse
08-06-2005, 07:17 AM
K guys, i've managed some of my coding ok so far but now im seriously stuck and need lots of help :ermm:

I've got a separate page based on the forum style which is a quarter-mile timechart. I need to populate a table with the contents of a database and havnt a clue how to do it :o

basically the table in the template has 3 columns, Username, Car, Quarter-mile time. The database table (it's a separate mysql database) has one table "quartermile" which has 4 fields which are

1. "ID", Int, auto-incremented, primary field
2. "username", varchar,
3. "car", varchar
4. "qttime", Decimal

I just need to get the entire content of that DB table displayed on a table on my site (it's my test site so cock-ups/errors wont cause any harm).

Any assistance will be immensely appreciated as i've been working on this all day, every day for the last week and seem to be getting nowhere fast.

Many thanks

------------------------------- EDIT ------------------------------------
OK, worked out how to retrieve the data from the database just need some instruction on how to get the data into a table properly :)

Anyone????


all i want to do is retrieve all rows and put them in a table

Vitesse
08-07-2005, 02:43 PM
^Bump

The only thing that's holding me back from going any further is not getting the data from the DB into the table. Surely someone must know how to do this?

Marco van Herwaarden
08-07-2005, 07:26 PM
It will depend on if you will be using vB Templates or plain html from your php-script.

Marco van Herwaarden
08-07-2005, 07:31 PM
Basic setup would be:

$result = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "tablename");
while ($myrow =$db->fetch_array($result))
{
.....here either eval the template, or echo your fields
}

Vitesse
08-07-2005, 08:23 PM
I'm going to use the template rather than just in HTML as it would look better that way (i hope) that's the bit i couldnt figure out, if i were doing it in just php and outputting as html i'd make a loop for the number for total rows in the DB but how would i do the equivalent and get it into the template?

Marco van Herwaarden
08-07-2005, 08:34 PM
$mydatabits = '';
$result = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "tablename");
while ($myrow =$db->fetch_array($result))
{
eval('$mydatabits .= "' . fetch_template('mydatabit') . '";');
}
eval('print_output("' . fetch_template('mypage') . '");');

Create 2 templates:
'mydatabit':
Holeding the detail lines, maybe something like:
<tr><td>Username: $myrow[username]</td><td>Car: $myrow[car]</td></tr>
And a second template to hold the rest of the page, including the <table> tags etc. In between the <table> and the </table> you will place the variable $mydatabits

Vitesse
08-07-2005, 09:31 PM
Thankyou ever so much :) :) it worked like a charm, eternally grateful to you sir