Click here to get back home

mysql to html table php function or class

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
mysql to html table php function or class Bob Bedford 07-04-2008
Posted by Bob Bedford on July 4, 2008, 8:09 am
Please log in for more thread options
Hi all,

wondering if does exists a mysql to html table function or class written in
PHP.

something like.

createtable($query,fieldname1,fieldname2,fieldname3,[fieldname4,fieldname5]);

the last parameter would be a link (first param = text to show, second param
= link).
It would help to have one of those...

If it doesn't exist, what would you see in such function or class ? Do you
preffer a class of function ?
at first, I may think of allowing creating the title line, add link or
images...but what else ?

Thanks for helping.



Posted by Jerry Stuckle on July 4, 2008, 11:29 am
Please log in for more thread options
Bob Bedford wrote:
> Hi all,
>
> wondering if does exists a mysql to html table function or class written in
> PHP.
>
> something like.
>
> createtable($query,fieldname1,fieldname2,fieldname3,[fieldname4,fieldname5]);
>
> the last parameter would be a link (first param = text to show, second param
> = link).
> It would help to have one of those...
>
> If it doesn't exist, what would you see in such function or class ? Do you
> preffer a class of function ?
> at first, I may think of allowing creating the title line, add link or
> images...but what else ?
>
> Thanks for helping.
>
>
>

Nope, nothing like that in PHP. Personally, I wouldn't find a lot of
use for it. The code to do such is pretty simple.

