Forum Moderators: coopster

Message Too Old, No Replies

Data access class - loops?

         

Nutter

11:43 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



I'm working on a class to access a MySQL database. One function I'd like to have is one that lists the records in a table. But, I'd like to be able to read from a loop.

Ex:
while ($x = $class->function())
{
echo $x['fieldname'];
}

How would I go about doing this?

Thanks...

coopster

10:41 pm on Feb 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess if I were going to go about this I might just have a method in the class that would format the result set in an HTML table. Something that would accept a result identifier that had already been set in the class instance and loop through and format.

Moosetick

10:44 pm on Feb 2, 2006 (gmt 0)

10+ Year Member



I grabbed this straight from from php.net. It does what you want..

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";