Codeigniter Dynamic Table View

Here is a dynamic view for the PHP MVC framework CodeIgniter. This view will take an associative array and turn it into a table, with allowing for some additional setting parameters, such as a class name on the table. First we need to setup the multi-dimensional array that contains our table data. Each key of our $data array will be a column. So using the example below we will have a three column table of `Things I like`, `Places I Go`, and `Widgets`. Also we will add a class of `my-great-table` to the table itself. For CSS and JS purposes.
$table_data = array(
      "class" => "my-great-table",
      "data" => array(
        "Things I Like" => array(
          "Thing1", 
          "Thing2",
        ),
        "Places I Go" => array(
          "Place 1",
          "Place 2", 
          "Place 3",
        ),
        "Widgets" => array(
          "Widget 1",
          NULL,
          "Widget 2",
        ),
      ),
    );
Notice that you can have different length columns, but if you need data to stay aligned because you're missing data in the middle of a column then you need to make sure something is there in the array (either a blank string or NULL, etc). Additionally, each column gets a secondary generic class of col-N, so to style the second column of our above table we would use .my-great-table .col-1 { font-weight: bold }
  /*  Table view dynamically creates a html table based on the data passed
   *  It takes the @data keys as column titles, displays the titles, then uses them
   *  to create a 2 factor lookup while looping thru the array, allowing a table to be created
   *  @data         array       Keys are the column names, values are for each table cell, by column
   *  @class        string      Optional. If you want a class name on the table
   */
  $column_names = array(); // to store the column names from the data keys

  if ( !is_array($data) )
  {
    echo "Error: You must specify at least one column";
    return;
  }

  foreach ($data as $column_name => $array)
  {
    $column_names[] = $column_name;
  }

  echo "";
  echo "";

  foreach ($column_names as $column_name)
  {
    echo "";
  }

  echo "";

  // find the longest column
  $longest_column_count = 0;
  foreach ($data as $column_data)
  {
    if (count($column_data) > $longest_column_count)
    {
      $longest_column_count = count($column_data);
    }
  }

  // display the data
  // loop thru each row of data
  reset($data);
  for ($i = 0; $i < $longest_column_count; $i++ )
  {
    echo "";

    // loop thru each column of each row
    reset($column_names);
    for ($g = 0; $g < count($column_names); $g++ )
    {
      echo "";

      // advance to the next row
      next ($data[ current($column_names) ]);
      // advance to the next column
      next ($column_names);
    }

    echo "";
  }

  echo "
". $column_name . "
". current($data[ current($column_names) ]) ."
";
Output:
Things I LikePlaces I GoWidgets
Thing1Place 1Widget 1
Thing2Place 2
Place 3Widget 2
Small dog web development studio

View All Our Posts

Web application developers in western Washington, creating user centric web solutions for businesses of all shapes and sizes.

Our projects include ecommerce, event organization, user driven web applications and more.


Small Dog Studios, LLC. Small Dog Studios News