How to get data from a third table? - php

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>

Related

Mysqli_query view the last image for all user

I have problem with mysqli_query. When last user upload image, all user have last image.
This code that I have write:
<?php
$query = mysqli_query($soci->con, "SELECT soci.*, (SELECT full_path FROM soci_media WHERE id = soci.immagine_profilo) AS immagine_profilo_file FROM soci");
while($socio = mysqli_fetch_assoc($query)) {
?>
<div class="col-sm-12 col-md-6 col-lg-4 single_socio pb-3">
<?php if($soci->user_data['immagine_profilo_file'] == '') { ?>
<img src="<?php echo get_site_url(); ?>/wp-content/themes/test/images/user.png" alt="" class="default_image img-fluid" />
<?php } else { ?>
<img src="<?php echo get_site_url(); ?>/uploads<?php echo $soci->user_data['immagine_profilo_file']; ?>" alt="" class="default_image img-fluid" />
<?php } ?>
<h2><?php echo $socio['nome']; ?> <?php echo $socio['cognome']; ?></h2>
</div>
<?php
}
?>
Screenshot table database and result
Instead of using the select clause to do another query (it will not work). You should join the tables. I used a left join to avoid not having values if there is no profile image.
SELECT soci.*,soci_media.full_path AS immagine_profilo_file
FROM soci
LEFT JOIN soci_media ON soci.immagine_profilo = soci_media.id
You need to replace your query like this:
$query = mysqli_query($soci->con, "SELECT soci.*,soci_media.full_path AS immagine_profilo_file FROM soci LEFT JOIN soci_media ON soci.immagine_profilo = soci_media.id");
In addition you need to assign the current variables:
while($socio = mysqli_fetch_assoc($query)) {
?>
<div class="col-sm-12 col-md-6 col-lg-4 single_socio pb-3">
<?php if($socio['immagine_profilo_file'] == '') { ?>
<img src="<?php echo get_site_url(); ?>/wp-content/themes/test/images/user.png" alt="" class="default_image img-fluid" />
<?php } else { ?>
<img src="<?php echo get_site_url(); ?>/uploads<?php echo $socio['immagine_profilo_file']; ?>" alt="" class="default_image img-fluid" />
<?php } ?>
<h2><?php echo $socio['nome']; ?> <?php echo $socio['cognome']; ?></h2>
</div>
<?php
}

PHP: Foreach with "if-else" statement

