我需要为视图生成url链接,并将数据库中的数据显示到此视图中。我的主要观点是艺术家/歌手,它有一个歌手表。每个歌手应该是一个链接,当我按下时会带我到他的艺术家页面,带有他的专辑,传记和歌曲。
我需要在Code Igniter中执行此操作,当然我可以阅读文档,但需要一周左右的时间,我只有2天。
这是我的代码:
$this->load->database();
$artists = $this->db->query('SELECT a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id');
echo "<tr><td>Artist</td><td>Year</td><td>Country</td></tr>";
foreach ($artists->result() as $row)
{
//echo $config['base_url'];
echo "<tr>";
echo "<td><a href=\"/Artist.php\">" . $row->name . "</a></td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->name . "</td>";
echo "</tr>";
}
echo "</table>";
答案 0 :(得分:1)
找到表艺术家的主键,用于expample id或artist_id并添加到查询
$artists = $this->db->query('SELECT a.artist_id,a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id');
并在您的代码中
foreach ($artists->result() as $row)
{
//echo $config['base_url'];
echo "<tr>";
echo "<td><a href=\"/Artist.php?aid=" . $row->artist_id . "\">" . $row->name . "</a></td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->name . "</td>";
echo "</tr>";
}
echo "</table>";
并在控制器艺术家通过$ _GET ['aid']
获取数据答案 1 :(得分:1)
我认为你需要创建一个像
这样的路线$route['artist/(:any)'] = "controller/load_artist_page/$1";
并在你的href标签中
base_url('artist/'.$parameter);
并在函数中获取id并加载该视图,
function load_artist_page($artist_id){
//do processing and load view.
}
在传递之前加密id。
答案 2 :(得分:1)
我会做这样的事情
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');
模型
Example_model.php
<?php
class Example_model extends CI_Model {
public function getArtist() {
$artists = $this->db->query('SELECT a.artist_id,a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id');
return $artists->result();
}
}
控制器示例
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('example_model');
}
public function index() {
$data['artist'] = $this->example_model->getArtist();
$this->load->view('example_view', $data);
}
}
然后在视图
<?php foreach($artist as $row) {?>
<tr>
<td><?php echo anchor('artist/' . $row->artist_id, $row->name, array('target' => '_blank'));?></td>";
<td>$row->date</td>
<td>$row->name</td>
</tr>
<?php }?>
然后在路线上
$route['artist/(:any)'] = "controller/function/$1";