change url content to another controller in yii
In my view->category->index.php, I show category names from category table. Then by $_get['id'] of cat_id, I show product names for a particular category id.
Here's the code:
$por=product::model()->findAllByAttributes(array('cat_id'=>$_GET['cat_id']));
$list = CHtml::listData($por,'pd_id','product开发者_JS百科_name');
foreach($list as $value)
echo CHtml::link(CHtml::encode($value),
array('showproduct',
'id'=>$data->pd_id));
For example, if we have in the category
table:
cat_id = 1, cat_name = "nokia"
and in the product
table:
product_id = 1, cat_id = 1, product_name = "N72"
product_id = 2, cat_id = 1, product_name = "N73"
Then I can dynamically show the two product names.
my doubt is,how to show features(from feature table, feature table has category and product id as foreign keys) of N72,by clicking N72. and how to apply chtml link and write action to perform this in same view->category->index.php.
please advice !
Create new relation in Product model:
public function relations() {
...
'features' => array(self::HAS_MANY, 'Features', 'product_id')
}
Now you can use $por->features to get all features of the product, but you have to change your code from procedural/arrays to more oop.
$products = Product::model()->findAllByAttributes(array('cat_id'=>$_GET['cat_id']));
foreach($products as $product) {
echo CHtml::link(CHtml::encode($product->product_name),
array('showproduct',
'id'=>$product->pd_id));
foreach ($product->features as $feature)
echo CHtml::link(CHtml::encode($feature->feature_name),
array('features/showfeature',
'id'=>$feature->ft_id))."\n";
}
Here i don't know you Features table structure, so i've put similar to Products column names. Also i don't know what other controller you want the link to point, so i've put 'features/showfeature' which is FeatureController and actionShowFeature as action.
精彩评论