Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to : Table with PHP?

Status
Not open for further replies.

HollyVally

Programmer
Jan 3, 2003
48
BD
Hi All,

I need to create HTML table on the fly. Although I have done in a hard way(encoding the table as strings & putting couple of loops around the different parts of it!), is there any built in way of doing it in PHP? Like including some specific module or something?

Cheers all.
 
Actually it isn't. What you could do is to multiply include the parts that are repeating.
 
Search the script archives.
There are some classes that do things like you want to do.
PHP itself has no functions to create HTML output.

Look into templates if you want to separate HTML and PHP. It's worthwhile in the long run.
 
I use a function for database tables, as they are always different number of columns or rows.

once you've done you query call
DrawResult($result);


add this somewhere:
///////////////////////////////////////////////////////////////////////////////
function DrawResult($my_result){

$columns=@mysql_num_fields($my_result);

// figure out how many columns are in it and do the table to fit.


echo &quot;<table align=\&quot;center\&quot; border=\&quot;0\&quot; cellspacing=\&quot;1\&quot; cellpadding=\&quot;0\&quot; width=\&quot;98%\&quot;>\n&quot;;

$row=0;

// display field names

// print &quot;<th>row</th>&quot;;

// for ($i = 0; $i < mysql_num_fields($my_result); $i++) {

// print &quot;<th>&quot;.mysql_field_name($my_result,$i).&quot;</th>\n&quot;;

// }

while ($myrow = mysql_fetch_array($my_result)){

// start the results display table

$row++;

if($row%2){

$color=&quot;#ECECEC&quot;;

}else{

$color=&quot;#D1DEFD&quot;;

}

echo &quot;<tr><td width=20>$row</td>&quot;;

for ($i = 0; $i < ($columns); $i++) {

echo &quot;<td bgcolor=\&quot;$color\&quot;> $myrow[$i] </td>\n&quot;;

}

echo &quot;</tr>\n&quot;;

}


mysql_free_result($my_result);

echo &quot;</table>\n&quot;;
}

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top