469,891 Members | 2,285 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,891 developers. It's quick & easy.

mysql to html table php function or class

Hi all,

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

something like.

createtable($query,fieldname1,fieldname2,fieldname 3,[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.
Jul 4 '08 #1
3 1635
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,fieldname 3,[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.
js*******@attglobal.net
==================

Jul 4 '08 #2
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_i d,
$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....
Jul 4 '08 #3
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_i d,
$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.
js*******@attglobal.net
==================

Jul 4 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by dave | last post: by
reply views Thread by Mike Chirico | last post: by
Atli
6 posts views Thread by Atli | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.