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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Function to wrap data in a table

    I am sick and tired of typing table data (which number of columns are different each time) around my while loops based on MySQL queries and am looking for something like

    $columns_array = array('column1name', 'column2name', 'column3name',.......)
    wraptable($columns_array);

    Is there a pre-made function for this that you know about?

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    You can do something like the following to get and print the column names and any number of columns -
    PHP Code:
    // 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"
    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
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thank you very much, your nickname is hard to type

    I wrote the following while asking the question and it did not work as it looks like the $result does not pass through the function as I expected.


    PHP Code:

    function Wrap($array,$result) {
        echo 
    '<table id="reports" cellspacing="8" cellpadding="8">';
        echo 
    '<tr>';
        
    // table header w. column names
        
    $i 0;
        while(
    $i count($array)) {
            echo 
    '<th>' $array[$i] . '</th>';
            
    $i++;
        }
        echo 
    '</tr>';
        
    // data
        
    while($row mysql_fetch_row($result)) {
            echo 
    '<tr>';
            
    $d 0;
            while(
    $d count($array)) {
                echo 
    '<td>' $row[$d] . '</td>';
                
    $d++;
            }
            echo 
    '</tr>';
        }
        echo 
    '</table>';


  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    If I try to turn your suggestion into a function, it does not work, any ideas why?

    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";  


  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    On PHP 5 it works. I suspect that you are on PHP 4. If so, try changing the function definition to this, so that the parameter is passed by reference, rather than by value -
    Code:
    function 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.

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Yes, I use PHP4. And yes, it works with &.
    Now I need to incorporate custom field names rather than db field names.
    Thanks a lot for your help!

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    In your query, if you have column names that you want to be named something else in the results, use the AS keyword to give them an alias -
    Code:
    SELECT dbcolumn1 AS column1name, dbcolumn2 AS column2name, ...
    I believe back-ticks ` would be needed around the alias names if they contained spaces or other characters that are not normally allowed in a column identifier.

    Edit: I just found an example of an alias with a space in the name that had quotes surrounding it.

    Edit2: I just confirmed that both back-ticks and quotes work (PHP5/MySQL5).
    Last edited by CFMaBiSmAd; 02-03-2007 at 01:39 AM.
    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.

  • #8
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Great, what about international characters?

    And can I add buttons to each row such as edit, delete?


  •  

    Posting Permissions

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