Top 10 PHP Tips, Functions and Techniques You Need to Know

This is a rundown of my top 10 PHP functions and techniques that I use on a daily basis (pretty-much) and thought I’d share them with you. Hopefully there’s some gems in there that you have never heard of (or used), that will change the way you code. Read More…

Posted in PHP at October 19th, 2010. 10 Comments.

AJAX Form Tutorial

This tutorial will show you how to create an AJAX form with PHP and jQuery. The AJAX form doesn’t look any different to an ordinary HTML form, with the exception of making sure that when we submit the form, it calls a javascript function rather than pushes the user to a page (causing a new page reload). I’ll also demonstrate how we can use ‘loading’ animation for the AJAX form, whilst we wait for the server to respond. Read More…

Posted in AJAX, JavaScript, jQuery, PHP at October 12th, 2010. 2 Comments.

PHP Login Script Tutorial

This PHP login script / tutorial will show you how you can have users register on your site, and log in to access secure areas. I have seen a few tutorials around the web which show how this can be done, but they all seem to lack in security. This user membership tutorial will show a better way of having users authenticated once logged in by using their session ID. Read More…

Posted in MySQL, PHP at October 6th, 2010. 27 Comments.

How to Convert PHP Multidimensional Array to Javascript Object (using jQuery)

I was working on a project today, and I needed to convert a PHP based multidimensional array into a Javascript array, so that I could use it for a dynamic select dropdown (depending on the top level category chosen, the next level select dropdown would be populated with the child elements of the top level category options – if you follow me!).

Anyway, I soon realised that converting the PHP array into a Javascript array was a bad idea, as converting it to a Javascript object would be much much better.
Read More…

Posted in JavaScript, jQuery, PHP at September 29th, 2010. 8 Comments.

PHP Class Tutorial – Part 3 – What Are Class Constructors?

So just what are class constructors, and how can they be used within PHP classes / OOP programming? When a PHP class is first called, the class will automatically run the class constructor function, which can help automatically configure the class. This can be useful if you need to preset some instance variables, sessions or cookies – prior to using the class methods.
Read More…

Posted in PHP at September 28th, 2010. 22 Comments.

PHP Class Tutorial – Part 2 – What is $this->

Please make sure you’ve followed my first PHP Class Tutorial before starting this one, as it follows on using the previous example. This tutorial will explain what $this-> is all about, and how to further your PHP class knowledge!

Read More…

Posted in OOP, PHP at August 18th, 2010. 17 Comments.

Simple PHP MySQL Class

That’s right! I have a simple MySQL class file that you can use in your PHP projects. I’ve been using it for years, and it’s never let me down!

You can grab it on my Github: http://github.com/a1phanumeric/PHP-MySQL-Class.

Read More…

Posted in Featured, MySQL, PHP at August 17th, 2010. 7 Comments.

How to make URL Safe strings for mod_rewrite using PHP

It’s relatively easy to make URL safe strings for use by mod_rewrite. Let’s use the example that you have a form that adds a new blog post to your site. When the user submits this form, you want to generate a URL safe string (based on the title of the blog post) for mod_rewrite to use. This little snippet will show you how this can be achieved in one line of code in PHP.
Read More…

Posted in Mod_Rewrite, PHP, Tools at July 11th, 2010. 3 Comments.

PHP Date Picker

This is a simple to use script that can be called at anytime to insert a date picker into your form.

Example


Read More…

Posted in PHP at January 16th, 2009. 6 Comments.

Get Random Row with MySQL

UPDATE: Please see my newer atricle on how to retrieve a random row, faster, without RAND().

This post assumes you know how to create and use a connection to a MySQL database in PHP and have a table named ‘quotes’ as shown below. In this post, I will aim to teach you how to use PHP to pull random quotes from a MYSQL table of quotes. This can be easily extended to pull a random banner as will be explained at the end of the post.

Let’s firstly assume we have a MySQL table similar to the following:

+----+------------------------------------------------+
| id | quote                                          |
+----+------------------------------------------------+
| 1  | I know Karate... and many other Chinese words! |
+----+------------------------------------------------+
| 2  | w00t this is geeky                             |
+----+------------------------------------------------+
| 3  | You're CLINICALLY MENTAL!                      |
+----+------------------------------------------------+

Now, in your PHP code, we need to:

  1. Build a suitable MySQL query to obtain a random result from the ‘quotes’ table.
  2. Store the result of the query to be used in the HTML somewhere.
  3. Output the result in the HTML somewhere.

So, for step one we’d use something like the following:

$sSQLQuery = "SELECT quote FROM quotes ORDER BY RAND() LIMIT 1";
$aResult = mysql_query($sSQLQuery);
$aRow = mysql_fetch_array($aResult, MYSQL_ASSOC);
$sQuoteOfTheDay = $aRow['quote'];

There, the variable ‘$sQuoteOfTheDay’ now has the value of our randomly pulled quote. Let’s just analyse each line of the code above to see what it does.

$sSQLQuery = "SELECT quote FROM quotes ORDER BY RAND() LIMIT 1";

This line stores the MySQL query we’re going to use against the database. It says, in laymans terms, “Select just one random value of the quote field from the table named quotes”. All this line does though is store the query into the variable ‘$sSQLQuery’.

$aResult = mysql_query($sSQLQuery);

This line runs the MySQL query, storing the result of running the query in the variable ‘$aResult’. It’s important that we store the result of the mysql_query in a variable, as the result of running a successful MySQL query using PHP’s function ‘mysql_query’ doesn’t return a nicely formatted array that we can necessarily use.

$aRow = mysql_fetch_assoc($aResult);

This is probably the hardest line for me to explain. Firstly, many of you may have seen a similar line like this used in a ‘while’ loop. However, our MySQL query used the ‘LIMIT 1′ string, so we know we’re only going to get ONE result, hence no need for a loop. The function ‘mysql_fetch_assoc’ takes one parameter: the result of the successful ‘mysql_query’ which as we know is ‘$aResult’. My biggest tip here is to use the ‘assoc’ method wherever possible, as it creates the array in such a way that we can reference each element by the column name, not a number. This is particularly useful if you ever update the MySQL table to have more columns.

Anyway, this line basically says ‘Fill the variable ‘$aRow’ with the CURRENT row of the returned query’. We know that the CURRENT row of the returned query is the ONLY row, hence (again) the lack of a loop. As the result of our query would return something like:

+-----------------------------------------------+
| quote                                         |
+-----------------------------------------------+
| You're CLINICALLY MENTAL!                     |
+-----------------------------------------------+

Our variable (or array) would literally look something like:

Array ( "quote" = "You're CLINICALLY MENTAL!" )

Which leads us on to our last line:

$sQuoteOfTheDay = $aRow['quote'];

Which just assigns the value of ‘$aRow['quote']‘ to the variable ‘$sQuoteOfTheDay’. In other words, ‘$sQuoteOfTheDay’ now has the value of a random quote pulled from the database of quotes.

To use this in an HTML page, we would simply just use this (AFTER the above code has grabbed the random quote from the DB for us):

echo $sQuoteOfTheDay;

Which will output the quote of the day somewhere in the HTML code.

As I said at the beginning, this can be extended easily to pull an image for a banner by simply changing the quotes table to store image paths such as ‘images/my_image.png’ which can then be pulled in the same way, and then output similar to the following:

My Image

Obviously we changed the variable name here to $sImageOfTheDay just to keep things constant.

Hope this has helped someone!

Posted in MySQL, PHP at February 9th, 2008. 3 Comments.