Web Programming Languages

PHP Source Code: table.php

<?php
//----------------------------------------------------------
//   The Web Language Project
//   Mark Brautigam
//   May-June 2015
//   http://www.mixed-up.com/markb/
//----------------------------------------------------------

include "common.php";
headers1();
headers2();
echo sidebar("php", "table", "php");
?>

  <div id="content">
    <h2>PHP: Dynamic Table Data</h2>

<?php
  //-------------------------------------------------------------
  // Abbr|State|Population|Rank|Capital

  $state_data = array (
    "NY|New York|19 m|3|Albany",
    "NJ|New Jersey|8.7 m|10|Trenton",
    "VT|Vermont|620 k|49|Montpelier",
    "IN|Indiana|6.2 m|15|Indianapolis",
    "OH|Ohio|11 m|7|Columbus",
    "TX|Texas|22 m|2|Austin",
    "NM|New Mexico|1.9 m|36|Santa Fe",
    "MA|Massachusetts|6.3 m|13|Boston",
    "CO|Colorado|4.6 m|22|Denver",
    "TN|Tennessee|5.9 m|16|Nashville",
    "ND|North Dakota|630 k|48|Bismarck",
    "WY|Wyoming|500 k|50|Cheyenne",
    "OK|Oklahoma|3.5 m|29|Oklahoma City",
    "WV|West Virginia|1.8 m|37|Charleston",
    "NV|Nevada|2.4 m|35|Carson City",
    "PA|Pennsylvania|12 m|6|Harrisburg",
    "CA|California|36 m|1|Sacramento",
    "WA|Washington|6.2 m|14|Olympia",
    "GA|Georgia|9.0 m|9|Atlanta",
    "MO|Missouri|5.8 m|18|Jefferson City",
    "NH|New Hampshire|1.3 m|41|Concord",
    "HI|Hawaii|1.2 m|42|Honolulu",
    "MD|Maryland|5.6 m|19|Annapolis",
    "DE|Delaware|840 k|45|Dover",
    "AK|Alaska|660 k|47|Juneau",
    "MN|Minnesota|5.1 m|21|Saint Paul",
    "NC|North Carolina|8.6 m|11|Raleigh",
    "KY|Kentucky|4.1 m|26|Frankfort",
    "MT|Montana|930 k|44|Helena",
    "LA|Louisiana|4.5 m|24|Baton Rouge",
    "NE|Nebraska|1.7 m|38|Lincoln",
    "WI|Wisconsin|5.5 m|20|Madison",
    "AL|Alabama|4.5 m|23|Montgomery",
    "IA|Iowa|2.9 m|30|Des Moines",
    "VA|Virginia|7.5 m|12|Richmond",
    "ME|Maine|1.3 m|40|Augusta",
    "ID|Idaho|1.4 m|39|Boise",
    "AR|Arkansas|2.7 m|32|Little Rock",
    "UT|Utah|2.4 m|34|Salt Lake City",
    "MS|Mississippi|2.9 m|31|Jackson",
    "RI|Rhode Island|1.0 m|43|Providence",
    "FL|Florida|17 m|4|Tallahassee",
    "OR|Oregon|3.6 m|27|Salem",
    "SD|South Dakota|770 k|46|Pierre",
    "MI|Michigan|10 m|8|Lansing",
    "KS|Kansas|2.7 m|33|Topeka",
    "CT|Connecticut|3.5 m|28|Hartford",
    "SC|South Carolina|4.2 m|25|Columbia",
    "IL|Illinois|12 m|5|Springfield",
    "AZ|Arizona|5.9 m|17|Phoenix" );
  $state_data_2d = array ();

  //-------------------------------------------------------------
  function init_2d ()
  //-------------------------------------------------------------
  {
    global $state_data;     // an array of strings
    global $state_data_2d;  // a 2D array of arrays
    $state_data_2d = array ();    // empty it
    for ($i=0, $n=count($state_data); $i<$n; $i++) {
      $data = explode ("|", $state_data[$i]);
      array_push ($state_data_2d, $data);
    }
    // print_r ($state_data_2d);
  }

  //-------------------------------------------------------------
  function draw_table_menu ()
  //-------------------------------------------------------------
  {
    echo "<p class='choose'>Choose a table:\n";
    echo "  <a href='table.php'>Multiplication</a> • \n";
    echo "  <a href='table.php?q=ascii'>ASCII</a> • \n";
    echo "  <a href='table.php?q=geo'>US States</a>\n";
    echo "</p>\n";
  }

  //-------------------------------------------------------------
  function draw_ascii () 
  //-------------------------------------------------------------
  {
    echo "<h3>ASCII Table</h3>\n";
    draw_table_menu();
    echo "<table class='mult ascii'>\n";
    echo "<tr><th></th>";
    for ($i=0; $i<16; $i++)
      printf ("<th>%X</th>", $i);
    echo "</tr>\n";

    for ($r=2; $r<8; $r++) {
      echo "<tr><th>$r</th>";
      for ($c=0; $c<16; $c++) {
        $ascii = chr (16*$r+$c);
        echo "<td>$ascii</td>";
      }
      echo "</tr>\n";
    }
    echo "</table>\n";
  }

  //-------------------------------------------------------------
  function draw_mult ()
  //-------------------------------------------------------------
  {
    echo "<h3>Multiplcation Table</h3>\n";
    draw_table_menu();
    echo "<table class='mult'>\n";
    echo "<tr><th></th>";
    for ($i=1; $i<=12; $i++)
      echo "<th>$i</th>";
    echo "</tr>\n";
    for ($r=1; $r<=12; $r++) {
      echo "<tr><th>$r</th>";
      for ($c=1; $c<=12; $c++) {
        $p = $r*$c;
        echo "<td>$p</td>";
      }
      echo "</tr>\n";
    }
    echo "</table>\n";
  }

  //-------------------------------------------------------------
  function col1sort ($a, $b)
  //-------------------------------------------------------------
  {
    return strcmp ($a[0], $b[0]);
  }

  //-------------------------------------------------------------
  function col2sort ($a, $b)
  //-------------------------------------------------------------
  {
    return strcmp ($a[1], $b[1]);
  }

  //-------------------------------------------------------------
  function col5sort ($a, $b)
  //-------------------------------------------------------------
  {
    // echo ">> col5sort ";
    return strcmp ($a[4], $b[4]);
  }

  //-------------------------------------------------------------
  function popsort ($a, $b)
  //-------------------------------------------------------------
  {
    if (strchr($a[2], 'm') > -1 && strchr($b[2], 'k') > -1) // a bigger
      return 1;
    if (strchr($a[2], 'k') > -1 && strchr($b[2], 'm') > -1) // a bigger
      return -1;
    return floatval($b[3]) - floatval($a[3]);
  }

  //-------------------------------------------------------------
  function ranksort ($a, $b)
  //-------------------------------------------------------------
  {
    return $a[3] - $b[3];
  }

  //-------------------------------------------------------------
  function draw_geo ($sort)
  //-------------------------------------------------------------
  {
    init_2d();
    global $state_data_2d;

    if ($sort == 'state') 
      usort ($state_data_2d, "col2sort");
    else if ($sort == 'pop') 
      usort ($state_data_2d, "popsort");
    else if ($sort == 'rank') 
      usort ($state_data_2d, "ranksort");
    else if ($sort == 'cap') 
      usort ($state_data_2d, "col5sort");
    else 
      usort ($state_data_2d, "col1sort");

    echo "<h3>US States, Capitals, and Population</h3>\n";
    draw_table_menu();
    echo "<p class='choose'>Choose a sort:\n";
    echo "  <a href='table.php?q=geo'>Abbreviation</a> • \n";
    echo "  <a href='table.php?q=geo&sort=state'>State</a> • \n";
    echo "  <a href='table.php?q=geo&sort=pop'>Population</a> • \n";
    echo "  <a href='table.php?q=geo&sort=rank'>Rank</a> • \n";
    echo "  <a href='table.php?q=geo&sort=cap'>Capital</a>\n";
    echo "</p>\n";
    echo "<table class='results geo'>\n";
    echo "  <tr><th>Abbr</th><th>State</th><th>Population</th><th>Rank</th><th>Capital</th></tr>\n";

    for ($i=0, $n=count($state_data_2d); $i<$n; $i++) {
      echo "<tr>";
      for ($j=0, $d=count($state_data_2d[$i]); $j<$d; $j++) {
        echo "<td>{$state_data_2d[$i][$j]}</td>";
      }
      echo "</tr>\n";
    }

    echo "</table>\n";
  }

  //-------------------------------------------------------------
  //  The main PHP code is here. 
  //-------------------------------------------------------------
  if (isset ($_GET['q']) && $_GET['q'] == 'ascii')
    draw_ascii();
  else if (isset ($_GET['q']) && $_GET['q'] == 'geo')
    draw_geo(isset ($_GET['sort']) ? $_GET['sort'] : "");
  else 
    draw_mult();

?>
    <p><a href='source.php?f=6'>Show PHP source code »</a>
  </div>

<?php 
footers(); 
?>