gpt4 book ai didi

php - 如何从第三个表中获取数据?

转载 作者:行者123 更新时间:2023-12-02 05:17:03 24 4
gpt4 key购买 nike

我的问题有点复杂,我有表名 business、Items、busines_items 和 item_review。

Business has attributes business_id,business_name
Items has attributes item_id (pk), item_name,item_price,item_description
Business_Items has attributes business_items_id (pk), Business_id (fk),item_id, image
Item_review has attributes item_review_id (pk), business_items_id (fk), rating.

我想在业务 View 中显示 business_items 表中的 business_items_id 和 item_review 表中该项目的评级。我做了一些工作,通过关系我得到了 business_items_id。但是我怎样才能从 item_review 获得评级。因此,当我转到此 url localhost/project/business/products/business_id 时,此 View 会向我显示该业务的所有产品及其评级和图片。

我在 business/viewitems 中查看文件

<div class="container">

<div class="container">
<h1 class="page-title"><?php echo $model->business_name; ?>'s Products</h1> <!-- add this heading in page-tittle -->

</div>
<?php $item=$model->businessItems;

?>

<div class="row row-wrap">
<?php foreach($item as $ba)
{
// echo $ba->id;

?>

<div class="col-md-3">
<div class="thumb">
<header class="thumb-header">
<a class="hover-img" href="<?php Yii::app()->request->baseUrl?>/businessitems/itemspage/<?php echo $ba->id; ?>">
<img src="<?php Yii::app()->request->baseUrl?>/img/<?php echo $ba->image;?>" alt="Image Alternative text" title="Items" />

</a>
</header>

<div class="thumb-caption">

<h5 class="thumb-title"><a class="text-darken" href="<?php Yii::app()->request->baseUrl?>/businessitems/itemspage/<?php echo $ba->id; ?>"><?php echo $ba->items->item_name; ?></a></h5>

</div>
</div>
</div>
<?php } ?>



</div>
</div>
<div class="gap gap-small"></div>
</div>

商业模式

<?php

/**
* This is the model class for table "business".
*
* The followings are the available columns in table 'business':
* @property integer $id
* @property string $business_name
* @property string $image
* @property string $business_description
* @property string $opening_hours
* @property string $closing_hours
* @property string $days
* @property string $Holiday
* @property string $website
* @property integer $phone
*
* The followings are the available model relations:
* @property Address[] $addresses
* @property BusinessItems[] $businessItems
* @property BusinessPackage[] $businessPackages
* * @property BusinessPhotos[] $businessPhotoses
* @property ClaimBusiness[] $claimBusinesses
* @property Facilities[] $facilities
* @property ReviewBusiness[] $reviewBusinesses
* @property SubCategoryBusiness[] $subCategoryBusinesses
*/
class Business extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'business';
}

/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('business_name, business_description, opening_hours, closing_hours, days', 'required'),
array('phone', 'numerical', 'integerOnly'=>true),
array('business_name', 'length', 'max'=>60),
array('image, opening_hours, closing_hours, days, Holiday, website', 'length', 'max'=>45),
array('business_description', 'length', 'max'=>500),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, business_name, image, business_description, opening_hours, closing_hours, days, Holiday, website, phone', 'safe', 'on'=>'search'),
array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'safe' => false,'on'=>'insert,update'),

);
}

/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),
'businessItems' => array(self::HAS_MANY, 'BusinessItems', 'business_id'),
'businessPackages' => array(self::HAS_MANY, 'BusinessPackage', 'business_id'),
'businessPhotoses' => array(self::HAS_MANY, 'BusinessPhotos', 'business_id'),
'claimBusinesses' => array(self::HAS_MANY, 'ClaimBusiness', 'business_id'),
'facilities' => array(self::HAS_MANY, 'Facilities', 'business_id'),
'reviewBusinesses' => array(self::HAS_MANY, 'ReviewBusiness', 'business_id'),
'subCategoryBusinesses' => array(self::HAS_MANY, 'SubCategoryBusiness', 'business_id'),
);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'business_name' => 'Business Name',
'image' => 'Image',
'business_description' => 'Business Description',
'opening_hours' => 'Opening Hours',
'closing_hours' => 'Closing Hours',
'days' => 'Days',
'Holiday' => 'Holiday',
'website' => 'Website',
'phone' => 'Phone',
);
}

