2011-09-22 174 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