创建自定义URL

时间:2011-09-22 14:13:44

标签: php codeigniter codeigniter-routing

我有一个模型,它返回数据库中艺术家姓名的列表及其ID。我希望在我的视图中循环浏览这些艺术家,并使用以下格式创建指向其页面的链接:

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

这样做的最佳方式是什么?

2 个答案:

答案 0 :(得分:0)

控制器

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

查看

<?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; ?>

答案 1 :(得分:0)

将模型中的数据传递到视图中,并像往常一样循环遍历它。

在您的控制器中:

$view_data['artists'] = $this->artist_model->get_artists();
$this->load->view('view.php', $view_data);

在您看来:

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