Yii: How to show dynamic bootstrap progress bars on view page

為{幸葍}努か 提交于 2019-12-25 18:17:55

问题


I am a yiibie. I have top 5 ngo's on my view page(top 5 ngo's for each month) and also the top 5 ngo's at the end of the year on the same page. Now i want to show the bootstrap progress bar in front of every ngo. For example if January is the month and it is showing me top 5 ngo's of that month,I want to show a progress bar in front of all those 5 ngo's and I want it for every month. And same thing for the end of the year. Like the 1st ngo progress bar will be 100%, 2nd ngo will have less then 100% and so on. Please help me in doing this, thank you. This is my code for my controller which has the view file function

           public function actionNgostats()
{

    $this->layout='main';
    $myCurrYear = date("Y");
    $userYear=UserRateReviewNgo::model()->findAll(array(
        'condition' => 'YEAR(date_created)=:year',
        'params' => array(':year'=>$myCurrYear),
        'select'=>'DISTINCT rate,ngo_id',
        'order'=>'rate DESC',
        'limit' =>  5
     ));  
    $this->render('ngostats', array("userYear"=>$userYear));
}

and this is my view file(ngostats)

    <div>
    <?php
    for($month = 1 ; $month <=date('m') ; $month++) 
    {

     $user=UserRateReviewNgo::model()->findAll(array(
            'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month',
            'params' => array(':year'=>2015, ':month'=>$month),
            'select'=>'DISTINCT rate,ngo_id',
        'order'=>'rate DESC',
        'limit' =>  5
         )); 
     foreach($user as $show) {
            $model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,));
            if (isset($model)) {

?>
<div><h4><?php echo $model->ngo_name?></h4></div>
 <div class="progress">
  <div class="progress-bar progress-bar-striped active" role="progressbar"
  aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width:40%">
    40%
  </div>
</div>

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


<h3>This is for the year's top 5 Ngo's</h3>
<div>   
    <?php // the for the year 
        foreach($userYear as $show) {
            $model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,));
            if (isset($model)) {
            ?>
    <div><h4><?php echo $model->ngo_name?></h4></div>
 <div class="progress">
  <div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar"
  aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width:40%">
    40% Complete (success)
  </div>
</div>
    <?php
            }
        }
    ?>
</div>

回答1:


This is for the month i hope you are able to adapt for the year

Is simple at begin the var $val is set to 100 and this value is assigned where it is needed then is decremented by 20 and the cycle is repeated

 <div>
    <?php
    for($month = 1 ; $month <=date('m') ; $month++) 
    {

        $dateObj   = DateTime::createFromFormat('!m', $month);
        $monthName = $dateObj->format('F'); 
        echo "<h3> " . $monthName . "</h3>"; 

        $user=UserRateReviewNgo::model()->findAll(array(
            'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month',
            'params' => array(':year'=>2015, ':month'=>$month),
            'select'=>'DISTINCT rate,ngo_id',
        'order'=>'rate DESC',
        'limit' =>  5
         )); 
        $val = 100;
        foreach($user as $show) {

            $model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,));
            if (isset($model)) {
                echo "<div><h4>" . $model->ngo_name ."</h4></div>
                    <div class='progress'>
                    <div class='progress-bar progress-bar-striped active' role='progressbar'
                    aria-valuenow='" . $val ."' aria-valuemin='0' aria-valuemax='100' style='width: ". $val ."%;'>" .  $val .
                    "</div>
                    </div>";
                    $val = $val -20;
    }}}
    ?>
</div> 


来源:https://stackoverflow.com/questions/34462524/yii-how-to-show-dynamic-bootstrap-progress-bars-on-view-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!