I'm afraid any class like this would be too specialized to be really
useful (i.e. can't set column widths, text attributes, etc.) or would
have so many options that it would be more complicated to use the class
than to write the code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by Bob Bedford on July 4, 2008, 11:43 am
Please log in for more thread options
> Nope, nothing like that in PHP. Personally, I wouldn't find a lot of use
> for it. The code to do such is pretty simple.
>
> I'm afraid any class like this would be too specialized to be really
> useful (i.e. can't set column widths, text attributes, etc.) or would have
> so many options that it would be more complicated to use the class than to
> write the code.

Mhhh, thanks for your reply...

first shot in 15 minutes. Not tested, not debugged....only the general idea.
This is because I help a guy to create a website with quite lot of tables
and he always loose some code in table formatting....and it's pretty hard
for him to get things right.

function createtable(){
//Parameter list. if no value, then use NULL
//1st: MySQL Query
//2nd: string containing HTML code of table parameters (width, border,
cellspacing...)
//3rd: array containing the titles of columns. If an empty array, uses the
query field name
//4th: array containing the line colors (ex: [#FFFFFF;#FFCCCC];
//5th: array of fields in the table. If NULL show all fields.
$column_count = 0;
$table_param = '';
$header = '';
$linecolors = array();
$linesplit = 0;
$arrayfields = array();
for($I=0;$I<func_num_args();$I++){
$param = func_get_arg($I);
if($param <> NULL){
switch($I){
case 0: $result_id = mysql_query($param) or die("Query error:" .
mysql_error());
$column_count = mysql_num_fields($result_id) or die("num fields
error:" . mysql_error());
break;
case 1: $table_param = $param;
break;
case 2: $header = "<TR>";
if(array_count_values($param)=0){
for ($column_num = 0;$column_num < $column_count;$column_num++)
$header .= "<TH>".mysql_field_name($result_id,
$column_num)."</TH>";
}else
foreach($param as $key=>$value)
$header .= "<TH>".$value."</TH>";
$header .= "</TR>";
break;
case 3: if(is_array($param)){
$linecolors = $param;
$linesplit = array_count_values($param);
}
break;
case 4: if(array_count_values($param)=0){
for ($column_num = 0; $column_num < $column_count; $column_num++)
array_push($arrayfields,mysql_field_name($result_id,
$column_num));
}else
foreach($param as $key=>$value)
array_push($arrayfields,$value);
break;

} //switch
} //param <> NULL
} // for

// Here the table attributes from the $table_params variable are added
echo("<TABLE $table_params >\n");
echo("$header\n");
// print the body of the table
while ($row = mysql_fetch_object($result_id)){
echo("<TR ALIGN=LEFT VALIGN=TOP>");
foreach($arrayfields as $key=>$value)
echo("<TD>$row[$value]</TD>\n");
echo("</TR>\n");
}
echo("</TABLE>\n");
}

Maybe it's worthing a try to implement or improve...

Thanks for advice....



Posted by Jerry Stuckle on July 4, 2008, 4:59 pm
Please log in for more thread options
Bob Bedford wrote:
>> Nope, nothing like that in PHP. Personally, I wouldn't find a lot of use
>> for it. The code to do such is pretty simple.
>>
>> I'm afraid any class like this would be too specialized to be really
>> useful (i.e. can't set column widths, text attributes, etc.) or would have
>> so many options that it would be more complicated to use the class than to
>> write the code.
>
> Mhhh, thanks for your reply...
>
> first shot in 15 minutes. Not tested, not debugged....only the general idea.
> This is because I help a guy to create a website with quite lot of tables
> and he always loose some code in table formatting....and it's pretty hard
> for him to get things right.
>
> function createtable(){
> //Parameter list. if no value, then use NULL
> //1st: MySQL Query
> //2nd: string containing HTML code of table parameters (width, border,
> cellspacing...)
> //3rd: array containing the titles of columns. If an empty array, uses the
> query field name
> //4th: array containing the line colors (ex: [#FFFFFF;#FFCCCC];
> //5th: array of fields in the table. If NULL show all fields.
> $column_count = 0;
> $table_param = '';
> $header = '';
> $linecolors = array();
> $linesplit = 0;
> $arrayfields = array();
> for($I=0;$I<func_num_args();$I++){
> $param = func_get_arg($I);
> if($param <> NULL){
> switch($I){
> case 0: $result_id = mysql_query($param) or die("Query error:" .
> mysql_error());
> $column_count = mysql_num_fields($result_id) or die("num fields
> error:" . mysql_error());
> break;
> case 1: $table_param = $param;
> break;
> case 2: $header = "<TR>";
> if(array_count_values($param)=0){
> for ($column_num = 0;$column_num < $column_count;$column_num++)
> $header .= "<TH>".mysql_field_name($result_id,
> $column_num)."</TH>";
> }else
> foreach($param as $key=>$value)
> $header .= "<TH>".$value."</TH>";
> $header .= "</TR>";
> break;
> case 3: if(is_array($param)){
> $linecolors = $param;
> $linesplit = array_count_values($param);
> }
> break;
> case 4: if(array_count_values($param)=0){
> for ($column_num = 0; $column_num < $column_count; $column_num++)
> array_push($arrayfields,mysql_field_name($result_id,
> $column_num));
> }else
> foreach($param as $key=>$value)
> array_push($arrayfields,$value);
> break;
>
> } //switch
> } //param <> NULL
> } // for
>
> // Here the table attributes from the $table_params variable are added
> echo("<TABLE $table_params >\n");
> echo("$header\n");
> // print the body of the table
> while ($row = mysql_fetch_object($result_id)){
> echo("<TR ALIGN=LEFT VALIGN=TOP>");
> foreach($arrayfields as $key=>$value)
> echo("<TD>$row[$value]</TD>\n");
> echo("</TR>\n");
> }
> echo("</TABLE>\n");
> }
>
> Maybe it's worthing a try to implement or improve...
>
> Thanks for advice....
>
>
>

If he's going to be doing website updates, he should learn html. Really,
about all you're doing is changing one syntax for another.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Similar ThreadsPosted
HTML table Update row PHP/MySQL January 24, 2007, 11:56 am
Trouble passing mysql table name to php function and using it! February 27, 2007, 10:43 am
Free mySQL Table Creating Off The Shelf Library Function December 1, 2005, 10:57 am
How to display database table data in a html table. January 26, 2007, 12:19 pm
mysql: how create a temp table as a copy of a existing table? July 22, 2004, 8:56 pm
Mysql fetch_field gets table alias, not real table name January 6, 2006, 8:01 am
Call class function from within another function? May 4, 2008, 12:21 pm
html table trouble April 26, 2007, 1:28 am
php blob - display in html table November 16, 2004, 2:07 pm
to insert data in a html table December 13, 2004, 11:53 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap