2011-09-22 184 views
1

我有一个模型,可以从数据库中返回艺术家名称列表及其ID。我想遍历我认为这些艺术家和创建链接到他们的格式如下页面:创建自定义网址

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

什么是做到这一点的最好方法是什么?

回答

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

你知道如何在模型中做到这一点?即让视图更清洁? – freshest

+0

模型使控制器更清洁。控制器使视图更清洁。将数据库和验证规则放入模型中,并在控制器中准备数组并将它们发送到视图。在视图内环绕它们。这看起来很干净。 –

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>"; 
} 
+0

codeigniter足够智能加载'view.php'吗?我从来没有在我的view()调用中包含文件扩展名。 –

+0

除非您使用'.php'以外的内容,否则不需要指定文件扩展名。 – birderic