CodeIgniter: Interacting With a Database

ci_logo_flameIn my previous post entitled, CodeIgniter: PHP Development Fun – Part 1, I talked a little about my excitement regarding CodeIgniter. So far, I have found the learning curve to not be too steep and continue on my trek to develop my own website with the framework.

Today, I wanted to talk about what I have been working on with CodeIgniter. I decided I wanted to make my own versions of Popurls, or AllTop. This is something I could have done in an hour or two with WordPress and some plugins as well as a bit of SimplePie usage, but on CodeIgniter, it has taken a little longer, but the excitement I experience as each piece works is much greater than if I had done the hack job of using WordPress for something it wasn’t intended for.

The first step for me was coming up with my database and then adding some information to it, and using the scaffolding feature in CodeIgniter, I was able to do this quite easily. Unfortunately, this wasn’t perfect as I was using two tables, and taking information from one table, to be used to reference the other was a pain in the butt. I decided to create my own administration panel to control the input of data into my site.

There were only three pieces of data that I wanted to have to input: the site name, the URL and selecting which category I wanted it to be in.

At first, I coded the category selection system in a convoluted way before realizing how silly it was. Much of the mistakes I have made in programming thus far have been planning and logic mistakes that could have been avoided if I had taken the time to really sit down and think through my plans.

My Administration Panel

Creating my administration panel was as simple as adding another view with the relevant form, and creating a function to insert the information into my database.

I first had to create a function in my front.php controller related to showing the administration panel view, and showing the right categories in my option box.
[php]
function add_blog()
{
$data[‘query’] = $this->db->get(‘niches’);
$this->load->view(‘admin_view’, $data);
}
[/php]

Then I needed to create a form in my admin_view.php file to fill out the details of the site I was adding. The following is of course without the requisite xhtml that should surround the form and create a layout to look pretty.

[php]
echo form_open(‘front/submit’);

echo “Site Name: ” .form_input(‘name’);
echo “
Site URL: ” .form_input(‘url’);
echo “