I'm doing some changes on an online store that is running OpenCart 2.2.
When browsing below the categories on the left side, there are 2 carousels that show different promotions. I want to modify it that before the second carousel there is a description text. To do that I should edit the template file. And here is where I get lost..
This is the code in the template file:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
From I see in the browser inspector and in my case, this code generates 2 banners with the ids - "banner0" and "banner1". The description text should go at the top of the code (right before ). If I use a simple paragraph, it gets displayed twice - above each banner.. How should I change it so that it will display the paragraph only above the second banner (id - banner1)?
I was thinking about an if-else statement, but I'm not sure if that will work... Could anyone help out a bit? My knowledge in PHP isn't much... :S
Thanks in advance!
Best regards,
Tsvetko Krastev
Then you need to know that this is the second time round the foreach loop. So change the foreach to include the index like this, and add a test inside the loop for $i == 1
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php //foreach ($banners as $banner) {
foreach ($banners as $i => $banner) {
?>
<div class="item">
<?php if ($banner['link']) {
if ( $i == 1 ) {
echo 'YOUR HTML CONTAINING SOME TEXT HERE';
}
?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
If I get the code right, $module is 0/1 (if id's get values banner0 and banner1), then this should work:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<?php if ($module == "1") { ?>
<p>Your description</p>
<?php } ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
Thank you so very much guys!!! Tried both versions and they throw a error, but looking more carefully into the code and both your solutions, I came up with this:
<div id="desc<?php echo $module; ?>">
<?php if ($module == 1) { ?>
<p>Description</p>
<?php } ?>
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
</div>
Thank you very much again for the quick responses and the help! :) :3
Best regards,
Tsvetko Krastev

echo active image alt text in <p>

I'm trying to figure out some php code.
I have a slider inside a fancybox popup.
What i need to do is then i click on image it gets me its alt text in "image-product-name" P class.
<div class="modal-body">
<button class="close">×</button>
<div class="UI-IMAGE image">
<img src="<?php echo Mage::helper('core/image')->init($_product, 'image', 'catalog/product')->resize(640, 400) ?>" width="640px" height="400px"
alt="<?php echo $this->htmlEscape($this->getImageLabel()) ?>"
title="<?php echo $this->htmlEscape($this->getImageLabel()) ?>">
</div>
<p class="image-product-name"><?php echo $_product->getName() ?></p>
<?php if (count($this->getGalleryImages()) > 0): ?>
<div class="previews UI-PREVIEWS">
<ul>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a href="#"
title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"
data-image="<?php echo $this->helper('core/image')->init($this->getProduct(), 'thumbnail', 'catalog/product', $_image->getFile())->resize(640, 400); ?>">
<img src="<?php echo $this->helper('core/image')->init($this->getProduct(), 'thumbnail', 'catalog/product', $_image->getFile())->resize(78, 78); ?>" width="78px" height="78px"
alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>
</a>
</li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
</div>
Right now its just echoes an image name.
What should i change in order to make it echo active image alt text?
You need javascript for this. In the next example I've used jQuery:
$('.imgClass').click(function(){ //When user click on your img
var altImg = $(this).attr('alt'); //Save the alt attribute into a variable
$('p.image-product-name').html(altImg); //Place the variable between the p tags;
});
without js just change the variable from:
<p class="image-product-name"><?php echo $_product->getName() ?></p>
to
<p class="image-product-name"><?php echo $this->htmlEscape($_image->getLabel()) ?></p>
pretty straighforward

hiding a slider image in php

I am creating a property website from a database with PHP. On the description of each property listed has a slider of up to 10 images/slides. However, if there are only 6 images/slides for example; i would like to hide the remaining 6 images/slides from showing.
Here is what i have so far;
$sql = DB::getInstance()->query('SELECT * FROM listings WHERE id =' . Input::get('id'));
foreach($sql->results() as $row) {
<div id="property-slider">
<section class="slider">
<div class="flexslider">
<ul class="slides">
<li>
<figure>
<img src="admin/<?php echo $row->img_set_1 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_2 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_3 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_5 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_6 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_7 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_8 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_9 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
<li>
<figure>
<img src="admin/<?php echo $row->img_set_10 ?>" alt="<?php echo $row->alt ?>">
</figure>
</li>
</ul>
</div>
</section>
</div>
}
I am trying to get a result simular to this, but think repeating this 10 time etc etc, just think there is a better way
<?php
if(empty($row->img_set_1)) {
echo "";
} else {
echo "<li>";
echo "<figure>";
echo "<a href='#'><img src='{admin/$row->img_set_1}'></a>";
echo "</figure>";
echo "</li>";
}
?>
this is the function for the query
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
THIS IS AN UPDATE OF THE WORKING SLIDER---NOT IDEAL BUT IT WORKS
<?php
if(empty($row->img_set_1)) {
echo "";
} else {
echo "<li>";
echo "<figure>";
echo "<a href='#'><img src='admin/{$row->img_set_1}'></a>";
echo "</figure>";
echo "</li>";
}
if(empty($row->img_set_2)) {
echo "";
} else {
echo "<li>";
echo "<figure>";
echo "<a href='#'><img src='admin/{$row->img_set_2}'></a>";
echo "</figure>";
echo "</li>";
}
//This repeats etc etc
?>
You could do something like this:
$x = 1;
while($x<=10) {
if(!empty($row["img_set_$x"])) {
echo "<li>";
echo "<figure>";
echo "<a href='#'><img src=".'{admin/'.$row["img_set_$x"].'}'."</a>";
echo "</figure>";
echo "</li>";
}
$x++;
}

Change image without reloading page

I am trying to figure out how to change the photo without the page refreshing. I have seen some of the example but just cannot figure out how to implement it in to my working page.
This is what I have right now:
<div id="propertyDetailsImage">
<img class="image photo" src="<?php echo $property->photos->photo[$mainPhoto - 1]->url; ?>" width="<?php echo $property->mainPhotoWidth * 0.77 ?>" height="<?php echo $property->mainPhotoHeight * 0.77 ?>" alt="<?php echo $property->address->full; ?>"/>
</div>
<div class="photoPosition">
<?php
$previousPhoto = $mainPhoto - 1;
if($previousPhoto == 0) {
$previousPhoto = $property->totalPhotos;
}
$nextPhoto = $mainPhoto + 1;
if ($nextPhoto > $property->totalPhotos) {
$nextPhoto = intval(1);
}
?>
<img src="images/previous.png" alt="Previous photo" height="12" width="13" border="none"/>
<span id="photoPosition"><?php echo $mainPhoto; ?></span> of <?php echo $property>totalPhotos; ?>
<img src="images/next.png" alt="Next photo" height="12" width="13" border="none" />
</div>
</div>
<div class="col-md-6">
<div id="thumbnails">
<h3 class="additional">Photos</h3>
<?php
// Iterate throught the list of photos
foreach($property->photos->photo as $photo) {
?>
<script type="text/javascript">
addPhoto(
<?php echo $photo->id; ?>,
<?php echo $photo->width; ?>,
<?php echo $photo->height; ?>,
"<?php echo $photo->caption; ?>");
</script>
<img src="<?php echo $photo->url; ?>" width="<?php echo $photo->widthSmall; ?>" height="<?php echo $photo->heightSmall; ?>" class="image photo" id="photo<?php echo $photo->position; ?>" alt="Additional Photo of <?php echo $photo->address->advertising; ?>" onclick="return showPhoto(<?php echo $photo->position; ?>)" />
<?php }
?>
Any help is appreciated. Cheers
Dima
You should use ajax to get like this results.
you should go here,
The image slider shown in this demo is for free.
For detailed instructions, please visit online
http://www.menucool.com/slider/javascript-image-slider-demo1

Categories