Pretty URLs using url_title() function

This example is based on the artists/view, albums/view and songs/view function and the source can be found here.  See example here

When creating a website your website’s URLs are very important.  It makes it easier for users to remember, easier to know what you are getting based on the URL alone and there is a firm belief that it makes a difference with search engines.  With url_title() function you can take any text, for example a blog header, a song title and create a great looking, functional URL.

For example:

  • Bad: songs/view/102/1234/150502
  • Bad: songs/view/109230230
  • Ok: songs/view/A-Nightmare-To-Remember (but this doesn’t work if you have multiple items with the same name.
  • Best: songs/view/Dream-Theater/Black-Clouds-Silver-Linings/A-Nightmare-To-Remember

By doing the URL this way you are including all relevant information for your page.  This type of multi-part URL works best with a hierarchical database structure with one to many relationships.  Each artist will have one or more albums and each album with have one or more songs.  So this will allow us to find “Truth” by Devin Townsend even though there are dozens of songs called “Truth”.  We are now about to find a song by an artists in a specific album without having to use numbers or cryptic urls.
Then to decode the new URL and get the variables you need just do something like the following in your controller function.

$artist = $this->uri->segment(3);
$album = $this->uri->segment(4);
$song = $this->uri->segment(5);
$this->load->model('SongModel');
$data['query'] = $this->SongModel->loadExtended($song, $album, $artist);

 if($data['query']->num_rows() > 0)
 {
 ...
 }

To set this up correctly in your database you need to have a title field and a url_title field which will hold the appropriate values.  Instead of looking up artist, album, and then song we make one database query that uses joins and multiple wheres.  In album/view I used a repeated setup which allows us to do error checking in the title and if the album was wrong redirect to the artists/view page.  We felt that a process like that for songs with 3 queries per page load would be too resource intensive for the such a limited benefit.

songmodel.php


function loadExtended($song, $album, $artist)
 {
 $this->db->join('albums', 'albums.album_id = songs.album_id');
 $this->db->join('artists', 'artists.artist_id = songs.artist_id');
 $this->db->where('song_seo_name', $song);
 $this->db->where('albums.album_seo_name', $album);    
 $this->db->where('artists.artist_seo_name', $artist);
 return $this->db->get('songs');

 }

Now when you add a new song or story or whatever you are doing just use the url_title($title) function to make pretty URLs.


$insert['song_seo_name'] = url_title($this->input->post('song', TRUE));

It a really simple process that makes your site’s URLs much more attractive and easier for search engines.

September 16, 2009 at 3:27 pm Leave a comment

A website dies, a blog is born

I decided to start this blog because of the kind and supportive words of the people on the Codeigniter forums. The code that will be featured on this blog will be in large part based around the great Codeigniter framework but many elements of standard PHP will inevitably be apart of my work.

After over a year of development, marketing, interviews, and research we have decided to essentially end our project Unravel The Music.  This site was intended to be a competitor with long standing king of song meanings, songmeanings.net.  We tried to create a user experience that was fluid, practical and sexy.  I do believe we accomplished that as a two man team but because the content of our website was music lyrics and a recent lawsuit brought upon two lyric sites we thought it would be wise to discontinue offering lyrics.  The site remains overall functional with a large catalog of 40,000+ artists, their albums and songs, dozens of songs meanings and comments, blogs posts and artists interviews.  We decided that we would be unable to maintain what would amount to as a simple music news blog.  We are both college students, that works and we don’t really have time for chasing down leads and attending concerts 5 days a week.

This blog will be a catalog of our work.  With it we hope to help people develop and expand their coding and design skills.  I will be dissecting the code that I had written over the past several months, telling why I did it, how it works and what I think about it.  I’m sure I’ll find some mistakes which will make me a better coder and I hope others learn from it as well.  Hopefully, my brother will be discussing design, usability and images with you but he hasn’t committed to anything yet.

Source: http://www.unravelthemusic.com/unravelthemusic.com.zip

website: http://www.unravelthemusic.com/

September 14, 2009 at 8:53 pm 2 comments

Using Codeigniter Pagination Class

This is my first post about my code so I thought I start with a fairly simple topic, pagination.  Pagination with Codeigniter’s pagination library is fairly straight forward but I have picked up a few tricks that I’d like to share.  This is however a bit more complicated than regular pagination because we will be paginating based on the first letter of a artist name in a database.  This example is based on the artists/catalog function and the source can be found here.  See example here


$this->load->library('pagination');

$letter = $this->uri->segment(3);
$offset = $this->uri->segment(4);

$data['letter'] = substr($letter, 0, 1);

if (!ereg('^[A-Za-z0-9]$', $data['letter'])) {
  redirect('artists/');
}

$this->load->model('ArtistModel');
$config['per_page'] = '100';
$data['results'] = $this->ArtistModel->loadByLetter($data['letter'], $config['per_page'], $offset);

$config['base_url'] = base_url() . '/artists/catalog/' . $data['letter'] . '/';
$config['uri_segment'] = 4;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['num_links'] = 6;
$config['total_rows'] = $this->ArtistModel->getTotalForLetter($data['letter']);

$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();

Pretty standard stuff if you follow the manual page except for a few things. First off, I take the ‘uri segment 3′ and I substr it to just one character. Therefore, if someone types in ‘abcderf’ I just get ‘a’.

On line 8-10 You see an ereg function. This function tests to make sure that $data['letter'] is a character or a number, and if not redirects to the basic artists page.  This could be done by passing the variable through the form validation class like I have done with searches but for 1 character it seems pretty silly to fire up a whole library to do what a simple ereg could accomplish.

After this point the $config variable is all pretty standard but the important part is the model functions because this is based on finding the first character of a name.

Here is the first model functions required

function loadByLetter($letter, $num, $offset)
 {
 if($letter == '0')
 {
 $this->db->where("artist REGEXP '^[0-9]'");
 $this->db->where('verified', 1);
 $this->db->order_by('artist', 'ASC');
 return $this->db->get('artists', $num, $offset);

 } else {
 $this->db->like('artist', $letter, 'after');
 $this->db->where('verified', 1);
 $this->db->order_by('artist', 'ASC');
 return $this->db->get('artists', $num, $offset);
 }
 }

As you can see we have the variables letter, num and offset which are all important in selecting data from the database. If the letter is the number zero (0), which is the default click options on the right side menu for artists beginning with numbers, it will look for all artists starting with a number. This is the case because we use $this->db->where(“artist REGEXP ‘^[0-9]‘”); to select the data from the database.  This regexp function will select all artists that begin with a number 0-9. We then only select those that are verified and arrange them by name.

If the $letter variable is not zero then we use a like function $this->db->like(‘artist’, $letter, ‘after’);. This will select all things that start with $letter, are verified and order them by artist name.  The ‘after’ portion of the like statement means that the wildcard (%) will only be placed after the $letter variable.  The after is crucial in making sure we only get artists that start with that letter.

The offset variable is used to tell the select statement how many to skip.

The other important model function we need is one that will tell us how many artists we have for that particular letter. The functions before are always using a limit of $num which for us was set to 100. We then need to use the following function to find the total.

function getTotalForLetter($letter)
 {
 $this->db->like('artist', $letter, 'after');
 $this->db->where('verified', 1);
 return $this->db->count_all_results('artists');
 }

This function will use the same like function as before but without the limit.

Then that’s it. Pass the $data['links'] variable to your view file and the pagination class takes care of the rest.

–Drew Town

September 14, 2009 at 3:38 pm 1 comment


Categories

Feeds


Follow

Get every new post delivered to your Inbox.