hiding a slider image in php - 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++;
}

Related

Shuffle and only show a certain item in a PHP foreach loop

I am trying to get shuffle and only 19 items in a for each loop. I used shuffle() and if (++$i == 19) {break;}, but the problem is one time it returns 12 items, one time 13, and another time 14. What did I do wrong?
Here is the code to check:
<?php
$i = 0;
shuffle($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
if (++$i == 19) {
break;
}
}
?>
Increase $i only when a result has been printed. Check the value before starting a new foreach loop.
<?php
$i = 0;
shuffle($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
$i++;
}
if ($i == 19) {
break;
}
}
?>
I think you want to shuffle array,
Use this function,
function shuffle_assoc(&$array) {
$keys = array_keys($array);
shuffle($keys);
foreach ($keys as $key) {
$new[$key] = $array[$key];
}
$array = $new;
return true;
}
$i = 0;
shuffle_assoc($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
if (++$i == 19) {
break;
}
}
?>
I hope this will help you solve your problem.
A simplified version of your code
foreach ($children as $child) {
if ($child['name_total'] > 0) {
// display stuff
}
if (++$i == 19) {
break;
}
}
So $i is incremented every time, even if nothing is displayed. If $child['name_total'] is 0, nothing will be displayed - but $i is still incremented.
If you want 19 items displayed, you need to only increment $i when something is actually displayed:
foreach ($children as $child) {
if ($child['name_total'] > 0) {
// display stuff
if (++$i == 19) {
break;
}
}
}
Please try the following.
<?php
$counter = 0;
shuffle($children);
foreach ($children as $child)
{
if ($counter < 18)
{
break;
}
if($child['name_total'] > 0)
{
$counter++;
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : "");?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
}
?>

How to loop a php code?

