0

I have a table name Business having attributes business_name,business_id,business_description,image. Now i want to display the 4 recently added business in my landing page? How can i achieve this goal

this is my code in main.php, i want it dynamic.

<div class="container">
            <div class="gap"></div>
            <h2 class="text-center">Recently Added</h2>
            <div class="gap">
                <div class="row row-wrap">
                    <div class="col-md-3">
                        <div class="thumb">
                            <header class="thumb-header">
                                <a class="hover-img curved" href="nandos.php">
                                    <img src="img/nandos.jpg" alt="Image Alternative text" title="Nando's" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
                                </a>
                            </header>
                            <div class="thumb-caption">
                                <h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/nandos.php">Nando's</h4>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="thumb">
                            <header class="thumb-header">
                                <a class="hover-img curved" href="rendezvous.php">
                                    <img src="img/rendezvous.jpg" alt="Image Alternative text" title="Rendezvous" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
                                </a>
                            </header>
                            <div class="thumb-caption">
                                <h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/rendezvous.php">Rendezvous<a></h4>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="thumb">
                            <header class="thumb-header">
                                <a class="hover-img curved" href="roasters.php">
                                    <img src="img/roasters.jpg" alt="Image Alternative text" title="Roasters" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
                                </a>
                            </header>
                            <div class="thumb-caption">
                                <h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/roasters.php">Roasters<a></h4>

                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="thumb">
                            <header class="thumb-header">
                                <a class="hover-img curved" href="themonal.php">
                                    <img src="img/themonal.jpg" alt="Image Alternative text" title="The Monal Resataurant" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
                                </a>
                            </header>
                            <div class="thumb-caption">
                                <h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/themonal.php">The Monal Restaurant<a></h4>

                            </div>
                        </div>
                    </div>
                </div>
            </div>


        </div>

the recently added, section is where i want the recently added business.

Usman Iqbal
  • 2,379
  • 5
  • 26
  • 50

3 Answers3

0

if business_id is primary or autoincrement you can use this:

$bussiness = Bussiness::model()->findAll(array('order'=>'business_id ', 'limit'=>'1'));

for select 4 rows:

 $bussiness = Bussiness::model()->findAll(array('order'=>'business_id ', 'limit'=>'4'));
0

First of all you have to generate model for your table using gii. Read more here

Once you have model you are able to pull all records from DB by using

Business::model()->findAll()
Business being name of the model generated by gii.

Below code shows how you would display all business_names.
$businesses = Business::model()->findAll();
  foreach($businesses as $business)
   echo $business->business_name;

Hopefully you can figure out how your code should look like :)

xReprisal
  • 810
  • 8
  • 23
-1
<?php 
$criteria = new CDbCriteria(array('order'=>'id DESC','limit'=>4));
$model = Business::model()->findAll($criteria);
$names=array();
$image=array();
foreach ($model as $list) {

//    echo ($list->business_name);
//                echo ($list->image);
          $names[] = $list->business_name;
          $image[] = $list->image;
                }  
                $a=0;
                $b=1;
                $v=2;
                $d=3;
for($i=0;$i<1;$i++){
    $one=$names[$a];
     $two=$names[$b];
     $three=$names[$v];
     $four=$names[$d];
      $oneimage=$image[$a];
     $twoimage=$image[$b];
     $threeimage=$image[$v];
     $fourimage=$image[$d];

}
 echo $one;
    echo $two;
    echo $three;
   echo $four;
    echo $oneimage;
    echo $twoimage;
    echo $threeimage;
   echo $fourimage;
?>

This is my index.php getting data from business model, like an array and store each value in a variable then where ever i want to show the variable value i just echo it, here it is..

<div class="container">
<div class="gap"></div>
<h2 class="text-center">Recently Added</h2>
<div class="gap">
<div class="row row-wrap">
<div class="col-md-3">
<div class="thumb">
<header class="thumb-header">
<a class="hover-img curved" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $oneid=$id[$a]?>">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $oneimage;?>" alt="Image Alternative text" title="recently added" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
</a>
</header>
<div class="thumb-caption">
<h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $oneid=$id[$a]?>"><?php echo $one;?></h4>
</div>
</div>
</div>
<div class="col-md-3">
<div class="thumb">
<header class="thumb-header">
<a class="hover-img curved" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $twoid=$id[$b]?>">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $twoimage;?>" alt="Image Alternative text" title="recently added" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
</a>
</header>
<div class="thumb-caption">
<h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $twoid=$id[$b]?>"><?php echo $two;?><a></h4>
</div>
</div>
</div>
<div class="col-md-3">
<div class="thumb">
<header class="thumb-header">
<a class="hover-img curved" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $threeid=$id[$c]?>">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $threeimage;?>" alt="Image Alternative text" title="Roasters" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
</a>
</header>
<div class="thumb-caption">
<h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $threeid=$id[$c]?>"><?php echo $three;?><a></h4>

</div>
</div>
</div>
<div class="col-md-3">
<div class="thumb">
<header class="thumb-header">
<a class="hover-img curved" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $fourid=$id[$d]?>">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $fourimage;?>" alt="Image Alternative text" title="The Monal Resataurant" /><i class="fa fa-plus box-icon-white box-icon-border hover-icon-top-right round"></i>
</a>
</header>
<div class="thumb-caption">
<h4 class="thumb-title"><a class="text-darken" href="<?php echo Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $fourid=$id[$d]?>"><?php echo $four;?><a></h4>

</div>
</div>
</div>
</div>
</div>


</div>  
Usman Iqbal
  • 2,379
  • 5
  • 26
  • 50