Skip to content

Instantly share code, notes, and snippets.

@stovak
Last active August 29, 2015 13:57
Show Gist options
  • Save stovak/9348910 to your computer and use it in GitHub Desktop.
Save stovak/9348910 to your computer and use it in GitHub Desktop.
<?php
/**
* file "orange.php"
* Ordinarily I wouldn't put everything in one file,
* but for this case, it will make the exercise much
* simpler and easier to run locally
*
* @author stovak
*/
$pdo_connect = "mysql:host=127.0.0.1;port=3306;dbname=apple";
$pdo_username = "root";
$pdo_password = "";
setlocale(LC_MONETARY, 'en_US');
?>
<!DOCTYPE html>
<html>
<head>
<title>Table Exercise</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/readable/bootstrap.min.css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" media="screen" charset="utf-8" />
<style><!--
.row { width: 90%; margin: 20px auto; }
.row table td { text-align: center; }
--></style>
</head>
<body>
<div class="row">
<div class="col-xs-12 col-md-12">
<?php echo main(); ?>
</div>
</div>
</body>
</html>
<?php
/**
* MAIN
*
* @return string
* @author stovak
*/
function main() {
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
return (string) new Alert("This script requires PHP 5.3 or newer.");
}
try {
$toReturn = "";
$db = new PDO($GLOBALS['pdo_connect'], $GLOBALS['pdo_username'], $GLOBALS['pdo_password']);
$results = $db->query("SELECT pos1, pos2, pos3, pos4, pos5, pos5, pos7, pos8, hardware1, hardware2, hardware3, hardware4, hardware5, extra_features FROM orange_RatePlans ORDER BY pos1 asc, pos2 asc, pos3 asc, pos4 asc");
if ($results instanceOf PDOStatement) {
$cols = array();
$last = null;
while ($result = $results->fetch(PDO::FETCH_ASSOC)) {
//first grouping of fields, put them in a separate table
if ($result['pos1'] == $last || ($last === null)) {
$cols[] = $result;
} else {
//xdebug_break();
$table = new Table();
$table->fromColumns($cols);
//xdebug_break();
$toReturn .= $table->__toString();
$cols = array($result);
//xdebug_break();
}
$last = $result['pos1'];
}
return $toReturn;
} else {
return (string) new Alert("No Results from Database");
}
} catch(Exception $e) {
return (string) new Alert($e->getMessage());
}
}
/**
* ALERT object
*
* @package default
* @author stovak
*/
class Alert {
/**
* text of the returned message
*
* @author stovak
*/
public $message;
/**
* class to put on bootstrap returned html
*
* @author stovak
*/
public $type;
/**
* constructor
*
* @param string $message
* @param string $type
* @author stovak
*/
function __construct($message = "", $type = "danger") {
$this->message = $message;
$this->type = $type;
}
/**
* __toString response
*
* @return void
* @author stovak
*/
function __toString() {
return "<div class='alert alert-{$this->type}'><h1>{$this->message}</h1></div>";
}
}
/**
* CELL object
*
* @package default
* @author stovak
*/
class Cell {
/**
* colspan
*
* @var int
*/
public $span = 1;
/**
* cell value
*
* @var string
*/
public $value;
/**
* constructor
*
* @param string $value
* @author stovak
*/
function __construct($value = "") {
if ($value === null) {
$value = "";
}
//if field is numeric, make the assumption it is a price.
if (is_numeric($value)) {
$value = money_format('%n', $value);
}
$this->value = $value;
}
/**
* __toString method
*
* @return void
* @author stovak
*/
function __toString() {
if ($this->span >= 2) {
$colspan = " colspan='{$this->span}'";
} else {
$colspan = "";
}
return " <td{$colspan}>{$this->value}</td>\n";
}
}
/**
* ROW object
*
* @package default
* @author stovak
*/
class Row {
/**
* The row's cells
*
* @var array
*/
private $cells = array();
/**
* previous cell's value
*
* @var string
*/
private $previousValue;
/**
* constructor
*
* @param string $rowData
* @param string $rowName
* @author stovak
*/
function __construct($rowData, $rowName) {
$previous = "";
foreach ($rowData as $key => $value) {
if (count($this->cells) >= 1
&& $this->previousValue == $value
&& substr($rowName, 0, 3) == "pos") {
end($this->cells)->span++;
} elseif (trim($value) != "") {
$this->cells[] = new Cell($value);
$this->previousValue = $value;
}
}
}
/**
* __toString method
*
* @return void
* @author stovak
*/
function __toString() {
$toReturn = "";
if (count($this->cells) >= 1) {
$toReturn = " <tr>\n";
foreach ($this->cells as $idx => $cell) {
if ($cell instanceOf Cell) {
$toReturn .= $cell->__toString();
}
}
$toReturn .= " </tr>\n";
}
return $toReturn;
}
}
/**
* TABLE object
*
* @package default
* @author stovak
*/
class Table {
/**
* a table's rows
*
* @var string
*/
private $rows = array();
/**
* constructor
*
* @author stovak
*/
function __construct() {
}
/**
* __toString method
*
* @return void
* @author stovak
*/
function __toString(){
$toReturn = "<table class='table table-striped table-bordered'>\n";
foreach($this->rows as $row) {
$toReturn .= $row->__toString();
}
$toReturn .= "</table>\n";
return $toReturn;
}
/**
* Add rows method
*
* @param array $rowData
* @return void
* @author stovak
*/
function addRows($rowData) {
foreach($rowData as $key => $value) {
$this->rows[$key] = new Row($value, $key);
}
}
/**
* Swap rows for columns before adding
*
* @param string $arr
* @return void
* @author stovak
*/
function fromColumns($arr) {
$out = array();
foreach ($arr as $key => $subarr) {
foreach ($subarr as $subkey => $subvalue) {
$out[$subkey][$key] = $subvalue;
}
}
$this->addRows($out);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment