Jump to content

getting titles


dogbiter

Recommended Posts

ok i'm useing the new 5.0.12 mysql version. and PHP Version 5.2.4-2ubuntu5.1 so my question is i know how to dynamically create table rows and fill them with what's in the table selected but i don't know how to get the titles of the columns that i'm looking at.

this is how i'm getting the info posted on the site, very basic.

        <? for ($i=0; $i<$query3; $i++){
                echo "<tr>";
                for ($d=0; $d<$query4; $d++){
                        echo "<td>".$query2[$d]."</td>";
                }
                echo "</tr>";
        }
?>
</table>

 

if you can help me get the titles above the columns i would be grateful.

Link to comment
Share on other sites

try this function

<?php
include 'db.php';    //connnection stuff

function table2Table($query) {
    $result = mysql_query($query) or die (mysql_error());

    $str = "<TABLE border='1' cellpadding='4'>\n";

    // column headings
          $str .=  "<tr>\n";
          while ($fld = mysql_fetch_field ($result)) {
                   $str .= "<th>{$fld->name}</th>\n";
          }
          $str .=  "</tr>\n";


    // list data
          while ($row = mysql_fetch_row($result)) {
          $str .=  "<tr>\n";
          foreach ($row as $field) {
          	  	   $field = $field=='' ? ' ' : $field;	
                   $str .=  "<td>$field</td>\n";
          }
          $str .=  "</tr>\n";
    }

    $str .=  "</TABLE>\n";
    
    return $str;
}

//
// call function
//
echo table2Table("SELECT * FROM tablename");               // your query here
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.