How to display the value from another table using the primary key

advertisements

I have implemented my project in Yii. I displaying search results in view part.

I wrote a search query for fetching the results from one table which is Recipe. In this table name, course_id, cuisinename,type, colorie_count respectively, course_id,cuisinename, type are columns respectively.

In my controller i write code like this:

 $result="SELECT * FROM recipe WHERE name LIKE '%$name%' AND `cuisinename` LIKE '$cuisine1%' AND course_id  LIKE '%$course1%' AND `type` LIKE '%$type1%' AND `calorie_count` LIKE '%$calorie1%' ORDER BY recipe_id DESC LIMIT 15";

values are getting. if i give condition based to display the search result text. not displaying all name. but those are displaying based on names.

I added below my view part condition and code:

$query=  Course::model()->find("course_id=$as1");
$course2=$query->course_name;

$query1= Cuisine::model()->find("id=$as4");
$cuisine2=$query1->cuisinename;

$query3= RecipeType::model()->find("id=$as3");
$type2=$query3->recipeType_name;

<?php echo '<p align="center"> Showing results for&nbsp:&nbsp'.'Name'.$getval.',&nbsp'.'Course-'.$course2.',&nbsp'.'Cuisine-'.$cuisine2.',&nbsp'.'Type-'.$type2;',&nbsp';'</p>';
echo ',&nbsp'.'Calories-'.$calorie;
?>


You need to create relations between tables look there. For Recipe model it should be

public function relations()
{
    return array(
        'cuisine'=>array(self::BELONGS_TO, 'Cuisine', 'cuisine_id'),
        'type'=>array(self::BELONGS_TO, 'RecipeType', 'type_id'),
    );
}

Then you can get values as $model->cuisine->name. If you don't understand creating relations, generate models (it tables must be correct foreign keys) with gii.