yii2 setting max id in controller action as default - php

I have a view which list multiple photo galleries.
so from the menu when I try to access the gallery view the action is
site/gallery
Now in my controller action I am trying like this to show the latest photo-gallery added as first.
This is my code in controller:
public function actionGallery($id = null)
{
//$this->layout = "#app/themes/frontend/main";
$activities = Activities::find()
->where(['status' => 1])
->orderBy(['id' => SORT_DESC])
->all();
$model = Activities::find()->where(['id' => $id])->one();
if ($id == null) {
$gallery = Gallery::find()
->where(
[
'status' => 'active',
'activity_name' => max(['id'])
]
)->all();
} else {
$gallery = Gallery::find()
->where(
['status' => 'active', 'activity_name' => $id]
)->all();
}
return $this->render(
'gallery',
[
'gallery' => $gallery,
'activities' => $activities,
'model' => $model
]
);
}
I also tried like:
$gallery = Gallery::find()->where(
[
'status' => 'active',
'activity_name' => max(['activity_name'])
]
)->all();
the code for view:
<?php
$this->title = 'gallery';
?>
<div class="container">
<div class="col-lg-2 col-md-2 col-sm-12 col-xs-12" style='background-color:#fafafa'>
<h3>Activities</h3>
<?php
foreach ($activities as $activity) {
?>
<!-- <a href="<?php // Yii::getAlias('#web');?>/site/gallery?activity_name=<?php //$Categories['id'];?>"> -->
<a href="<?php Yii::getAlias('#web');?>/site/gallery/?id=<?php echo $activity['id']; ?>">
<br><h5 class="text-center"><?php echo $activity['activity_name']; ?></h5>
</a>
<?php
}
?>
</div>
<div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
<header class="section-header">
<div>
<h3 class="text-center">
<?php echo $model['activity_name'] ?>
</h3>
<h5 class="text-center">
<?php echo $model['activity_description'] ?>
</h5>
<br>
</div>
</header>
<div id="grid-container" class="cbp-l-grid-projects">
<ul>
<?php foreach ($gallery as $galleries) {?>
<div class="cbp-item" style="width:320px;">
<a href="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image']; ?>" data-lightbox="portfolio" data-title="<?php echo $galleries['title']; ?>" title="Preview" class="cbp-lightbox">
<picture>
<source srcset ="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo basename($galleries['image_thumb'], '.jpg') . '.webp'; ?>" type="image/webp">
<source srcset ="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image_thumb']; ?>" type="image/jpeg">
<img src="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image_thumb']; ?>" style ="width:100%;" class="img-fluid" alt="">
</picture>
</a>
</div>
<?php
}
?>
</ul>
</div>
</div>
</div>

I think I have resolved it using the below code.
public function actionGallery($id=null) {
//$this->layout = "#app/themes/frontend/main";
$activities = Activities::find()->where(['status' => 1])->orderBy(['id'=>SORT_DESC])->all();
$model = Activities::find()->where(['id' => $id])->one();
$latestActivity = Activities::find()->max('id');
if($id==null){
$gallery = Gallery::find()->where(['status' => 'active','activity_name'=>$latestActivity])->all();
}else{
$gallery = Gallery::find()->where(['status' => 'active','activity_name'=>$id])->all();
}
return $this->render('gallery', ['gallery' => $gallery,'activities'=>$activities,'model'=>$model]);
}

Related

How to show data from database in dscending order in codeigniter?

