Category: "PHP"

Simple Hit Counter Example

Hit counters are valuable because they allow site visitors to see how many people visited or used a server-based tool.

This is a very simple implementation of a counter. You can see it run at the link above.

The counter code is in the back-end/server-side logic.

PHP5 version

$iCount=file_get_contents('count.txt');
file_put_contents('count.txt',$iCount+1);
echo '<p><span style="font-weight:bolder;border:1px solid #000;padding:2px;">'.$iCount.' gradients created.</span></p>';

PHP4 version

$iCount=file_get_contents('tri.count.txt');
$fp=fopen('tri.count.txt','w+');
fwrite($fp,$iCount+1);
fclose($fp);
echo '<p>'.$iCount.' downloads</p>';

Gradient Generator Notes

Value

The value of this utility is in its simplicity. Image, photo, and graphical editors are very action-intensive. This tool allows a selection of a single color, the width/height, and then it creates all four gradients. This is a very fast way to create custom gradients, and it is easy to use.

UI

  • Page should not require scrolling to operate, read instructions, and access gradients and text.
  • Interface should allow both mouse and keyboard control. Gradients can be generated by mouse only, or keyboard.
  • Interface should allow entry of hex color code and width/height directly.
  • It is assumed the gradient will have enough color that a white title will be visible.

LAMP Stack

  • CSS/divs are used to layout the page.
  • ColorPicker is a dojox widget. Additional widgets are the width/height input and the slider. Client-side validation is run off the widgets.
  • Two buttons are offered, one that creates the gradients and one that resets the inputs to the default values.
  • When the create button is clicked, the form is validated and submitted to the server.
  • PHP5 is used for the back-end script because it supports json_encode.
  • HTML is returned from the back-end script and placed in the dojo ContentPane.
  • The background image for the page is set to one of the gradients. This is a good preview method and it makes the page more interesting.
  • ImageMagick is used to create the gradients. It creates one gradient and rotates it to create the remaining three.
  • Directory index should not be accessible. This is accomplished by another file.
  • File system - image files must be writeable by Apache.

Optional Parameters - Extending Existing Functions

Both PHP and javascript allow optional parameters and variable arguments.

PHP

javascript

  • The arguments sent to a function in javascript can be accessed as function_name.arguments. To find out how many arguments were passed, you can use function_name.arguments.length. http://www.w3schools.com/jS/js_functions.asp
  • Implementing default arguments with javascript can be done by using the arguments array.

Multi-Dimensional Array Sorts for PHP

To sort a multi-dimensional array in PHP, you can use usort, like so:

usort($aArray,"cmp_func");

function cmp_func($a,$b)
{
  return strcasecmp($a['x'],$b['x']);
}

dojox Grid Select Indicator Demo

Demonstration of a selection indicator for a dojox grid.

Notes:

  • Grey checkmark serves as select all/deselect all, if no rows are selected, clicking on the checkmark will select them all, if any rows are selected, all will be deselected.
  • Row selector uses a green checkmark to indicate selection. Additional styling can be applied.
  • Add and remove rows work, but no save is performed.
  • Icons from Crystal Project (http://everaldo.com/crystal)

* Demonstration is for the design, not operation of grid.