Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Column Titles

  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Column Titles

    Hi all,
    I'm only now breaking myself into the coding world but am STUMPED on what to do here. Basically all I want to do is add the column titles to the table that is generated by the following code... any help would be gratefully recieved!

    Thanks

    Lloyd.


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Display Contracts</title>
    <link rel="stylesheet" type="text/css" href="contractapp.css" />
    </head>
    <body>
    <div id="container" class="containerDiv">
    <div class="headerDiv"><h1>Display Contracts</h1></div>
    <div class="menuDiv"><a href="home.html">Main menu</a></div>
    <div class="contentDiv">

    <!-- *********************** START OF MAIN CONTENT *********************** -->

    <?php
    include("dbinfo.inc.php");
    $comm=@mysql_connect(localhost,$username,$password);
    $rs=@mysql_select_db($database) or die( "Unable to select database");
    $sql="SELECT * FROM contract";
    $result = mysql_query($sql) or die("SELECT Error: ".mysql_error());
    echo "<table class=\"table\">\n";
    while ($get_info = mysql_fetch_row($result))
    {
    echo "<tr>\n";
    foreach ($get_info as $field)
    echo "<td class=\"table\">&nbsp$field&nbsp&nbsp</td>\n";
    echo "</tr>\n";
    }
    echo "</table>\n";
    mysql_close();
    ?>
    </div>

    <!-- ************************ END OF MAIN CONTENT ************************ -->

    <div class="footerDiv">Footer Text</div>
    </div>
    </body>
    </html>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its a relatively simple task to do this. You can grab the name using the mysql_field_name function:
    Code:
    print('<tr>');
    for ($i = 0; $i < mysql_num_fields($sql); ++$i)
    {
        printf('<td>%s</td>', mysql_field_name($sql, $i));
    }
    print('</tr>');
    Also, in the future please wrap your code in [php][/php] or [code][/code] tags to preserve the formatting.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    idewar (03-12-2013)

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Great thanks, sorry I didn't know about the tags.

    Also where in relation to my code do I add the code you gave me, sorry for being such a major novice!

    thanks again!
    Last edited by idewar; 03-12-2013 at 08:04 PM. Reason: spelling

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'd put it before the while loop, then the while loop will populate the recordset with the data.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hey thanks for the reply this is helping me out a lot! I popped you'r code into the SQL but when it displays the results its as if that code doesn't exist ie. no title

    any thoughts thanks again

    lloyd

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    But I would like to suggest an alternate answer.

    When you do SELECT * FROM contract that means you are getting *ALL* the columns from that contract table.

    Very seldom will you do this in real life.

    Suppose that contract had the following columns:
    contractid -- INT AUTO_INCREMENT, just for internal control
    contractdate
    company
    address
    city
    state
    zip
    phone
    contactperson
    contractlength
    contractdeatils LONGTEXT (e.g., the full text of the contract)

    *SURELY* you don't want to dump out *ALL* those fields. Especially the contractdetails and the contractid, say.

    IN GENERAL, you should *AVOID* using SELECT * and, instead, select *ONLY* those fields you really need and want to show in your report. On top of that, you often want the fields to be in a different order in the report than they are in the DB. And, finally, you often want the column titles to be more human readable than the names in the db. So you might do something like
    Code:
    SELECT company, contactperson, phone, contractdate, contractlength 
    FROM contract
    ORDER BY company
    And now you don't *NEED* to get the names from the query using mysql_field_name.

    Instead, just use HTML and code it by hand:
    Code:
    <html>
    <body>
    <table>
    <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Phone</th>
        <th>Start date</th>
        <th>Length</th>
    </tr>
    <?PHP
    $sql = "SELECT company, contactperson, phone, contractdate, contractlength 
            FROM contract
            ORDER BY company";
    ... etc ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Fou-Lu used the wrong variable, $sql. To match your code, he needed to use $result.

    Code:
    $result = mysql_query($sql) or die("SELECT Error: ".mysql_error());
    ...
    print('<tr>');
    for ($i = 0; $i < mysql_num_fields($result); ++$i)
    {
        printf('<td>%s</td>', mysql_field_name($result, $i));
    }
    print('</tr>');
    (And I don't even use PHP. <grin/>)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    idewar (03-12-2013)

  • #8
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi there i appreciate the answer, however i do need to the report to show all columns, and there are a few tables I've already done that display without the headings, it would be great to have a quick fix if one exists,
    again i appreciate the response.

    Lloyd

  • #9
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    old pendant perfect thanks!!! however they display like this....

    contractidnamecustomernumbertelephone

    rather than

    contract id | name | customer number | telephone |

    any thoughts

    thanks!

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    rofls yep I sure did use the wrong one.
    Check the source code, does it show its blocked in the tr and td tags? If so, just make sure it's blocked by the table tags.
    I also agree to use explicit naming. I rarely name my properties exactly how I want them to display (ie: pid and aid don't really make any sense in a display name).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Here. Let's write it out for you. You surely just have it in the wrong place.
    Code:
    <?php
    include("dbinfo.inc.php");
    $comm=@mysql_connect(localhost,$username,$password);
    $rs=@mysql_select_db($database) or die( "Unable to select database"); 
    $sql="SELECT * FROM contract";
    $result = mysql_query($sql) or die("SELECT Error: ".mysql_error());
    
    echo "<table class=\"table\">\n";
    
    echo "<tr>\n";
    for ($i = 0; $i < mysql_num_fields($result); ++$i)
    {
        printf("<td>%s</td>\n", mysql_field_name($result, $i));
    }
    echo "</tr>\n";
    
    while ($get_info = mysql_fetch_row($result))
    { 
        echo "<tr>\n";
        foreach ($get_info as $field) 
        {
            echo "<td class=\"table\">&nbsp$field&nbsp&nbsp</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
    mysql_close();
    ?>
    It seems VERY VERY STRANGE, to me, that you would give your <table> the same class that you give a <td>. *SURELY* <td class="table"> is an error?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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