Normally when I get the data from my database I there are arranged in the order of their id.However, I have created a column called 'sales' which is also an integer
and I would like my data to be arranged in ascending order according to the sales.Here is my code:
My Model
public function get_bestseller($id = FALSE) {
if ($id === FALSE)
{
$query = $this->db->get('submit');
return $query->result_array();
}
$query = $this->db->get_where('submit', array('id' => $id));
$this->db->order_by('sales', 'DESC');
return $query->result_array();
}
My View
<div class="tab-pane fade" id="faq-cat-2">
<div class="panel-group" id="accordion-cat-2">
<?php foreach ($bestseller as $bestseller_item): ?>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img class="lazy" src="<?php echo base_url() ?>Images/last.gif"
data-original="<?php echo base_url() ?>uploads/<?php echo $bestseller_item['imgname']; ?>"
style="width: 350px; height: 150px">
<div class="caption">
<?php if ($bestseller_item['Price'] == 0): ?>
<h4 class="pull-right"><p class="text-success">Free</p></h4>
<?php else: ?>
<h4 class="pull-right">$<?php echo $bestseller_item['Price']; ?></h4>
<?php endif; ?>
<h4>
<a href="<?php echo site_url('submit/view/' . $bestseller_item['id']); ?>"
target="_blank"><?php echo $bestseller_item['Title']; ?></a>
</h4>
<p><?php echo $bestseller_item['textarea']; ?></p>
</div>
<p class="text-right"><b
class="author">By: <?php echo $bestseller_item['Author']; ?></b>
</p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
My Controller
public function index()
{
$data['bestseller'] = $this->Submit_Database->get_bestseller();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('templates/footer');
}
$query = $this->db->order_by('sales', 'DESC')->get_where('submit', array('id' => $id));
You are adding the order by clause after the query has already ran.

subcategory images in category page in opencart 2.0.3.1

I am not able to see any changes after editing opencart 2.0.3.1, I am not getting the images and also edited the files from category.php (catlog>controller>product) and category.tpl(catlog>view>theme>*>template>product) and replaced
$data['categories'][] = array(
and
<?php if ($categories) { ?>
with
if ($result['image']) {
$image = $this->model_tool_image- >resize($result['image'], 100, 100);
} else {
$image = $this->model_tool_image- >resize('placeholder.png', 100, 100);
}
$data['categories'][] = array(
'name' => $result['name'] . ($this->config- >get('config_product_count') ? ' (' . $this->model_catalog_product- >getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url),
'thumb' => $image
);
and
<?php if ($categories) { ?>
<div class="col-sm-12">
<div>
<h3><?php echo $text_refine; ?></h3>
</div>
<div style="text-align: center;">
<div class="row">
<?php foreach ($categories as $category) { ?>
<div class="col-sm-2 col-xs-6" style="margin-bottom: 20px;">
<img src="<?php echo $category['thumb']; ?>" alt="<?php echo $category['name']; ?>" />
<?php echo $category['name']; ?>
</div>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
respectively but not able to get any thing! Help highly appreciated

How to get data from a third table?

My question is bit complex, I have tables name business, Items, busines_items and 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.
I want to show in a view of business, business_items_id from business_items table and rating of that item from item_review table.I have done some work, Through relationship i get the business_items_id. But How can i get the rating from item_review. so that when i go to this url localhost/project/business/products/business_id this view shows me all the products of that business along with their ratings and picture.
My view file in 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>
Model of business
<?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);
}
}
Any one who is interested in the answer here it goes.
My business controller is given below.
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
));
}
Now I can access it by using foreach loop in my viewitems view.
foreach ($rate as $ba) {
echo $ba->rating;
}
Now given below is my view file, where i am getting specific rating against specific business and specific business items.
<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>

Pagination in yii without in gridview or list view

