Selecting data for front-end integration with PE. Engraved on March 10, 2008 by Michael Dick; Comments (10)

PE IntegrationSurprising 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.

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 '
  • ' . $row['title'] . '
  • ';
    }
    ?>

    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.

    Comments

    Engraved by sway on 03/10/2008
    woah, a very thorough and noob proof tutorial.

    nice work
    Engraved by Michael Dick on 03/11/2008
    @Sway, I was posting this article to my blog as I was being rushed onto a plane at the airport. I wasn't able to completely upload many cool features that makes this article an easier and more fun of a read -- such as the code blocks.
    Engraved by Jack Keller on 03/11/2008
    Very good writeup Michael! I look forward to playing with PE some more, a project has been keeping me from doing that so far :(

    Keep up the good work!
    Engraved by Michael Dick on 03/11/2008
    @Jack, keep us all updated on how PE works out for you. Good luck on your current project.
    Engraved by Jared on 03/11/2008
    Michael, what a great tutorial on PureEdit's front-end functionality. Thanks so much!
    Engraved by Michael Dick on 03/11/2008
    Long time no talk, Jared. Thanks!
    Engraved by Tristan O'Neil on 06/11/2008
    Please help, it appears as if db.class.php is not included in the newest version of Pure Edit.
    Engraved by Michael Dick on 06/11/2008
    Tristan, I have been meaning to update the tutorial since the release of 1.4. Thanks for calling me on it! I got your email and will reply to your email as well.

    To fix this, change the following line:

    include('pe-admin/classes/db.class.php');
    

    to:

    include('pe-admin/databases/mysql.db.php');
    

    That's all, should work from here. Thanks again for reminding me to comment about this.
    Engraved by Shaun on 06/23/2008
    Hey, trying our PE now, it seems awesome, i love the simplicity so you can still use it like a normal admin, its just like a really well done simple admin panel :D GREAT JOB!!

    Also, using this on my latest project so hope it goes well :D
    Engraved by Michael Dick on 07/10/2008
    Shaun, thanks for the feedback! Head over to the community if you need any help or want to post an add-on you want to share!

    http://www.pureedit.com/community/

    Give your word.

    HTML is allowed...line breaks are converted to <br />'s. You need to be somewhat friendly, at least to me or you'll get deleted. Now off, go post!

    I am currently...

    The ever popular, Twitter >

    Latest posts