I have a model that returns a list of artist's names from a database along with their ID. I want to loop thr开发者_开发问答ough 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?
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; ?>
Pass the data from the model into the view and loop through it like you normally would.
In your controller:
$view_data['artists'] = $this->artist_model->get_artists();
$this->load->view('view.php', $view_data);
In your view:
foreach ($artists as $artist) {
echo "<a href=\"http://www.example.com/{$artist['name']}/artist-portfolio/{$artist['id']}.html\">{$artist['name']}</a>";
}
精彩评论