SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    have PHP output SELECTs into table form

    Guys..

    sigh. the title is self-explanatory

    Basically, I just want PHP to output stuff from my database... multiple rows with about 7 cols in a tabular format.. ala cells in Excel. As long as it resembles a neat vertical table.

    Any functions to call or tricks to do this? Perhaps write HTML code for a table.. and have PHP output SELECT inside that table.. I don't know...

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I might be missing something here, and I probably am, but would it not just be as simple as like:
    echo '<table>';

    while ($result = mysql_fetch_array($sql)) {
    echo "<tr><td>$value></td><td>$value</td></tr>";
    }

    echo '</table>';

    Hope that is kinda what you were after. I suppose you could write a class to draw the table, which could make it slightly easier if you were to do it multiple times, or a function even.

  3. #3
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a little snippet of code i take with me on all my database projects....it's a wonderful thing:



    PHP Code:

        <?



    function simpletable($results) {


    /*    Take any results from a MySQL query and throw them into a simple table
        to give a nice simple display. Comes in really handy for testing 
        results sets to see what you are actually getting back if things
        seem to be broken.
        
        To use set up your query, make it, test it and then send the results 
        to this function like this:
        
        simpletable($your_results_variable_here);
        
        A better way to use it would probably to echo it straight out:
        
        echo simpletable($your_results_variable_here);
        
        
    */



        
    $table '<table cellpadding=1 cellspacing=0 border=1><tr>'


        
    $x mysql_num_fields($results);
        
    $i 0;


        while (
    $i $x ) {
            
    $meta mysql_fetch_field($results);
            
    $table .= '<td><font face=verdana size=1>' $meta->name '</font></td>';
            
    $i++;
        }


        while ( 
    $row mysql_fetch_array($results) ) {
            
    $table .= '</tr><tr>';
            for (
    $y 0$y $x$y++) {
                
    $table .= "<td>$row[$y]</td>";
            }
        }

        
    $table .= '</tr></table>';
        
        return 
    $table
        
    }

    ?>

    Now be warned, this may not work cause i just made this out of a different set of instructions i used. I think this should work, but it might not.


    hrm....new topic in order now....
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  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)
    yeah, i farked up that last piece of code....here's the correct version:



    PHP Code:

    <?



    function simple_table($xresults) {


    /*    

        Take any results from a MySQL query and throw them into a simple table
        to give a nice simple display. Comes in really handy for testing 
        results sets to see what you are actually getting back if things
        seem to be broken.
        
        To use set up your query, make it, test it and then send the results 
        to this function like this:
        
        simpletable($your_results_variable_here);
        
        A better way to use it would probably to echo it straight out:
        
        echo ( simpletable($your_results_variable_here) );
        
        
    */



        
    $table '<table cellpadding=1 cellspacing=0 border=1><tr>'


        
    $x mysql_num_fields($xresults);
        
    $i 0;


        while (
    $i $x ) {
            
    $meta mysql_fetch_field($xresults);
            
    $table .= '<td><font face=verdana size=1>' $meta->name '</font></td>';
            
    $i++;
        }


        while ( 
    $row mysql_fetch_array($xresults) ) {
            
    $table .= '</tr><tr>';
            for (
    $y 0$y $x$y++) {
                
    $table .= "<td>$row[$y]</td>";
            }
        }

        
    $table .= '</tr></table>';
        
        return 
    $table;
        
    }

    ?>

    Enjoy!
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  5. #5
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hot dang... thanks A LOT guys!!

    will keep you informed...

  6. #6
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey.. thanks a bill milamber!!

    your simpletable() function worked flawlessly!! though i have to play around with cell padding and stuff.. but the basic thing is:

    I HAVE A FREAKIN TABLE!!!!!!!

    Thanks again!! will be keeping an eye on that post of yours for nifty functions!! (any websites that offer cool fns. like the one you gave me?)

    appreciate your help!

  7. #7
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by infinitium
    hey.. thanks a bill milamber!!

    your simpletable() function worked flawlessly!! though i have to play around with cell padding and stuff.. but the basic thing is:

    I HAVE A FREAKIN TABLE!!!!!!!

    Thanks again!! will be keeping an eye on that post of yours for nifty functions!! (any websites that offer cool fns. like the one you gave me?)

    appreciate your help!

    No problem.


    As far as i know of there aren't any sites that are just for "function lists" i know there are some functions out there, but i don't know of those places off hand.

    Sorry! Got to make your own!
    -Jeff Minard | jrm.cc - Battlefield 2 Stats


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
  •