I wanted to do the DRY approach in my code but I'm having a hard time figuring it out. And also, I want to hide the entire code if there's no image_1. Hope you could help me do the trick.
Here's the code
<div class="col-md-4">
<?php
$image = get_field('image_1');
if(get_field('image_1'))
{
echo '<a href="' . get_field('image_link_1') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_2');
if(get_field('image_2'))
{
echo '<a href="' . get_field('image_link_2') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_3');
if(get_field('image_3'))
{
echo '<a href="' . get_field('image_link_3') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
You should put differences to arrays and then wrap everything into for loop:
<?php
$images = array('image_1', 'image_2', 'image_3');
$links = array('image_link_1', 'image_link_2', 'image_link_3');
for($i=0; $i<3; $i++){
?>
<div class="col-md-4">
<?php
$image = get_field($images[$i]);
if(get_field($images[$i])){
echo '<a href="' . get_field($links[$i]) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
}
?>
</div>
<?php
}
?>
Just a hint...:
<?php
for ($i = 0; $i < 3; $i++) {
echo "<div class='col-md-4'>" . "\n";
$image = get_field("image_" . ($i + 1));
...
echo "</div>" . "\n";
}
?>
Something along these lines should get you started if I'm understanding you correctly:
<?php for ($q = 1; $q <= 3; $q++) {
$image_loop = 'image_' . $q;
echo '<div class="col-md-4">';
if ($image = get_field($image_loop)) {
echo '<a href="' . get_field('image_link_' . $q) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<?php } // end loop ?>
The other suggestions here will work as well but here's what I would do:
First arrange the images in an associative array with keys being the image name and values being the image link and then iterate via a foreach loop.
I generally try to not echo HTML unless strictly necessary.
<?php
$array = [
"image_1" => "image_link_1",
"image_2" => "image_link_2",
"image_3" => "image_link_3",
"image_4" => "image_link_4"
];
foreach ($array as $name => $link):
$image = get_field($name);
if ($image): ?>
<div class="col-md-4">
<a href="<?=get_field($link)?>">
<img src="<?= $image['url']; ?>" title="<?= $image['title']; ?>" alt="<?= $image['alt']; ?>" />
</a>
<?php else: ?>
<img src="http://localhost/image.png">
<?php endif; ?>
</div>
<?php endforeach; ?>

PHP loop need to create new div every 5 elements

I've got a forech loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php foreach($this->entries as $entry): ?>
<figure class="image_container">
<img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
<?php endforeach; ?>
</div>
</div>
I hope you guys can help me.
A simple counter could help -
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php
$i = $j = $k = 0;
foreach($this->entries as $entry):
$i++;
$class = '';
if($j === 0) {
$class = 'first';
}
$j++;
$html = '';
if($i % 5 === 0) {
$k++;
$j = ($i - (5 * $k));
$class = 'last';
$html = "</div></div>
<div class='autogrid_wrapper cte block'><div class='inner'>";
}
?>
<figure class="image_container <?php echo $class; ?>">
<img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
<?php
echo $html;
endforeach;
?>
</div>
</div>

How to get individual product related images in magento

I want to get each product related images which is not assign to product but want to show all related images in front-end.
<?php
foreach ($data['product'] as $product) {
$product_id = $product->getId();
$product_name = $product->getName();
$product_description = $product->getDescription();
$product_price = $product->getPrice();
$isporductImage = $product->getImage();
$product_image = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, true) . "media/catalog/product" . $product->getImage();
$isproductthumbnail = $product->getThumbnail();
$product_thumbnail = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, true) . "media/catalog/product" . $product->getThumbnail();
$collectionimage = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, true) . "media/catalog/category/thumb/" . $data['collection']['image'];
$productplaceholderImage = $this->getSkinUrl() . "whi/assets/images/whi_placeholder.png";
?>
<?php if ($isproductthumbnail != "") { ?>
<div class="image-div" style="background: url('<?php echo $product_thumbnail;?>');">
<img src="<?php echo $product_thumbnail; ?>" alt="celeb" />
</div>
<?php }elseif ($isporductImage != "") { ?>
<div class="image-div" style="background: url('<?php echo $product_image;?>');">
<img src="<?php echo $product_image; ?>" alt="celeb" />
</div>
<?php } else { ?>
<div class="image-div" style="background: url('<?php echo $productplaceholderImage;?>');">
<img src="<?php echo $productplaceholderImage ?>" alt="celeb" />
</div>
<?php } ?>
i want to show all related product images in this list.
<ul class="outfit-images-cmt">
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
</ul>
Try the below code for fetch all the gallery images:
$product_image_url_gallery = $product->getMediaGalleryImages();
$galleryURL = '';
echo '<ul class="outfit-images-cmt">';
foreach ($product_image_url_gallery as $image) {
$galleryImage = Mage::helper('catalog/image')->init($product, 'image', $image->getFile());
echo '<li><img src="'.$galleryURL.'"></li>'
}
echo '</ul>';

Grouping database results within while loop

I am running a while loop to return all the results of a mysqli query but I want to use php to group the results.
My code is
while($row = mysqli_fetch_array($result))
{
if($row['total'] >0)//row['total'] is found out within the query itself
{
$recipeWords = str_word_count(preg_replace('/\s{1,}(and\s*)*/', ' ', $row['title']));
if($countTerms == $recipeWords)
{
echo "<h2>Exact Match</h2>";?>
<div class="fluid recipeContainerSmall">
<a href="recipe_details.php?id=<?php echo $row['recipeID']?>">
<img src="images/recipes/<?php echo $row['image']?>" alt="<?php echo $row['title']?>" title="<?php echo $row['title']?>" />
<h4><?php echo $row['title']?></h4></a>
</div><?php
}
elseif($countTerms == ($recipeWords-1))
{
echo "<h2>Closest Matches</h2>";?>
<div class="fluid recipeContainerSmall">
<a href="recipe_details.php?id=<?php echo $row['recipeID']?>">
<img src="images/recipes/<?php echo $row['image']?>" alt="<?php echo $row['title']?>" title="<?php echo $row['title']?>" />
<h4><?php echo $row['title']?></h4></a>
</div><?php
}
else
{
echo "<h2>Other Suggestions</h2>";?>
<div class="fluid recipeContainerSmall">
<a href="recipe_details.php?id=<?php echo $row['recipeID']?>">
<img src="images/recipes/<?php echo $row['image']?>" alt="<?php echo $row['title']?>" title="<?php echo $row['title']?>" />
<h4><?php echo $row['title']?></h4></a>
</div><?php
}
}
}?>
What I am trying to do is group the results so that:
all the exact matches are displayed first
then I want all the records where all but one of the keywords are found
then the rest
at the moment they are being displayed on what seems a random order.
<?php
while($row = mysqli_fetch_array($result))
{
if($row['total'] >0)//row['total'] is found out within the query itself
{
$recipeWords = str_word_count(preg_replace('/\s{1,}(and\s*)*/', ' ', $row['title']));
$exactMatches = "";
$closestMatches = "";
$otherSuggestions = "";
if($countTerms == $recipeWords)
{
$exactMatches .= <<<EXACT_MATCH
<h2>Exact Match</h2>
<div class="fluid recipeContainerSmall">
<a href="recipe_details.php?id={$row['recipeID']}">
<img src="images/recipes/{$row['image']}" alt="{$row['title']}" title="{$row['title']}" />
<h4>{$row['title']}</h4></a>
</div>
EXACT_MATCH;
}
elseif($countTerms == ($recipeWords-1))
{
$closestMatches .= <<<CLOSEST_MATCH
<h2>Closest Match</h2>
<div class="fluid recipeContainerSmall">
<a href="recipe_details.php?id={$row['recipeID']}">
<img src="images/recipes/{$row['image']}" alt="{$row['title']}" title="{$row['title']}" />
<h4>{$row['title']}</h4></a>
</div>
CLOSEST_MATCH;
}
else
{
$otherSuggestions .= <<<OTHER_SUGGESTIONS
<h2>Other Suggestions</h2>
<div class="fluid recipeContainerSmall">
<a href="recipe_details.php?id={$row['recipeID']}">
<img src="images/recipes/{$row['image']}" alt="{$row['title']}" title="{$row['title']}" />
<h4>{$row['title']}</h4></a>
</div>
OTHER_SUGGESTIONS;
}
// ECHO in the order you want to...
echo $exactMatches . $closestMatches . $otherSuggestions;
}
}?>
As asked by you above, please try this code.
Please note that I've used HEREDOCS to simplify the parsing of the data.

Categories