Adding column names when using database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wardy7
Forum Commoner
Posts: 38
Joined: Wed Aug 24, 2005 4:45 am
Location: UK

Adding column names when using database

Post by Wardy7 »

I need what I can only think is a very simple thing doing.

I have a table of data with 6 columns in that displays all the data in 6 colums when I call it fromthe database.
What I want is to add code so I can list the title for each column at the top. I want to do this from within this code instead of doing it by hand.

Does this makes sence, if so can anyone point me in the right direction, ta :)

Code: Select all

***snip***

    if(mysql_num_rows($result) == 0){ 
        echo("No articles yet! Why not be the first to submit one?!"); 
    } 

    $bgcolor = "#FFFFFF"; //
    $bgcolor2 = "#FBBC00"; //
    $bgcolor3 = "#FFCC33"; //
    $bgcolor4 = "#FFDF80"; //
    $bgcolor5 = "#FFEEB9"; //
    $bgcolor6 = "#FFF7DD"; //
    $bgcolor7 = "#FFFBEC"; //
	$font =    "Verdana, Arial, Helvetica, sans-serif"; //
    $fontsize = "2"; //
    $tdwidth = "120"; //
    $tdwidth2 = "90"; //
    $tdwidth3 = "100"; //

    echo("<table>"); 
      
    while($row = mysql_fetch_array($result)){ 
        if ($bgcolor == "#FFFFFF"){ 
            $bgcolor = "#FFFFFF"; 
        }else{ 
            $bgcolor = "#FFFFFF"; 
     } 

    echo("<tr bgcolor=".$bgcolor.">\n<td>");
    echo("<td width=".$tdwidth." bgcolor=".$bgcolor2.">");
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo($row["deal"]);
    echo("<td width=".$tdwidth2." bgcolor=".$bgcolor3.">");
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo($row["speed"]);
    echo("<td width=".$tdwidth3." bgcolor=".$bgcolor4.">");
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo('£' . $row["cost"] . '');
    echo("<td width=".$tdwidth3." bgcolor=".$bgcolor5.">");
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo($row["age"]);
    echo("<td width=".$tdwidth3." bgcolor=".$bgcolor6.">");
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo($row["length"]);
    echo("<td width=".$tdwidth3." bgcolor=".$bgcolor7.">");
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo($row["offer"]);
    } 

    echo("</table>");
?>
Cheers
Wardy
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The information is available from the keys of the array returned from mysql_fetch_array().
Post Reply