PDA

View Full Version : Quicker way in PHP to display database table data



tc101
August 25th, 2008, 08:39 PM
In php, if I read data from a table, for example "select * from table1" and want to display the data on a web page, do I have to loop through the results and format each line using <tr> and <td> tags, or is there some quicker way to display the data?

LaRoza
August 25th, 2008, 08:44 PM
Not sure what you want. It would be a single foreach loop. Doesn't get much shorter than that.

tc101
August 25th, 2008, 08:54 PM
Thanks LaRoza. No problem doing that, but if there was some simple onword way to do it I wanted to know. I am new to this.

tc101
August 25th, 2008, 09:05 PM
I wrote the following code below, and it echos each value twice. Any idea why?

while ($row = mysql_fetch_array($result)) {
echo '<p> ';
foreach ($row as $value) {
echo $value. " ";
}
}

Reiger
August 25th, 2008, 09:25 PM
Yes there is:



Note

It is possible to obtain XML-formatted output from MySQL in the mysql and mysqldump clients by invoking them with the --xml option. See Section 4.5.1, “mysql — The MySQL Command-Line Tool”, and Section 4.5.4, “mysqldump — A Database Backup Program”.

-- From: http://dev.mysql.com/doc/refman/5.1/en/xml-functions.html

Which entails... You can test how your MySQL db returns the stuff; then use an XSLT stylesheet to format it into w/ever you want it to end up looking like.

Then echo the result within the webpage.

So: fetch XML formatted results instead of raw text; grab formatXML($row,$style,$isBody); echo it as you see fit.

From a PHP file I wrote a while ago:


function formatXML($doc,$style,$isBody) {

// Load the XML
$xml = new DomDocument;
$xml ->load($doc);

if($style!='xhtml') {
// Load the XSLT
$xslt= new DomDocument;
$xslt->load($style);

// Transform the XML to XHTML using the XSLT
$proc= new XSLTProcessor;
$proc->importStylesheet($xslt);
$xhtml= $proc->transformToXML($xml);
}
/**
@ Note allowing raw XHTML to pass through the function 'unharmed'.
**/
else
$xhtml=$xml->saveXML();

if($xhtml)
return trimXHTML($xhtml);
else
if($isBody)
return '<body><div><h2>XSLT Processing Failed!</h2></div></body>';
else
return '<div><h2>XSLT Processing Failed!</h2></div>';
}

function trimXHTML($xhtml)
{
return preg_replace("/[\t\n]|&#\d+;|<\\?.*\\?>/",'',$xhtml);
}


Note that formatXML() was written to keep returning valid XHTML hence the $isBody switch (in case the bit formatted was required to act as <body></body> section too).