Step 6 – Creating categories for a multilanguage site in CodeIgniter

Created at: February 10, 2015; Last update: April 13, 2015

(created at: February 10, 2015; last update: April 13, 2015)
Well… This tutorial is in two versions. Let me explain why…

The first time I created the tutorial, back in 10 february 2015, I was still in some testing regarding a multilanguage CMS, but now, in april 2015, all seems a bit clearer. So, after I’ve created an admin area for pages, I returned to this tutorial about categories and I found myself saying “What the fuck was I thinking??? Packs??? What is thaaat??? A name column in categories table??? WHYYYYYY???”. If you still want to see that old tutorial, you can, as I’ve left it at the bottom of this new version.

And when I am saying “new version” I am actually saying that we should simply do the same as we did in the previous tutorial about creating, listing and editing pages. The only difference would be the name of the tables and replacing “page” or “pages” in the code with “category” and “categories”. Simple, no?

The tables

The models

The Controller

The views

And that’s it. BUT… Did you notice something about the Pages controller and the Categories controller? Is there something fishy in there? Please comment below if you notice what I am talking about. If you don’t know what I am talking about, I will tell you in the next episode.

Here you have an ARCHIVE WITH THE APPLICATION we’ve created so far. Take care as this archive is not containing a system directory. You should download a fresh copy of CodeIgniter 3, put all the files from the archive inside your fresh framework, set the database configuration and then import the ci_app.sql into your database.

Now… the old tutorial follows:

Once we’ve made our multilanguage site we must think about populating it with content. So let’s start by creating the categories for our posts.

How do we organize our tables

First thing to think about is the layout (schema) of our database. We already have a table named languages. So let’s think how we can set the tables so that they will allow for same content to be available in several languages. We will try to create tables that will be very comprehensive.

The packs table

In the pack table will have e some general data that is the same for all languages:

– an id column
– a type column (example of types: menu_items, categories, posts, pages)
– a parent_id column
order column (who knows, maybe we need to order the items)
– a created_at column
– an updated_at column
– a created_by column
– an updated_by column

Ok. Let’s create it:

We should also add an index on name column.

Cool.

The categories table

Now let’s create the categories table. For this we will need the following columns:

– an id column
– a pack_id column
– a language_id column
– a name column
– a description column
– a slug column

And that’s about it…

So the MySQL will look like this:

Now, let’s do an index on slug column.

OK. Database ready for categories.

Now let’s look at our files

application/controllers/admin/Categories.php

application/models/admin/Category_model.php

application/models/admin/Pack_model.php

application/views/admin/categories/index_view.php

application/views/admin/categories/create_view.php

application/views/admin/categories/edit_view.php

application/views/admin/categories/edit_pack_view.php

Well… As you can see, creating a multilanguage site is not that easy as it may seem…

15 thoughts on “Step 6 – Creating categories for a multilanguage site in CodeIgniter

    1. avenirer Post author

      Yes. you can find it at the end of the new tutorial (do a search in the page for “the application”).

      Reply
  1. Samuel

    Noticed you merged the category_translation and the page_translation. Why were they separate in the first place.I was wondering what the categories were. Great tutorial. I was searching on how to use Ion_auth. This opened my eyes to multi_language applications which is great. Nice one

    Reply
    1. avenirer Post author

      Are you referring to one of my Github repositories (https://github.com/avenirer/CodeIgniter-multilanguage-site)? If you are, then congratulations for looking into it 🙂 What I thought in the first place when creating the multilanguage site was to have different tables for different content types, thinking that… well… they are different. But a week ago I thought: “Wait a minute… They are not that different… So why not create a ‘contents’ table and a ‘content_translations’ table?”.

      PS: If you want to try the site on Github, you can use “administrator” as username and “password” as password.

      Reply
    1. avenirer Post author

      Now you can find the sql file in the github repository. About working on some projects together, why not?

      Reply
        1. avenirer Post author

          This is a tutorial, not an application for you to test… You are doing the testing by creating the categories, editing them, deleting them and listing them.

          Reply
      1. Granit

        Hi Avenir ,

        Why this site wont work when i try to load from localhost ? It shows to me a lot of errors and when i try to login it just refreshes the page , and when i check in database there is no username and password registered as you have mentioned . I downloaded the code from your git repository but it doesn’t work for me . Can you help me ?

        Thanks !

        Reply
        1. avenirer Post author

          It seems that the problem comes from the sql file. Try downloading again from Github and use multilanguage.sql to set up the tables

          Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.