Support » Plugins » Hacks » Building a table

  • I’m writing a small plugin to manage a (US) football league. Currently I’m working on outputting the results. I’m wondering if there is a cleaner way to build the table than what I’m currently using?

    class wpcomm_league_class {
        function display_league( $args ) {
            // Globals
            global $wpdb;
            // Get any arguments, discard any that are not valid
            extract( shortcode_atts( array(
    		'id' => '0' // set a default value of 0
    	), $args ) );
            $league = $wpdb->get_var( "SELECT description FROM " . $wpdb->prefix . "wpcomm_league WHERE league_id = " . $id );
                $display = "<h2>" . $league . "</h2>";
            $divisions = $wpdb->get_results( "SELECT division_id, description FROM ". $wpdb->prefix. "wpcomm_division WHERE league_id = " .$id );
                foreach ( $divisions as $division ) {
                    $display .= "<table>";
                    $display .= "<tr>";
                    $display .= "<th>" . $division->description . "</th>";
                    $display .= "</tr>";
                    $display .= "<tr>";
                    $display .= "<td>Team</td><td>Won</td><td>Lost</td><td>Tied</td><td>%</td><td>Next Game</td>";
                    $display .= "</tr>";
                    $teams  = $wpdb->get_results( "SELECT team_name FROM ". $wpdb->prefix. "wpcomm_team WHERE division_id = " .$division->division_id );
                    foreach ( $teams as $team ) {
                        $display .= "<tr>";
                        $display .= "<td>" . $team->team_name . "</td>";
                        $display .= "</tr>";
                    $display .= "</table>";
            return $display;
    // Set shortcodes
    add_shortcode( 'wpcomm_league', array( 'wpcomm_league_class', 'display_league' ));

    As you can (hopefully) see I want to use a shortcode to allow a league to be displayed on any page/post desired. So as of now I’m appending each of the pieces of the table to the $display variable which is returned at the end of the function and gets displayed. It’s working fine so far in regards to getting the results I want and having them displayed. It just seems like an ugly/unclean way to do it. Thoughts on improving it?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Looks perfectly fine to me. That’s how I’ve done my other plugins. One thing I do though is line breaks:

    $display .= "<table>"."\n";
    $display .= "<tr>"."\n";

    Just so when viewing the source it’s clean. And, seeing as this could result in a very big table, looking at one long line of HTML is ugly.

    Great point about the newline, that was in the back of my mind to fix.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Building a table’ is closed to new replies.