PHP

...now browsing by category

All about the PHP language.

 

Drupal 6 Social Networking

Saturday, April 18th, 2009
Drupal 6 Social Networking is one of the lastest publications addressing the use of the popular open source content management framework.

Drupal 6 Social Networking is one of the lastest publications addressing the use of the popular open source content management framework.

I’ve fought it for a long enough time, but I’m finally taming my dislike of Drupal.  I’ll admit - its mostly because I’m now using it for what it is - a framework and not as a content management system.  Now I seem to be growing as a Drupler.  Fast-foward to a few weeks back: I was browsing about Amazon and came across Drupal 6 Social Networking.  After reading the entire book in a few days, I felt it deserved a review.

Drupal 6 Social Networking is written to reach a wide audience - from programmers to content administrators, no matter the level of experience.  It is authored by Michael Peacock of Peacock-Carter, a web developer from Newcastle, UK.

The book is broken down into 10 chapters, ranging from an introduction to drupal and social networks, installing drupal, using the framework to create the functionality desired, and even into web site promotion.

The highlights of this book is that is an easy read and is written for those that are not exactly sure of what certain features relate to in the system.  For example, have you ever wondered what the full use of Taxonomy is in Drupal?  Peacock has a casual writing style that gets directly to the point, accompanying his writing with on-point visual examples to add visual detail to his writing.

If you are new to the Drupal framework, this book will guide you from installation to project setup.  If you are seasoned with the framework, but not exactly sure how to make the best of specific modules to get the performance / functionality you need, this book will enlighten you with which module to use and why to use it.

Overall, this is a worthy purchase that gets directly to what the title states - drupal 6 and social networking.  This mean, you guessed it, Michael directly focuses on how drupal can be used to build a social network and he doesn’t slide off topic in doing so.

For those interested:

5-for-1 Deal from SitePoint - profits going to victims of Australian bushfires

Thursday, February 12th, 2009


SitePoint Special Sale

Just thought I’d pass this awesome offer along to those that read the blog.

SitePoint is offering a deal to get 5 of their books in PDF format for only $29.99.

The beauty of it is that the proceeds are going to be donated to the victims of the Australian bushfires.

I’ve read a few of the books and some are hit or miss.  I definitely recommend:

Check it out, its for a good cause!

Populate DropDown List with MySQL and PHP

Friday, August 22nd, 2008

Drop-down lists have long been a form designer’s friend in terms of packing a lot of choices into a single area. The maintenance of these however can become tedius over time if you must continually update the list.

In the static Web 1.0 days, we would have coded each option in the list. Fortunately through the use of a server-side language and a database we can make this a lot more efficient.

In this example I will showcase how to do this in PHP. For those that use ASP.NET, I’m pretty sure you are accustomed to the drag-n-drop capabilities of Visual Studio, where you simply drag-n-drop and set your connection settings accordingly.

