Code Snippets

Slide Toggle Checkbox – Pure CSS


Pure CSS toggle switch example – replaces checkboxes.

Code Snippets PHP

Loop Through PHP Request Values – GET and POST

Loop through PHP Request Values

In order to Loop through PHP Request Values – GET and POST without knowing what they all might be, you can simply use the following code:

foreach ($_REQUEST as $key => $value)  
$value = mysql_real_escape_string( $value );  
$value = addslashes($value);  
$value = strip_tags($value);  
if($value != ""){$requestnumber ++;}  
echo $key. ' - '.$value.'
'; }

This is useful when you have an undetermined/unknown set of POST or GET values that you would like to loop through.

Code Snippets

Mobile Redirection With Full Site Option

It’s handy for people who don’t know that you have a mobile version of your site, or know what the URL is (is it Is it Is it something else?) On the other hand people often want to view the full version of a site on their mobile device. You can achieve this quite simply.

This code goes at the top of your header include file, or at least at the top of each page you wish to redirect. It must go before any page rendering happens, or you will get the dreaded “headers have already been sent” error.

In the header of the mobile site add some code to reset the override cookie to allow the mobile site to be seen. The assumption is that you view the mobile site until you tell the application otherwise:

All that is left to do is to give the manual option for switching between the two versions, in the form of links.

From mobile to full (includes variable to set view mobile to false):

<a href="">View Full Website</a>

And the option to return to the mobile version, where the cookie is automatically changed just by visiting:

<a href="">View Mobile Website</a>

Visual Studio

Visual Studio Web Developer Permission Error Fix

Occasionally when working with Visual Studio on a local site, I have received an permission error when trying to run the application. I usually dig and dig before finding the solution, so I am going to save it here.

Solution: Turn off NTLM authentication in Property Window.

Code Snippets

Encode Mysql Results to JSON

The title says it all:

mysql_set_charset('utf8', $database_connection); // charset
$sqlStr = "SELECT * FROM yourtable;"; //provide the query
$result = mysql_query ($sqlStr, $database_connection) or die ($sqlStr); // get the results
$rows = array(); // create the array
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) { // populate array with results
$rows[] = $row;
echo json_encode($rows); // encode all in json
mysql_close($database_connection); // close the database connection