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

时间:2015-11-12 01:01:16

标签: php yii view rating

我的问题有点复杂,我有表名char sentence[30]; int i=0; printf("Enter phrase: \n"); scanf("%c", sentence); while(sentence[i]!='.' && sentence[i]!='!') { i++; scanf("%c",&sentence[i]); } printf("%d\n", i); return i;

business, Items, busines_items and item_review.

我希望在业务视图中显示,business_items表中的business_items_id以及item_review表中该项目的评级。我已经做了一些工作,通过关系,我得到了business_items_id。但我如何从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. 时,此视图会向我显示该商家的所有产品及其评分和图片。

我在business / viewitems中的视图文件

localhost/project/business/products/business_id

业务模式

<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>

1 个答案:

答案 0 :(得分:-1)

任何对此答案感兴趣的人都会这样做。 我的业务控制器如下所示。

 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视图中使用foreach循环来访问它。

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

下面给出的是我的视图文件,我在其中获得针对特定业务和特定业务项的特定评级。

        <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>