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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Putting data into a table

    Hi,

    I am displaying data from a database.
    I want it to look neater on the html page.
    Is there a way of inserting the data from the database into a dynamic table.
    How would i do that?

    Thank you

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    Here is a generic function that accepts a resource result variable and outputs a table -
    PHP Code:
    function Wrap($result) {

        
    // Printing results in HTML
        
    echo "<table border=1>\n";
        echo 
    "\t<tr>\n";
        
    /* get column metadata */
        
    $i 0;
        while (
    $i mysql_num_fields($result)) {
            
    $meta mysql_fetch_field($result$i);
            echo 
    "\t\t<td><b>".$meta->name."</b></td>\n";
            
    $i++;
        }
        echo 
    "\t</tr>\n";
        while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
            echo 
    "\t<tr>\n";
            foreach (
    $line as $col_value) {
                echo 
    "\t\t<td>$col_value</td>\n";
            }
            echo 
    "\t</tr>\n";
        }
        echo 
    "</table>\n";  
    // end of wrap function 
    Typical usage -
    PHP Code:
    $query 'SELECT * FROM your_table';
    $result mysql_query($query) or die('Query failed: ' mysql_error());
    wrap($result); 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks thats great

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    190
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Hi

    Thanks CFMaBiSmAd...its great!! One thing though...at the moment the attribute name sets the size of the column which means any value under this attribute longer than the attribute name will result in the value displayed on two lines. I was wondering if it is possible to prevent this so all values will be set one line? For example maybe letting the values set the column size rather than the attribute. Ive been trying to modify it but to no avail. Let me know if im not making sense!

    Any ideas? Thanks for all your help


  •  

    Posting Permissions

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