Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created March 12, 2017 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/e69264d14f68fc603f29fc886742c656 to your computer and use it in GitHub Desktop.
Save phpfiddle/e69264d14f68fc603f29fc886742c656 to your computer and use it in GitHub Desktop.
[ Posted by Joe Bloggs ] Load an associative array of colour names and their hex values from a file, then display the names inside appropriately coloured divs.
<?php
class css_colour_displayer {
private $colours_file_name;
private $page_heading;
private $styles;
function __construct($file_url, $file_name, $page_heading, $styles) {
$this->styles = $styles;
$this->page_heading = $page_heading;
$this->colours_file_name = $file_name;
$this->load_and_write_file($file_url);
}
function load_and_write_file($url) {
$txt = file_get_contents($url);
$myfile = fopen($this->colours_file_name, "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
}
function display_heading() {
echo "<h1>" . $this->page_heading . "</h1>";
}
function display_page() {
echo $this->styles;
$this->display_heading();
$this->display_colours();
}
function display_colours() {
include_once $this->colours_file_name;
foreach($colour_names as $colour_name => $hex) {
echo "<div style='display:inline-block;border:1px solid #ccc;width:240px;padding:10px;background-color:".$hex.";'><a href='#'>" . $colour_name, " ", $hex, "</a></div>";
}
}
}
$styles = <<<STYLES
<style>
body {margin: 1em auto;width:80%;}
h1 {font-family: sans-serif;text-align:center;font-weight:400;font-size:3em;}
a {text-decoration:none;}
a:hover {text-decoration:underline}
</style>
STYLES;
$url = 'http://dts300.bitballoon.com/computer-programming/colour-names-associative-array.php';
$colour_displayer = new css_colour_displayer($url, 'colour-names.php', 'Colour names and Hex codes', $styles);
$colour_displayer->display_page();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment