Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 11:42:27 PM

Login with username, password and session length
Search:     Advanced search
Interested in joining the WebsiteBaker team?
For more Information read here or on our new website.
155556 Posts in 21715 Topics by 7737 Members
Latest Member: gx-world
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Display table snippet ... droplet to follow  (Read 574 times)
mdemaree99

Offline Offline

Posts: 68


« on: December 01, 2011, 08:18:12 PM »

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>');


« Last Edit: December 01, 2011, 11:38:39 PM by mdemaree99 » Logged
mdemaree99

Offline Offline

Posts: 68


« Reply #1 on: December 01, 2011, 11:23:08 PM »

Droplet Below
Let me know what you think.

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

Droplet Name
Code:
DisplayTable

Droplet Example
Code:
[[DisplayTable?table=wb1_users]]

Description
Code:
Display contents of a table [[DisplayTable?table=wb1_users]]

Droplet Code
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;

« Last Edit: December 01, 2011, 11:38:52 PM by mdemaree99 » Logged
mdemaree99

Offline Offline

Posts: 68


« Reply #2 on: December 13, 2011, 06:58:59 PM »

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?
Logged
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!