/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id);
$criteria->compare('business_name',$this->business_name,true);
$criteria->compare('image',$this->image,true);
$criteria->compare('business_description',$this->business_description,true);
$criteria->compare('opening_hours',$this->opening_hours,true);
$criteria->compare('closing_hours',$this->closing_hours,true);
$criteria->compare('days',$this->days,true);
$criteria->compare('Holiday',$this->Holiday,true);
$criteria->compare('website',$this->website,true);
$criteria->compare('phone',$this->phone);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Business the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}

最佳答案

任何对这里的答案感兴趣的人。我的业务 Controller 如下。

 public function actionViewitems($id)
{
//render main layout
$this->layout = 'main';

$rate = ItemReview::model()->findAll();
$this->render('viewitems', array(
'model' => $this->loadModel($id),
'rate' => $rate, //pass data
));
}

现在我可以在我的 viewitems View 中使用 foreach 循环访问它。

foreach ($rate as $ba) {
echo $ba->rating;
}

现在下面是我的 View 文件,我在其中获得针对特定业务和特定业务项目的特定评级。

        <div class="container">

<div class="container">
<h1 class="page-title"><?php echo $model->business_name; ?>'s Products</h1> <!-- add this heading in page-tittle -->

</div>
<?php
function name($percentage){

if($percentage==0 && $percentage<0.5)
{ ?> <div class="booking-item-rating">
<ul class="icon-list icon-group booking-item-rating-stars">
<li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=0.5 && $percentage<1) { ?>
<div class="booking-item-rating">
<ul class="icon-list icon-group booking-item-rating-stars">
<li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-half.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</li>

</ul>

</div>

<?php }
elseif ($percentage>=1 && $percentage<1.5) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=1.5 && $percentage<2) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-half.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;

</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=2 && $percentage<2.5) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=2.5 && $percentage<3) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-half.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=3 && $percentage<3.5) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<div class="rating">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=3.5 && $percentage<4) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-half.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=4 && $percentage<4.5) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">

<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-off.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage>=4.5 && $percentage<5) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-half.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
elseif ($percentage=5) { ?> <div class="booking-item-rating"><ul class="icon-list icon-group booking-item-rating-stars"><li><div class="rating_subcat">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo "star-on.png";?>">;
</div>
</li>

</ul>

</div>
<?php }
}

?>
<?php $item=$model->businessItems;

?>

<div class="row row-wrap">
<?php foreach($item as $ba)
{
$bizitems=$ba->id; //getting id of business items
$biz=$ba->business_id; //business id from model business items
$value=$model->id; //storing business id of current business
if($value==$biz){ //matching business id and business id from busienss items table
$image=$ba->image; //geting image from business items table
$item_name=$ba->items->item_name;
$sum=0;
$time=0;
?>

<div class="col-md-3">
<div class="thumb">
<header class="thumb-header">
<a class="hover-img" href="<?php Yii::app()->request->baseUrl?>/businessitems/itemspage/<?php echo $bizitems; ?>">
<img src="<?php Yii::app()->request->baseUrl?>/img/<?php echo $image;?>" alt="Image Alternative text" title="Items" />
</a>
</header>
<?php

foreach ($rate as $ab){
$ratebiz=$ab->business_items_id; //getting business items id from item review
if($ratebiz==$bizitems){ //comparing business items id with business items id in item review
$sum=$sum+$ab->rating;
$time++;
}
}?>
<div class="thumb-caption">
<ul class="icon-group text-tiny text-color">
<?php if($time!=0)
{
$percentage=$sum/$time;
} ?>
<?php name($percentage);//echo $percentage; //showing rating ?>
</ul>
<h5 class="thumb-title"><a class="text-darken" href="<?php Yii::app()->request->baseUrl?>/businessitems/itemspage/<?php echo $ba->id; ?>"><?php echo $ba->items->item_name; ?></a></h5>

</div>
</div>
</div>
<?php } } ?>

</div>
</div>
<div class="gap gap-small"></div>
</div>

关于php - 如何从第三个表中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com