How to do pagination in Yii without any grid view and listview
I was stack in their for two days
these is my controller
public function actionIndex()
{
$criteria = new CDbCriteria;
// $criteria ->order = 'date';
$pages = new CPagination(OfferEvents::model()->count());
$pages ->pageSize = 2;
$pages -> applyLimit($criteria);
$post= OfferEvents::model()->findAll($criteria);
$this -> render('index', array('post' => $post, 'pages' => $pages));
}
and my view
<?php foreach($models as $model): ?>
<?php for($x = 0; $x < $arrlength; $x++) { ?>
<?php if($catiddet == 1){?>
<div class="col-sm-4">
<div class="list-box fadeInDown ">
<div class="img-box-1">
<img src="<?php echo $offerListArray[$x]['offerimg']; ?>" style="width:360px;height:202px;" />
<a href="#" class="view-btn" d='modal-launcher' data-toggle="modal"
data-target="#detailsOffeEve" onClick="showdetails(<?php echo $offerListArray[$x]['id']; ?>)">View Event</a>
</div>
<div class="text-box">
<?php
$date2=$offerListArray[$x]['enddate'] ;
$diff = (strtotime($date2)- strtotime($now))/24/3600;
?>
<div class="round-box clear"><span><h1><?php echo $diff ?><small>Days</small></h1></span></div>
<p style="height:19px;overflow: hidden;"> <b ><?php echo $offerListArray[$x]['offertitle']; ?> </b></p>
<p style="height: 80px;overflow: hidden;"><?php echo $offerListArray[$x]['description']; ?> </p>
</div>
</div>
</div>
<?php } else{?>
<div class="col-sm-4" >
<div class="list-box">
<div class="img-box-1">
<?php $filepathnw=$offerListArray[$x]['offerimg'];
//if (file_exists($filepathnw)) { $filepathSrc=Yii::app()->baseUrl.'/'.$filepathnw; } else
//{ $filepathSrc='http://www.childnet.com/pimages/1351720/thumbnail4.jpg'; }
?>
<img src="<?php echo $filepathnw; ?>" />
<a href="#" class="view-btn" d='modal-launcher' data-toggle="modal"
data-target="#detailsOffeEve" onClick="showdetails(<?php echo $offerListArray[$x]['id']; ?>)">View Offer</a>
</div>
<div class="text-box" style="padding:2px !important;">
<?php
$date2=$offerListArray[$x]['enddate'] ;
$diff = (strtotime($date2)- strtotime($now))/24/3600;
?>
<span><h1><?php echo $diff ?><small>Days</small></h1></span>
<span><h1><?php echo $offerListArray[$x]['discountper']; ?>%<small>DISCOUNT</small></h1></span>
<span><h1>$<?php echo $offerListArray[$x]['discountperamt']; ?><small>You Save</small></h1></span>
<div class="text-box" style="padding:15px;">
<p style="height:19px;overflow: hidden;">
<b ><?php echo $offerListArray[$x]['offertitle']; ?> </b></p>
<p style="height: 80px;overflow: hidden;"><?php echo $offerListArray[$x]['description']; ?> </p>
</div> </div>
</div>
</div>
<?php } ?>
<?php } ?>
<?php endforeach; ?>
// display pagination
<?php $this->widget('CLinkPager', array(
'pages' => $pages,
)) ?>
out put comes fine..but the data repeats...
select * FROM offer_events WHERE enddate >= '$now' AND (title like '%$locationdet%' OR description like '%$locationdet%') AND type = '$catiddet' ORDER BY id DESC LIMIT 6 ");
$datalat = $dbCommand->queryAll(); $numlatData=count($datalat);
// echo "<pre>"; print_r($datalat); echo "</pre>";
$latlongdats='';
if($numlatData!=0){
foreach($datalat as $expertn)
{
$ids=$expertn['id'];
$offertitle=$expertn['title'];
if($expertn['image']!=""){ $imgUrl=$expertn['image']; } else { $imgUrl='image-not-available.png'; }
$infowinimgpath='theme/images/OfferEvents/thumb/'.$imgUrl;
if (file_exists($infowinimgpath)) { $infowinimgpathSrc=Yii::app()->baseUrl.'/'.$infowinimgpath; } else
{ $infowinimgpathSrc=Yii::app()->baseUrl.'/theme/images/OfferEvents/thumb/image-not-available.png'; }
$offerimg=$infowinimgpathSrc;
$latinow=$expertn['latitude'];
$longinow=$expertn['longitude'];
$latlongdats.="['".$ids."', ".$latinow.",".$longinow.",'".$offertitle."','".$offerimg."','".$expertn['startdate']."'],";
$offertList = array(
"id" => $ids,
"offertitle"=> $offertitle,
"offerimg" => $offerimg,
"description" => $expertn['description'],
"startdate" => $expertn['startdate'],
"enddate" => $expertn['enddate'],
"discountper" => $expertn['discountper'],
"discountperamt" => $expertn['discountperamt']
);
array_push($offerListArray,$offertList);
}
$zoomlevel=3;
$latlongdatsfinal=rtrim($latlongdats, ",");
} else { $latlongdatsfinal="['','',''],"; $ErrorMsgmap="No Result Found."; }
}
and could you please explain me how this happend???
You can use CActiveDataProvider to perform your pagination needs
public function actionIndex()
{
$criteria = new CDbCriteria;
// $criteria ->order = 'date';
$post= new CActiveDataProvider('OfferEvents', array(
'criteria' => $criteria,
'pagination'=>array('pageSize' => 2),
));
$this -> render('index', array('post' => $post));
}
In your view, instead of using foreach loop, use ListView / GridView and pass the CActiveDataProvider instance ($post) in your case
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$post,
'itemView'=>'_post', // refers to the partial view named '_post'
));
the "_post.php" subview file would contain
$data->property // whatever properties you have set for 'OfferEvents'

