SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hiding empty table columns [-Renamed- Was: Hum, what should I do]

    I can't figure the best way to accomplish this, per someone can share a few options

    I have an html table with 10 columns (example below 5 columns), first row is table headers, and there is 3 rows of data from mysql.

    Code:
    6"	12"	24"	36"	48"	60"
    11	21		21		54
    33	44		45	55	12	
    45				50
    What I would like to do is only show those columns that have data in then. In the example above I would like to hide the column labeled 24"

    Thanks

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can you post the code you are currently using to output the table? In [php] bbcode tags please
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    //using ez_sql
    include_once "ezsql/ez_sql_core.php";
    include_once "ezsql/ez_sql_mysql.php";
    $db = new ezSQL_mysql('user','password','db','localhost');

    // get measurements
    $measure = $db->get_results(
    "SELECT usb_measurement_types.measurement_type, 
        usb_measurement.*
    FROM usb_measurement_types 
    INNER JOIN usb_measurement ON usb_measurement_types.measurement_type_id = usb_measurement.measurement_type_id
    WHERE usb_measurement.blank_id = ".$id." ORDER BY measurement_type_id"
    );


    // display measurements
    if($measure){?>
    <table id="measurements" cellpadding="0" cellspacing="0">
    <!--<th>
    <td>Nose</td><td>Nose 6"</td><td>Nose 12"</td><td>Nose 24"</td><td>Nose 36"</td><td>Nose 48"</td>
    <td>Center</td>
    <td>Tail 48"</td><td>Tail 36"</td><td>Tail 24"</td><td>Tail 12"</td><td>Tail 6"</td><td>Tail</td></th>
    -->
    <tr>
    <?php
    // display measurements
    $i=1;
    foreach(
    $measure as $rs2)
    {
        if(
    $i &#37; 2){
            
    $rowclass="odd";
        }else{
            
    $rowclass="even";
        };
    echo 
    "<tr class='".$rowclass."'>";   
    echo 
    "<td class='left'>".$rs2->measurement_type.'</td>';
    echo 
    "<td>".cm2inch($rs2->nose).'"<br/>('.$rs2->nose.' cm)</td>';
    echo 
    "<td>".cm2inch($rs2->n6).'"<br/>('.$rs2->n6.' cm)</td>'
    echo 
    "<td>".cm2inch($rs2->n12).'"<br/>('.$rs2->n12.' cm)</td>'
    echo 
    "<td>".cm2inch($rs2->n24).'"<br/>('.$rs2->n24.' cm)</td>'
    echo 
    "<td>".cm2inch($rs2->n36).'"<br/>('.$rs2->n36.' cm)</td>'
    echo 
    "<td>".cm2inch($rs2->n48).'"<br/>('.$rs2->n48.' cm)</td>'
    echo 
    "<td>".cm2inch($rs2->center).'"<br/>('.$rs2->center.' cm)</td>'
    echo 
    "<td>".cm2inch($rs2->t48).'"<br/>('.$rs2->t48.' cm)</td>'
    echo 
    "<td>".cm2inch($rs2->t36).'"<br/>('.$rs2->t36.' cm)</td>';
    echo 
    "<td>".cm2inch($rs2->t24).'"<br/>('.$rs2->t24.' cm)</td>';
    echo 
    "<td>".cm2inch($rs2->t12).'"<br/>('.$rs2->t12.' cm)</td>';
    echo 
    "<td>".cm2inch($rs2->t6).'"<br/>('.$rs2->t6.' cm)</td>';
    echo 
    "<td>".cm2inch($rs2->tail).'"<br/>('.$rs2->tail.' cm)</td>';  
    echo 
    "</tr>";          
    $i++;
    }
    ?>
    </th>
    </table>
    <?php }?>
    I sure this is not much help, as I just have the TH commented out.
    Last edited by kohoutek; Mar 24, 2010 at 16:18. Reason: Added closing php bbcode tag

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, so first thing we'll need to do is add some pre-output processing - in otherwords, first parse all of the information into an array. Then traverse the array checking if each field has values.

    So, here's what I came up with.

    First have a list of possible field names and their table headers. Have an empty array which will contain those fields which are existent.

    Loop through each record. For each field which has a value, that isn't already in the current field list, add it to the field list and remove it from the fields to search. If all fields have already been found, there isn't a reason to keep going through the records so break out of the loop.

    Then loop through field names to output the table headers, and loop through each record, outputting cells for each field name.

    Like so:

    PHP Code:
    $MeasureArray = array();
    $Columns = array(
        
    'measurement_type' => 'Measurement Type',
        
    'nose' => 'Nose',
        
    'n6' => 'Nose 6"',
        
    'n12' => 'Nose 12"',
        
    'n24' => 'Nose 24"',
        
    'n36' => 'Nose 36"',
        
    'n48' => 'Nose 48"',
        
    'center' => 'Center 12"',
        
    't48' => 'Tail 48"',
        
    't36' => 'Tail 36"',
        
    't24' => 'Tail 24"',
        
    't12' => 'Tail 12"',
        
    't6' => 'Tail 6"',
        
    'tail' => 'Tail"'
    );
    $ColumnsLeft array_keys($Columns);
    $FinalColumns = array();
    foreach(
    $measure as $m){
        foreach(
    $ColumnsLeft as $Key => $Value){
            if(
    $m->$Value != ""){
                unset(
    $ColumnsLeft[$Key]);
                
    $FinalColumns[$Value] = $Columns[$Value];
            }
        }
        if(empty(
    $Columns)){
            break;
        }
    }
    echo 
    '<table>';
    echo 
    '<tr>';
        foreach(
    $FinalColumns as $Name){
            echo 
    '<th>' $Name '</th>';
        }
    echo 
    '</tr>';
    foreach(
    $measure as $m){
        echo 
    '<tr>';
            foreach(
    $FinalColumns as $Key => $Name){
                echo 
    '<td>';
                    echo 
    $m->$Key;
                echo 
    '</td>';
            }
        echo 
    '</tr>';    
    }
    echo 
    '</table>'
    Here's the full code I used to test with, as I don't have data in the database etc:
    PHP Code:
    <?php
    class Measure{
        public 
    $measurement_type 'a'$nose$n6$n12$n24$n36$n48$center$t48$t36$t24$t12$t6$tail;
        function 
    __construct($nose$n6$n12$n24$n36$n48$center$t48$t36$t24$t12$t6$tail){
            
    $this->nose $nose;
            
    $this->n6 $n6;
            
    $this->n12 $n12;
            
    $this->n24 $n24;
            
    $this->n36 $n36;
            
    $this->n48 $n48;
            
    $this->center $center;
            
    $this->t48 $t48;
            
    $this->t36 $t36;
            
    $this->t24 $t24;
            
    $this->t12 $t12;
            
    $this->t6 $t6;
            
    $this->tail $tail;
        }
    }
    $measure = array(
        new 
    Measure(12345678910''1213),
        new 
    Measure(12345678910''1213),
        new 
    Measure(12345678910''1213),
        new 
    Measure(12345678910''1213)
    );
    $MeasureArray = array();
    $Columns = array(
        
    'measurement_type' => 'Measurement Type',
        
    'nose' => 'Nose',
        
    'n6' => 'Nose 6"',
        
    'n12' => 'Nose 12"',
        
    'n24' => 'Nose 24"',
        
    'n36' => 'Nose 36"',
        
    'n48' => 'Nose 48"',
        
    'center' => 'Center 12"',
        
    't48' => 'Tail 48"',
        
    't36' => 'Tail 36"',
        
    't24' => 'Tail 24"',
        
    't12' => 'Tail 12"',
        
    't6' => 'Tail 6"',
        
    'tail' => 'Tail"'
    );
    $FinalColumns = array();
    foreach(
    $measure as $m){
        foreach(
    $Columns as $Key => $Value){
            if(
    $m->$Key != ""){
                unset(
    $Columns[$Key]);
                
    $FinalColumns[$Key] = $Value;
            }
        }
        if(empty(
    $Columns)){
            break;
        }
    }
    echo 
    '<table>';
    echo 
    '<tr>';
        foreach(
    $FinalColumns as $Name){
            echo 
    '<th>' $Name '</th>';
        }
    echo 
    '</tr>';
    foreach(
    $measure as $m){
        echo 
    '<tr>';
            foreach(
    $FinalColumns as $Key => $Name){
                echo 
    '<td>';
                    echo 
    $m->$Key;
                echo 
    '</td>';
            }
        echo 
    '</tr>';    
    }
    echo 
    '</table>';
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow Jake,

    Thanks so very much.


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
  •