我的Yii音乐商店应用程序中有此代码,但我不知道错误。我的想法是显示/浏览专辑,艺术家和流派的类别/标准。但它总是显示“其他条件”。我试图修复它,但我找不到错误。代码如下。希望有人能解决它。谢谢大家
<?php
class StoreController extends Controller
{
public $message;
public function actionIndex()
{
$this->message = "Hello from Store.Index()";
$this->render('index', array('content'=>$this->message));
}
public function actionBrowse()
{
if(isset($_GET["gid"])){
$gid = $_GET["gid"];
$genreCriteria = new CDbCriteria();
$genreCriteria->select = "`GenreId`, `Name`, `Description`";
$genreCriteria->condition = "genreId = " . $_GET["gid"];
$artistCriteria = new CDbCriteria();
$artistCriteria->alias = "t1";
$artistCriteria->select = "DISTINCT `t1`.`Name`, `t1`.`ArtistId`";
$artistCriteria->join = "LEFT JOIN `tbl_album` ON `tbl_album`.`ArtistId` = `t1` . `ArtistId`";
$artistCriteria->order = "`t1`.`ArtistId` ASC";
$albumCriteria = new CDbCriteria();
$albumCriteria->alias = "t2";
$albumCriteria->select = "`AlbumId`, `GenreId`, `Title`, `Price`, `AlbumArtUrl`";
$albumCriteria->condition = "`GenreId` = " . $_GET["gid"];
$albumCriteria->order = "`ArtistId` ASC";
$this->render('index', array('Albums' => Album::model()->findAll($albumCriteria),
'Artist' => Artist::model()->findAll($artistCriteria),
'Genre' => Genre::model()->findAll($artistCriteria)));
}else{
$this->message = "Hello from Store.Browse()";
$this->render('index', array('content'=>$this->message));
}
}
public function actionDetails()
{
$this->message = "Hello from Store.Details()";
$this->render('index', array('content'=>$this->message));
}
// Uncomment the following methods and override them if needed
/*
public function filters()
{
// return the filter configuration for this controller, e.g.:
return array(
'inlineFilterName',
array(
'class'=>'path.to.FilterClass',
'propertyName'=>'propertyValue',
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
*/
}
答案 0 :(得分:2)
我很确定答案非常简单,您的网页网址缺少“gid”及其价值。这是“if isset”条件属于“else”部分的唯一合理解释。
您可以进行快速确认测试。转到您的浏览器,输入 www.yoursitename.com/store/browse?gid=111 ,然后查看是否能获得所需的结果。请注意,我不知道您的确切网址及其结构,所以这只是一个示例,但“/ browse?gid = 111 ”这一部分非常重要。它简单地为$ _POST数组创建“gid”并将其值设置为“111”。
附加说明:与“isset”一起,您还可以检查“gid”值是否为空,有时候这样有用且更安全。
此外,这是我的Yii代码版本:
$genreCriteria = new CDbCriteria();
$genreCriteria->select = "GenreId, Name, Description";
$genreCriteria->condition = "genreId = " . $_GET["gid"];
$artistCriteria = new CDbCriteria();
$artistCriteria->alias = "t1";
$artistCriteria->select = "DISTINCT t1.Name, t1.ArtistId";
$artistCriteria->join = "LEFT JOIN tbl_album ON tbl_album.ArtistId = t1.ArtistId";
$artistCriteria->order = "t1.ArtistId ASC";
$albumCriteria = new CDbCriteria();
$albumCriteria->alias = "t2";
$albumCriteria->select = "AlbumId, GenreI, Title, Price, AlbumArtUrl";
$albumCriteria->condition = "GenreId = " . $_GET["gid"];
$albumCriteria->order = "ArtistId ASC";
如您所见,我只是删除了引号。这个代码没有在Yii本地测试,但查询本身 - 对我来说很好。