Yii form not validating

This is my code in actionCheckout():
public function actionCheckout() {
$proInfo = Yii::app()->session['cart'];
if (Yii::app()->user->isGuest) {
$model = new Payment;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Payment'])) {
$model->attributes = $_POST['Payment'];
$model->thoigian = date('Y-m-d H:i:s');
if ($model->save()) {
foreach ($proInfo as $key => $value) {
$order = new Order;
$order->id_sp = $key;
$order->soluong = $value;
$product = Product::model()->findByPK($key);
if (empty($product->khuyenmai)) {
$price = $product->gia_sp;
} else {
$price = ceil($product->gia_sp * (100 - $product->khuyenmai) / 100 / 1000) * 1000;
}
$order->thanhtien = $price * $value;
$order->id_payment = $model->id_hoadon;
$order->save();
}
unset(Yii::app()->session['cart']);
$this->redirect(Yii::app()->request->baseUrl . '/cart/success/' . $model->id_hoadon);
}
}
$this->render('checkout', array(
'cart' => $proInfo,
'model' => $model,
));
} else {
$model = new Payment;
$user = User::model()->findByPk(Yii::app()->user->id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Payment'])) {
$model->attributes = $_POST['Payment'];
$model->id_user = Yii::app()->user->id;
$model->hoten = $user->hoten;
$model->email = $user->email;
$model->dienthoai = $user->dienthoai;
$model->diachi = $user->dia_chi;
$model->thoigian = date('Y-m-d H:i:s');
$model->xuly = "Chưa xử lý";
if ($model->save()) {
foreach ($proInfo as $key => $value) {
$order = new Order;
$order->id_sp = $key;
$order->soluong = $value;
$product = Product::model()->findByPK($key);
if (empty($product->khuyenmai)) {
$price = $product->gia_sp;
} else {
$price = ceil($product->gia_sp * (100 - $product->khuyenmai) / 100 / 1000) * 1000;
}
$order->thanhtien = $price * $value;
$order->id_payment = $model->id_hoadon;
$order->save();
}
unset(Yii::app()->session['cart']);
$this->redirect(Yii::app()->request->baseUrl . '/cart/success/' . $model->id_hoadon);
}
}
$this->render('checkout', array(
'user' => $user,
'cart' => $proInfo,
'model' => $model,
));
}
}
And this is my "checkout.php" file in views:
<link rel="stylesheet" type="text/css" href="<?= Yii::app()->request->baseUrl ?>/css/cart.css"/>
<?php
$this->breadcrumbs = array(
'Giỏ hàng' => array('cart/index'),
'Thanh toán'
);
$this->pageTitle = "Thanh Toán";
$items = 0;
$total = 0;
$i = 0;
?>
<h1>Thanh toán</h1>
<?php
if (empty($cart)) {
echo "<p>Bạn chưa có sản phẩm nào trong giỏ hàng</p>";
} else {
$form = $this->beginWidget('CActiveForm', array(
'id' => 'payment-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation' => false,
));
?>
<div class="payment">
<?php
if (!Yii::app()->user->isGuest) {
?>
<div class="paymentCustomer">
<h3>Thông tin thanh toán</h3>
<div style="padding: 10px; clear:both; float:left">Họ tên:<?= Yii::app()->user->name ?></div>
<div style="padding: 10px; clear:both; float:left">Email:<?= $user->email ?></div>
<div style="padding: 10px; clear:both; float:left">Địa chỉ:<?= $user->dia_chi ?></div>
<div style="padding: 10px; clear:both; float:left">Điện thoại:<?= $user->dienthoai ?></div>
</div>
<?php
} else {
?>
<div class="paymentCustomer">
<h3>Thông tin thanh toán</h3>
<div class="inputUser"><div>Họ tên <span>*</span></div><?php echo $form->textField($model, 'hoten', array('class' => "inputText loginText")); ?></div>
<?php echo $form->error($model, 'hoten'); ?>
<div class="inputUser"><div>Email <span>*</span></div><?php echo $form->textField($model, 'email', array('class' => "inputText loginText")); ?></div>
<?php echo $form->error($model, 'email'); ?>
<div class="inputUser"><div>Địa chỉ <span>*</span></div><?php echo $form->textField($model, 'diachi', array('class' => "inputText loginText")); ?></div>
<?php echo $form->error($model, 'diachi'); ?>
<div class="inputUser"><div>Điện thoại <span>*</span></div><?php echo $form->textField($model, 'dienthoai', array('class' => "inputText loginText")); ?></div>
<?php echo $form->error($model, 'dienthoai'); ?>
</div>
<?php
}
?>
<div class="paymentCustomer">
<h3>Hình thức thanh toán</h3>
<p class="paymentYour">
<?php
echo $form->radioButtonList($model, 'phuongthuc', array(
'Tại Cửa hàng' => 'Thanh toán tại cửa hàng',
'Khi Nhận hàng' => 'Thanh toán khi nhận được hàng',
'Tài khoản Ngân hàng' => 'Thanh toán qua tài khoản Ngân hàng'
), array("class" => "myCheck"));
?>
<?php echo $form->error($model, 'phuongthuc'); ?>
</p>
<h3 style="margin: 50px 0 5px 0;">Mã khuyến mãi</h3>
<?php echo $form->textField($model, 'coupon', array('class' => "inputText loginText")); ?>
<?php echo $form->error($model, 'coupon'); ?>
</div>
</div>
<div class="cartInfo">
<div class="headCart">
<div class="cartField1">Mã sản phẩm</div>
<div class="cartField2">Tên sản phẩm</div>
<div class="cartField3">Đơn giá</div>
<div class="cartField3">Số lượng</div>
<div class="cartField3">Thành tiền</div>
</div>
<?php
foreach ($cart as $key => $value) {
$product = Product::model()->findByPK($key);
if (empty($product->khuyenmai)) {
$price = $product->gia_sp;
} else {
$price = ceil($product->gia_sp * (100 - $product->khuyenmai) / 100 / 1000) * 1000;
}
?>
<div class="bodyCart">
<div class="cartField1" data-label="Mã sản phẩm"><?= $product->ma_sp ?></div>
<div class="cartField2" data-label="Tên sản phẩm"><span><?= $product->ten_sp ?></span></div>
<div class="cartField3" data-label="Đơn giá"><?= $price . "đ" ?></div>
<div class="cartField3" data-label="Số lượng"><?= $value ?></div>
<div class="cartField3" data-label="Thành tiền"><?= $price * $value . "đ" ?></div>
</div>
<?php
$items +=$value;
$total +=$price * $value;
}
?>
<div class="footCart">
<div class="cartField1">Tổng cộng</div>
<div class="cartField2"></div>
<div class="cartField3"><?= $items ?></div>
<div class="cartField3"><?= $total . "đ" ?></div>
</div>
</div>
<div>
<a href="<?= Yii::app()->request->urlReferrer ?>">
<input type="button" class="cartLeftButton button" value="Quay trở lại"/>
</a>
<?php echo CHtml::submitButton('Xác nhận và gửi đơn hàng', array("class" => "cartRightButton button")); ?>
</div>
<?php
$this->endWidget();
}
?>
But when submit that form it not direct to success page. I have try to insert die("abc") to each line in Controller and find out that it not run from the line
if($model->save()){....
How can I fix that now?

Categories