我正在一个数据库驱动的网站上工作,我希望我的链接的 URI 段从我的数据库中加载相应的数据。该站点在 CodeIgniter 2.1.4 上运行,带有 JQuery Mobile 通过 AJAX 加载页面。这是我的 URL 示例。
site.com/browse(运行索引功能,加载艺术家列表)
site.com/browse/artist/album(运行艺术家功能,将第三个 URI 片段专辑作为参数传递给数据模型,加载该艺术家的专辑列表)
我了解如何将 ID 从 URL 传递到我的模型以加载数据并将该数据处理到我的视图中。调用我的 Browse 类的 index 函数时,一切正常。我能够创建艺术家列表。调用 Artist 函数时,URI 段被传递给模型并加载数据,但 $data 似乎没有传递给我的视图,因此在尝试显示我的数据时我不断收到未定义的变量错误。
获取数据.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Get_data extends CI_Model {
function getAllArtists() {
$query = $this->db->query('SELECT DISTINCT ARTIST FROM data ORDER BY ARTIST');
if($query->num_rows() > 0) {
foreach($query->result_array() as $row){
$data[] = $row;
}
return $data;
}
}
function getAllAlbums($artist) {
//Get artist from URI segment
$artist = urldecode($this->uri->segment(3));
$query = $this->db->query('SELECT DISTINCT ALBUM, ARTIST FROM data WHERE ARTIST = "'.$artist.'" ORDER BY ALBUM');
if($query->num_rows() > 0) {
foreach($query->result_array() as $row){
$data[] = $row;
}
return $data;
}
}
}
浏览控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Browse extends CI_Controller {
public function index(){
//Page Data
$data['title'] = 'Site: Browse';
$this->load->model('get_data');
$data['rows'] = $this->get_data->getAllArtists();
$rows = $this->data['rows'];
//Page Views
$this->load->view('t/header', $data);
$this->load->view('p/browse', $data);
$this->load->view('t/footer');
}
public function artist($artist){
//URL Decode
$artist = urldecode($artist);
//Page Data
$data['title'] = 'Site: '.$artist;
//Load model and retrieve data from database
$this->load->model('get_data');
$this->data['rows'] = $this->get_data->getAllAlbums($artist);
$rows = $this->data['rows'];
//Page Views
$this->load->view('t/header', $data);
$this->load->view('p/artist', $data);
$this->load->view('t/footer');
}
public function album($album){
//Page Data
$data['title'] = 'site: '.urldecode($album);
//Load model and retrieve data from database
$this->load->model('get_data');
$this->data['rows'] = $this->get_data->getAllSongs($album);
$rows = $this->data['rows'];
//Page Views
$this->load->view('t/header', $data);
$this->load->view('p/artist', $data);
$this->load->view('t/footer');
}
}
浏览视图(这可以正常工作):
<!-- Start content.php -->
<div data-role="page">
<div data-role="header" data-id="header" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="http://www.site.com/home" class="" data-transition="fade">Home</a></li>
<li><a href="http://www.site.com/browse" class="" data-transition="fade">Browse</a></li>
<li><a href="http://www.site.com/about" class="" data-transition="fade">About</a></li>
<li><a href="javascript:void(0)"id ="toggle">▶</a></li>
</ul>
</div>
</div><!-- /jqm-header -->
<div data-role="content">
<ul id="song_list" data-role="listview" data-filter="true">
<?php foreach ($rows as $r) {?><li data-icon="false"><a data-transition="slide" href="http://www.site.com/browse/artist/<?php echo urlencode($r['ARTIST']); ?>"><?php echo $r['ARTIST']; ?></a></li><?php };?>
</ul>
</div>
<div data-role="footer">
<h4>site.com</h4>
<h5><?php echo $this->benchmark->elapsed_time();?></h5>
</div><!-- /jqm-footer -->
艺术家视图(这给了我一个错误:未定义的变量“行”):
<!-- Start content.php -->
<div data-role="page">
<div data-role="header" data-id="header" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="http://www.site.com/home" class="" data-transition="fade">Home</a></li>
<li><a href="http://www.site.com/browse" class="" data-transition="fade">Browse</a></li>
<li><a href="http://www.site.com/about" class="" data-transition="fade">About</a></li>
<li><a href="javascript:void(0)"id ="toggle">▶</a></li>
</ul>
</div>
</div><!-- /jqm-header -->
<div data-role="content">
<ul id="song_list" data-role="listview" data-filter="true">
<li data-icon="false"><a data-rel="back">Back</a></li>
<?php foreach ($rows as $r) {?><li data-icon="false"><a data-transition="slide" href="http://www.site.com/browse/album/<?php echo urlencode($r['ALBUM']); ?>"><?php echo $r['ALBUM']; ?></a></li><?php };?>
</ul>
</div>
<div data-role="footer">
<h4>site.com</h4>
<h5><?php echo $this->benchmark->elapsed_time();?></h5>
</div><!-- /jqm-footer -->
这一直让我发疯。我之前有它工作,现在我不知道问题是什么。我禁用了 jQuery Mobile AJAX 加载,但它仍然给我这个错误。任何帮助,将不胜感激。谢谢。