SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I do this?

    How can I list the fields of a table? I want to create a generic function that can create an html table containing the field names and data contained in a table.

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By table do you mean database table? If so, what database?

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Further explanation

    I have a database table called webpages, which has seven fields. I want to call a function and create an html table with the field names as cells of the first row and then have the data appear in the remaining rows.

  4. #4
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wee!

    Click the link in my signature.
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not quite it.

    Milamber, that's close but not quite it.

    It should have a query that looks something like this:

    select field_name from table_fields where table_name = 'webpages';

  6. #6
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Not quite it.

    Originally posted by rgremill
    Milamber, that's close but not quite it.

    It should have a query that looks something like this:

    select field_name from table_fields where table_name = 'webpages';

    you can change the query however you'd like, i was just giving you an example of how to display your query as a table.

    Silly,
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    My main question is how do I get the database to tell me the columns of a table. I think I can use the show command for this. I need to do more research.

  8. #8
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Question

    Originally posted by rgremill
    My main question is how do I get the database to tell me the columns of a table. I think I can use the show command for this. I need to do more research.

    PHP Code:


        $x 
    mysql_num_fields($xresults);
        
    $i 0;
        while (
    $i $x ) {
            
    $meta mysql_fetch_field($xresults);
            echo (
    $meta->name <br>\n" );
            
    $i++;
        } 

    that'll give you the columns from a sql query.
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Let me try it out.
    Last edited by rgremill; Jul 4, 2001 at 19:10.

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OK, here's the answer...

    Milamber, thanks for all of your help. Here's the answer I came up with the help of your code. It's a little ornate...


    function get_html_table($db_conn,$tablename)
    {

    $query = "select * from $tablename";
    $xresults = mysql_query($query,$db_conn);
    $x = mysql_num_fields($xresults);


    $html_string .= "<table border=0 cellpadding=1 cellspacing=0 bgcolor=#CCCCCC width=100%>";
    $html_string .= "<TR><TD>";
    $html_string .= "<table border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC width=100%>";
    $html_string .= "<TR bgcolor=#FFCC66><TD>&nbsp;</TD>";
    $i = 0;
    while ($i < $x ) {
    $meta = mysql_fetch_field($xresults);
    $html_string .= ("<TD><B>$meta->name</B></TD>" );
    $i++;
    }
    echo "</TR>";

    $row_count = 1;
    $background_color="#FFFFFF";
    while ( $row = mysql_fetch_array($xresults) ) {
    $html_string .= "<tr bgcolor='$background_color'>";
    $html_string .= "<TD>&nbsp;$row_count&nbsp;</TD>";
    for ($y = 0; $y < $x; $y++) {
    $html_string .= "<td>$row[$y]</td>";
    }
    $html_string .= '</tr>';
    $row_count ++;

    if ($background_color == "#EEEEEE")
    {
    $background_color = "#FFFFFF";
    } else {
    $background_color = "#EEEEEE";
    }
    }


    $html_string .= "</TABLE>";
    $html_string .= "</TD></TR></TABLE>";

    return $html_string;
    }
    Last edited by rgremill; Jul 4, 2001 at 19:52.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •