我有一个模型,它从数据库中返回艺术家姓名列表以及他们的 ID。我想在我的视图中遍历这些艺术家,并使用以下格式创建指向他们页面的链接:
http://www.example.com/the-artist-name/artist-portfolio/ID.html
做这个的最好方式是什么?
我有一个模型,它从数据库中返回艺术家姓名列表以及他们的 ID。我想在我的视图中遍历这些艺术家,并使用以下格式创建指向他们页面的链接:
http://www.example.com/the-artist-name/artist-portfolio/ID.html
做这个的最好方式是什么?
控制器
$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; ?>
将模型中的数据传递到视图中,并像往常一样循环遍历它。
在您的控制器中:
$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>";
}