In PHP however, it is a little more work. In the following I will have assumed you already have a table named states created with 3 columns (I used state_id, state, state_abbr - *If you haven’t created the table, Kim Briggs has a wonderful sql example at http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-create-state-table.file .*

I will also assume that you have already connected to the database in your file.

We will begin first by creating a variable which will hold our SQL query. From there we will create a variable that will hold the result.

<?
$query="SELECT * FROM state";
$result = mysql_query ($query);

Then we shall begin the drop down list:

echo "<select name='state' value=''>State</option>";

And we will use a while statement to create an array to cycle through the results printing HTML options for each item. As you will see, we will be binding the columns to the values that we want to print on the page.

while($myfetch=mysql_fetch_array($result)){
echo "<option value=$myfetch[state_id]>$myfetch[state_abbr]</option>";
}

From there we will close the list and the PHP file.:

echo "</select>";
?>

And for the code in its complete form:

 <?
  //Create SQL Query
  $query="SELECT * FROM state";
  // Variable for results
  $result = mysql_query ($query);
  // Begin select tag
  echo "<select name=state value=''>State</option>";
  //Return each result
  while($myfetch=mysql_fetch_array($result))
  {
     echo "<option value=$myfetch[state_id]>$myfetch[state_abbr]</option>";
  }
  //End select
  echo "</select>";
?>

Easy enough huh? Do note that with the query, we can control how the results are printed (IE alphabetical, by id, etc.). This can be used for a variety of fields, from states to categories.

Test Database Connectivity in PHP with PDO

Tuesday, August 19th, 2008

Testing connectivity to a database can be achieved a few different ways in PHP. Today we’ll take a quick look at a feature called PHP Database Objects (PDO). PDO is defined by php.net as:

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.

Now, on to how this bad boy works.

As with anything, we must start by assigning various variables. Similar to other methods of connecting, we will assign variables for the mysql connection host, database, username, and password.

// Assign variables for connecting to MYSQL Host
$dsn = 'mysql:host=localhost;dbname=mydatabase;';
$user = 'user';
$password = 'password';

In other ways, we could break it down into more variables:

$db_host = 'localhost';
$db_name = 'mydatabase';
$user = 'user';
$password = 'password';
$dsn = 'mysql:host=$db_host;dbname=$db_name;';

From there we will move on to a try catch statement using the PDO class to create a<strong> new PDO()</strong>. We will use our trust try/catch statement to test the connection and report any errors that may occur.

// Test the connection, alert if fails - very similar to C# in a sense
try
{
$sqlconn = new PDO($dsn, $user, $password);
//echo 'success';<img class="btnimage" onclick="formattext('text', 'outdent', '')" src="http://www.bobbyhash.com/wysiwyg/kevinrte/images/outdent.png" alt="" width="25" height="24" />
}

We will use $e as the exception / what our event will argue.

catch (PDOException $e)
{
echo 'Connection failed: ' .$e->getMessage();
}

As you can see it is a simple process. Set the variables, then utilize a try/catch statement to make the PDO request / handle the error.

And now for the unmolested code: Enjoy!

// Assign variables for connecting to MYSQL Host
$dsn = 'mysql:host = localhost;dbname=litebox;';
$user = 'user';
$password = '';

// Test the connection, alert if fails - very similar to C# or
//any other try/catch in a programming language in a sense
try
{
$sqlconn = new PDO($dsn, $user, $password);
//echo 'success';
}
catch (PDOException $e)
{
echo 'Connection failed: ' .$e->getMessage();
}

What? My Program Works?

Monday, May 21st, 2007

I gotta say, the ability to understand programming pays off. After getting fed up with the time I was wasting logging into Ta-Da List I decided to write my own listing program.

I like to look at it as a list management system.

I really didn’t need to be super organized, such as organizing by day, week, etc. I created the list just how I would create my own sticky notes - random, jot some info down with a title and post it. The program works just the same - I have an idea, I click my bookmark, the web application comes up and I’m ready to go. Add a title, a little descriptive information and wah-la! It’s posted. I can even edit the list if I screw something up and I can delete it once the task/list/whatever is completed.

I’m still thinking about releasing it once I complete the interface to meet my tastes. Will it be open source? Most likely!

PHP and MySQL are a great marriage. What are some of the applications some of you guys are building to meet your needs?

CodeIgnitor Test Drive

Thursday, May 10th, 2007

As I posted last night, I downloaded CodeIgnitor, a PHP framework. I must say, so far I am very impressed.

After looking at some of the video tutorials to get a feel for how the framework is based (simple Hello World, Blog in 20 Minutes), I thought I would give it a try. I started by creating a few simple scripts, ranging from ‘Hello World’ to a basic string that would print variables and solutions to a math problem. From there I created my own blogging system in under 50 minutes, which isn’t bad at all I think for just picking up the system.

Now it’s time for the fun-run. The listing system I created a few weeks ago is taking shape but is far from where I wanted it. Currently it runs on close to, give or take, 8 files. After 35 minutes I have recreated the system with CodeIgnitor. All that is left is to add the delete function and it will be set to go.

All in all, I think this is a great resource for PHP developers and those that are just picking up the language. If you have programmed in Java, then you will definitely appreciate the libraries as it will make time more manageable in your PHP development.

Test Driving CodeIgniter

Wednesday, May 9th, 2007

I’ve recently downloaded CodeIgniter, a PHP framework. From what I’ve seen from the videos it looks to quite effective. I’ll be testing it out this week and will post later on my thoughts and comments.