CodeIgniter: Interacting With a Database
In 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.
{
$data['query'] = $this->db->get('niches');
$this->load->view('admin_view', $data);
}
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.
echo "Site Name: " .form_input('name');
echo "<br />Site URL: " .form_input('url');
echo "<br /><select name='niche'>";
echo "<option selected='selected' value=''></option>";
foreach ($query->result() as $row):
echo "<option value='$row->id'>" . $row->nichename . "</option>";
endforeach;
echo "</select>";
echo "<p><input type='submit' value='Submit'></p>";
echo form_close('');
And lastly, I needed a function in my front.php controller for putting that inputted information in the database which was as simple as:
{
$this->db->insert('sites', $_POST);
redirect('front/add_blog');
}
I know I've been bad, as I haven't locked down my little administration panel yet, but this does show how easy it is to manage a database with CodeIgniter, from querying the database, displaying rows, and then inserting information into another table, I would have needed to write a lot more PHP had I done this without a framework.
Also, if you know of a way to do this with less code, better code or have any thoughts or opinions on these CodeIgniter posts, please let me know in the comments below.


Want an avatar? Get a gravatar! • You can link to this comment
Hi David,
Don’t know if you just havnt otten there yet in the tutorial, but your db calls ’should’ (in MVC anyway) be in models.
Then your foreach in the view could be in your controller, where you prep an array, and then use the form_dropdown helper.
- I spend more time with codeigniter, than i do my family… Let me know if you need any help that doesnt include RTFM – love getting new people into CI.
Phil
Want an avatar? Get a gravatar! • You can link to this comment
Yeesh. Too many echos. You can just type in html without echos.
Example:
Site Name:
Site URL:
result() as $row): ?>
<option value=’id?>’>nichename?>
Would work, I believe.
Want an avatar? Get a gravatar! • You can link to this comment
That completely mauled my example.
http://pastie.org/380751
Want an avatar? Get a gravatar! • You can link to this comment
Phillip – Haven’t got there yet, I’ve only given myself the View and Controller parts of the MVC model.
Thank you though. I will look into it, as I do want to do it right (not just easy).
Jason – Why would I open and close PHP so often? Hasn’t that been proven to be slower?
Want an avatar? Get a gravatar! • You can link to this comment
It’s slower by tens of thousandths of a second over thousands of iterations.
But it saves time when you have to edit code because it’s easier to read. Especially when your syntax highlighting isn’t screwed up by everything being in an echo.