...

View Full Version : Function to wrap data in a table



guvenck
02-02-2007, 06:44 PM
I am sick and tired of typing table data (which number of columns are different each time) around my while loops based on MySQL queries and am looking for something like

$columns_array = array('column1name', 'column2name', 'column3name',.......)
wraptable($columns_array);

Is there a pre-made function for this that you know about?

CFMaBiSmAd
02-02-2007, 06:59 PM
You can do something like the following to get and print the column names and any number of columns -

// Printing results in HTML
echo "<table border=1>\n";
echo "\t<tr>\n";

/* get column metadata */
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
echo "\t\t<td><b>".$meta->name."</b></td>\n";
$i++;
}
echo "\t</tr>\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";

guvenck
02-02-2007, 07:11 PM
Thank you very much, your nickname is hard to type :)

I wrote the following while asking the question and it did not work as it looks like the $result does not pass through the function as I expected.





function Wrap($array,$result) {
echo '<table id="reports" cellspacing="8" cellpadding="8">';
echo '<tr>';
// table header w. column names
$i = 0;
while($i < count($array)) {
echo '<th>' . $array[$i] . '</th>';
$i++;
}
echo '</tr>';
// data
while($row = mysql_fetch_row($result)) {
echo '<tr>';
$d = 0;
while($d < count($array)) {
echo '<td>' . $row[$d] . '</td>';
$d++;
}
echo '</tr>';
}
echo '</table>';
}

guvenck
02-02-2007, 07:33 PM
If I try to turn your suggestion into a function, it does not work, any ideas why?




function Wrap($result) {

// Printing results in HTML
echo "<table border=1>\n";
echo "\t<tr>\n";
/* get column metadata */
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
echo "\t\t<td><b>".$meta->name."</b></td>\n";
$i++;
}
echo "\t</tr>\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";
}

CFMaBiSmAd
02-02-2007, 08:09 PM
On PHP 5 it works. I suspect that you are on PHP 4. If so, try changing the function definition to this, so that the parameter is passed by reference, rather than by value -

function Wrap(&$result) {

guvenck
02-03-2007, 12:03 AM
Yes, I use PHP4. And yes, it works with &.
Now I need to incorporate custom field names rather than db field names.
Thanks a lot for your help!

CFMaBiSmAd
02-03-2007, 12:25 AM
In your query, if you have column names that you want to be named something else in the results, use the AS keyword to give them an alias -

SELECT dbcolumn1 AS column1name, dbcolumn2 AS column2name, ...I believe back-ticks ` would be needed around the alias names if they contained spaces or other characters that are not normally allowed in a column identifier.

Edit: I just found an example of an alias with a space in the name that had quotes surrounding it.

Edit2: I just confirmed that both back-ticks and quotes work (PHP5/MySQL5).

guvenck
02-03-2007, 02:02 PM
Great, what about international characters?

And can I add buttons to each row such as edit, delete?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum