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?
<?php
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?