Search code examples
phpcodeignitercodeigniter-routing

Create Custom URLs


I have a model that returns a list of artist's names from a database along with their ID. I want to loop through these artists in my view and create links to their pages with the following format:

http://www.example.com/the-artist-name/artist-portfolio/ID.html

What is the best way to do this?


Solution

  • Controller

    $data['artists'] = $this->artists_model->get_all(); // should return array
    $this->load->view('yourview', $data);
    

    View

    <?php foreach($artists as $artist): ?>
        <a href="http://example.com/<?php echo $artist['name']; ?>/artist-portfolio/<?php echo $artist['id']; ?>.html">
            <?php echo $artist['name']; ?>
        </a>
    <?php endforeach; ?>