Selecting data for front-end integration with PE. Engraved on March 09, 2008 by Michael Dick

Surprising or not, one of the most requested features for PureEdit is not actually a feature but a tutorial. More and more adopters are asking me how to integrate the PureEdit database to the front-end of their website because they have not yet reached the level of PHP to do so.

Note: This blog post is out dated and has not been updated. Please make your way to the PureEdit Community for any questions.

When I launched PureEdit, I was assuming that the majority of the adopters would be hybrids like me, those needing a powerful, yet simple solution to CMS development so that they could focus more on the front-end programming. However, I am excited to see that my assumptions have proved me wrong and I am glad to be able to write this tutorial in hopes of helping out those in need.

Before we begin.

In this tutorial I am going to be using tools that are included in PureEdit that help make database communication simpler. Just remember that you can program the PHP for your front-end however you'd like.

It is also assumed that you know the very basics of PHP. If you don't, then I am going to assume that you are smart enough to survive because you have got this far.

Setting up PureEdit

The first thing you need to do is obviously install PureEdit and get your CMS up and running by itself. Some people may want to build the database as they are building the front-end, and work together as they go. (That's how I do it). But, for this example we will pretend that PE is already installed and ready to go.

Designing the interface.

I am not going to go into detail about the interface because the interface is your own deal. You will make it how you wish, so for this example I am going to use a simple fictitious design I cooked up for PureEdit in a few minutes. You can see my static interface here.

Connecting PureEdit to your website.

It is now time to start working with PHP and adding the code necessary to connect your front-end to PureEdit's database. Add the following code to the top of your HTML above your HTML or DOCTYPE declarations.

The second line of code is including all the settings PureEdit needs to function, such as database access credentials, your site title, and your site url. The third line is including the file that converts the most used settings from the settings.lib.php file into an easier to use variable name called a constant, such as $settings['site']['title'] to SITE_TITLE. And, last but not least the fourth line is including the database file that connects to your database and opens access to use all of PureEdit's powerful database tools.

Selecting data from your database.

Now comes the fun part. It's time to take the data you have created via the PureEdit interface and then placing it onto your website. There are two basic ways to select data from your database.

  • Select one row/entry at a time.
  • Select all rows/entries at once.

To select data from the database we are going to minimize a lot of code by using a special tool PureEdit provides for you called the $Db->select() function. If you send only one parameter to this function then it will assume you are wanting a full list of what is in the table and spit back the lists in an array such as this below.

$Db->select("tableName")

If you send two parameters to the select function then it assumes that you want to select only one specific row and will proceed to only return the row which corresponds with the id you send to it. The code to do so is below.

$Db->select("tableName", idNumber)

Selecting all rows from a table.

Let's take what we just learned above and use it. To select all rows from a table called bulletinposts at once you will need to start with the basic code below.

select("bulletinposts");
while($row = mysql_fetch_assoc($getRows))
{
echo '<li>' . $row['title'] . '</li>';
}
?>

On line 3 of the code above we are assigning a database select query to the database to the $getRows variable. On line 4 we are looping through all the entries/rows found and then on line 6 we are using that code as the code to display for each row/entry found.

Selecting one row from a table.

To select one row/entry at a time from the bulletinposts table you will need to use the basic code below.

select("bulletinposts", $_GET['id']);
$row = mysql_fetch_assoc($getRow);
echo '

' . $row['title'] . '

';echo '' . strip_tags($row['body']) . ';

The code on line 3 is doing almost the same as before, however we have added a new parameter telling the PHP which entry/row to select specifically by using the $_GET['id'] variable. (If you don't know how to use $_GET[] and $_POST[] variables, you can find those in the PHP documentation.) On line 4 we are telling the PHP to fetch the data from the database and assign the values to the $row[] array.

Lines 5 and 6 are simply echoing the values by taking the column name and placing it inside the [] symbols. For example: $row['columnName']. For this example you will notice I wrapped the strip_tags() function around the $row['body'] variable which removes any HTML tags from the value, thus removing any img tags I had in my body.

You can see it all in action or you can view the PHP source code I used.

Conclusion.

Selecting data from a database is pretty simple once you wrap your head around it. I really hope this tutorial not only helped you understand how to use PE on your front-end, but also how to use plain old PHP in general. If you are having any trouble you can always head over to the PureEdit Community where you will find plenty of talented help there.

Until next time, follow me on twitter, @michaeldick