Categories
PHP

Arrays in PHP

Some day I'll get around do writing some stuff on arrays in PHP.  In the meantime, here are some links to another site with some outdated but still quite useful information on arrays in PHP.

Categories
Database MySQL PHP

Pulling a single value from the database

Sometimes you just want to pull a single value from the database without a whole lot of work.  Here's how to do it in a single line;

MySQLi

// Assumes that a mysqli connection ($db) has been made to the database
$value = $db->query("SELECT value FROM table WHERE condition = 'met'")->fetch_row()[0];

MySQL (Deprecated)

// Assumes that a connection exists to the database (mysql_connect()) and a database is selected (mysql_select_db())
$value = mysql_result(mysql_query("SELECT value FROM table WHERE condition = 'met'"), 0);
Categories
PHP Web Development

Formatting dates in PHP

Formatting dates in PHP can seem a little daunting.  The best way to start is to ensure that you've stored all your dates with the slightly counter-intuitive time() function. This stores the date and time together in your database as one long number that really doesn't mean anything. This is a Unix timestamp.
The date() function is then used to reformat it. For instance 1206371089 comes out to March 24th, 2008 at 15:04:49 (GMT).

If your date/time is stored in the variable $lastupdated, you can display it in a friendlier format as;
[sourcecode language='php']
echo date("Y-m-d H:i:s",$lastupdated);
[/sourcecode]

which will give you a date display like 2008-03-24 13:04:08 (note that this format is well suited to sorting chronologically in the output if it needs to be sorted after processing.)

If you need to refomat a date stored as a string, this is the way to do it;
[sourcecode language='php']
echo date("l jS F Y",strtotime("2008-03-05"));
[/sourcecode]

You can format the dates pretty much however you want.  For a list of the format codes, it's best to go straight to the source at http://www.php.net/date.

Categories
PHP Web Development

Alternate row background colouring in PHP (with only one line of code)

Alternate row background colouring in PHP is something I've seen and used often, but I was fooling with it on one of my projects today and realized it could be done with one line of code.

First, notice that I've got a while loop to cycle through the records in a dataset I've pulled from a database. You'll also see in the first and last rows that I've created the table outside the WHILE loop because the table is only created once. A row is created once for every row in the dataset so it's inside the FOR loop.

To set the background row I use a ternary operator which tests a condition, then assigns one of two values based on the evaluation of the condition. If you aren't familiar with the Ternary Operator, get to know it. It will dramatically simplifyyour code. Here's a basic example; 

1
$variable = (colour = "green") ? "The colour is green." : "The colour is not green." ;

So looking at the green line, basically what it says is this;

If the variable $bgcolour is not set, or if it is "$F0F7FF", set the value of $bgcolour to "#FFFFFF".

Otherwise, set the value to #F0F7FF". 

Note here that the condition has two parts separated by the OR operator, "||".  The first condition !isset($bgcolour) checks to see if the variable doesn't exist.  This will occur the first iteration and putting it here saves me from having to initialize the variable beforehand.

1
2
3
4
5
6
echo "<table>";
while ($row = mysql_fetch_array($dataset)) {
  $bgcolour = (!isset($bgcolour) || $bgcolour == "#F0F7FF") ? "#FFFFFF" : "#F0F7FF";
  echo "<tr style='background-color:$bgcolour;'><td> TABLE DATA GOES HERE </td></tr>";
} // end while
echo "</table>";

Keep in mind – if you use this trick twice on the same page, you'll have to initialize the variable to ensure that the first row background is the same on each table.  You want it to be consistent on all tables.  In this situation it's best to initialize the value just above the while() loop as shown here.

 

1
2
3
4
5
6
7
echo "<table>";
$bgcolour = "#FFFFFF"; // initialize it here, outside the while() loop
while ($row = mysql_fetch_array($dataset)) {
  $bgcolour = ($bgcolour == "#F0F7FF") ? "#FFFFFF" : "#F0F7FF";
  echo "<tr style='background-color:$bgcolour;'><td> TABLE DATA GOES HERE </td></tr>";
} // end while
echo "</table>";