Display table snippet ... droplet to follow

mdemaree99

More info...
Droplet works with DynamicTable however, not able to hide columns as needed.

Does anybody think this is useful or even tried it? 
How do you display tables?

mdemaree99

#2
Droplet Below
Let me know what you think.

Also.. Just checked and does not work with dynamic_table.

Droplet Name

DisplayTable


Droplet Example
[[DisplayTable?table=wb1_users]]

Description

Display contents of a table [[DisplayTable?table=wb1_users]]


Droplet Code

$fields_array=array();
          $num_fields=0;
          $num_row=1;
         
// Define Table Used Here
// Can be used for multiple tables, but must define here or hard code in SQL search
//           $table="wb1_users";

// SQL select for table
// Current example displays all headings for an individual table
          $sql = "SELECT * FROM `$table`";        

// find position of "FROM" in query
          $fpos=strpos($sql, 'from');

// get string starting from the first word after "FROM"
          $strfrom=substr($sql, $fpos+5, 50);

// Find position of the first space after the first word in the string
          $Opos=strpos($strfrom,' ');

// Get result from query
           $result=mysql_query($sql);
           $num_row=mysql_numrows($result);
         
// get string starting from the first word after "FROM"
          $strfrom=substr($sql, $fpos+5, 50);
         
// Find position of the first space after the first word in the string
          $Opos=strpos($strfrom,' ');

// Get result from query
           $result=mysql_query($sql);
           $num_row=mysql_numrows($result);

           if($num_row >0)
           {
//Get number of fields in query
                   $num_fields=mysql_num_fields($result);      
// get column metadata
           $i = 0;

        $output .= '<br>'."\n";
        $output .= '<br><table id="myDynamicTable"><thead><tr>'."\n";

while ($i < $num_fields)
      {

//Get fields (columns) names
           $meta = mysql_fetch_field($result);
           $fields_array[]=$meta->name;

// Can Display Headers in UPPER, LOWER, OR PROPER CASE
//                         example db table heading is:  MyTableHeading
// code                                                  Output
// strtoupper for upper case                             MYTABLEHEADING
// strtolower for lower                                  mytableheading
// ucfirst for for first letter upper                    Mytableheading
// if no code is used headign will be as in table        MyTableHeading
           
           
$output .= '<th SCOPE="col"><b>'.$fields_array[$i].'</b></th>'."\n";            
                     $i=$i+1;
        }
$output .= '</tr></thead>'."\n";    
$output  .= '  <tbody> '."\n";                    

//Get values for each row and column
        while($row=mysql_fetch_row($result))
             {
$output .= '<tr> '."\n";

                       for($i=0; $i<$num_fields; $i++)
                       {
//Display values for each row and column

$output .= '   <td>'.$row[$i].' </td> '."\n";
}

$output .= '</tr> '."\n";
}                                      
}
$output .= '</tbody></table>     '."\n";

return $output;



mdemaree99

#1
Necessity is the mother of invention so put together some code to display an entire table.
Wanted to give everybody a view before I converted to a droplet

Let me know what you think.

Mine works except found an issues with glassberry template CSS
Tested with other templates and works great.  Llet me know if you have seen this before.
in firefox (v3.6) table border doesn't go around outside of table
in ie (v8).. border is around outside, but not on the inside
if run in ie compatibility mode works fine

Next step after droplet would be to see if dynamictable would work well with it.
Quote
          $fields_array=array();
          $num_fields=0;
          $num_row=0;

// Define Table Used Here
// Can be used for multiple tables, but must define here or hard code in SQL search
          $table="wb1_users";

// SQL select for table
// Current example displays all headings for an individual table
          $sql = "SELECT * FROM `$table`";        

// find position of "FROM" in query
          $fpos=strpos($sql, 'from');

// get string starting from the first word after "FROM"
          $strfrom=substr($sql, $fpos+5, 50);

// Find position of the first space after the first word in the string
          $Opos=strpos($strfrom,' ');

// Get result from query
           $result=mysql_query($sql);
           $num_row=mysql_numrows($result);
         
           if($num_row >0)
           {
//Get number of fields in query
                   $num_fields=mysql_num_fields($result);      
// get column metadata
           $i = 0;

           echo ('<br>');
           echo('<br><table border="1" color="black"  align="center" ><thead><tr>');

            while ($i < $num_fields)
            {
//Get fields (columns) names
           $meta = mysql_fetch_field($result);
           $fields_array[]=$meta->name;

// Can Display Headers in UPPER, LOWER, OR PROPER CASE
//                         example db table heading is:  MyTableHeading
// code                                                  Output
// strtoupper for upper case                             MYTABLEHEADING
// strtolower for lower                                  mytableheading
// ucfirst for for first letter upper                    Mytableheading
// if no code is used headign will be as in table        MyTableHeading

// Example Codes
// upper case example     print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>');
// no ftn example         print('<th><b>'.$fields_array[$i].'</b></th>');


         
           echo ('<th SCOPE="col"><b>'.$fields_array[$i].'</b></th>');

                   $i=$i+1;
                   }

           echo ('</tr></thead>');
               
           echo ('<tbody>');
                  //Get values for each row and column
               while($row=mysql_fetch_row($result))
               {
           echo ('<tr>');

                       for($i=0; $i<$num_fields; $i++)
                       {
//Display values for each row and column
           echo ('<td>'.$row[$i].'</td>');

                       }

           echo ('</tr>');
               }
               }
          echo ('</tbody>');
          echo ('</table>');
             echo ('<br>');