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
https://forums.phpfreaks.com/topic/107170-getting-titles/
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
https://forums.phpfreaks.com/topic/107170-getting-titles/#findComment-549822
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.