Add display property only from second item in for loop - php

I am creating a simple image gallery that requires the first image to be shown first, while the images after are supposed to have a display;none property. How do I achieve it so that only the first image is different? The following is my code so far.
<div class="content">
<?php
foreach($variants['productVariants'][0]['productVariantImages'] as $variantImage){
if(isset($variantImage) && $variantImage['visible']){
?>
<img src="<?php echo $variantImage['imagePath']; ?>" class="image_<?php echo $variantImage['id']; ?>" style="display:none" alt="" />
<?php }}?>
</div>
My current output is
<div class="content">
<img src="images/bigs/2.jpg" class="image_1" style="display:none" alt="">
<img src="images/bigs/2.jpg" class="image_2" style="display:none" alt="">
<img src="images/bigs/3.jpg" class="image_3" style="display:none" alt="">
</div>
I would like to be able to achieve the following
<div class="content">
<img src="images/bigs/1.jpg" class="image_1" alt="">
<img src="images/bigs/2.jpg" class="image_2" style="display:none" alt="">
<img src="images/bigs/3.jpg" class="image_3" style="display:none" alt="">
</div>

You have multiple ways:
Using a variable (eg. $first, set it to true before the loop and to false at the end of the first iteration)
Using the keys of your (probably 0-indexed?) array (eg. foreach($variants['productVariants'][0]['productVariantImages'] as $key => $variantImage){, notice the $key => part). You can then check if $key is 0 in the loop and that's your first element.
As #alirezasafian suggested, simply use CSS - this is probably the most versatile method.

You don't need to use php for that, you can do it by css.
.content img:not(:first-child)
{
display: none;
}
.content img:not(:first-child) {
display: none;
}
<div class="content">
<img src="images/bigs/1.jpg" class="image_1" alt="1">
<img src="images/bigs/2.jpg" class="image_2" alt="2">
<img src="images/bigs/3.jpg" class="image_3" alt="3">
</div>

Related

Extract links from specific table

I have a html code with many html tables. I want to extract links from specific one which has specific div above.
Here's my sample code:
<div class="boxuniwersal_header">Table 1</div>
<img src="img/boxuniwersal_top.gif" width="210" height="18" alt="" style="margin-top: 5px" />
<div class="boxuniwersal_content">
<div class="boxuniwersal_subcontent">
<div class='menu_m1'><table cellpadding="3"><tr><td><img src="some.jpg" width="45" /></td><td>Some text</td></tr></table></div>
<br />
</div>
</div>
<!-- /box -->
<!-- box -->
<div class="boxuniwersal_header">Table 2</div>
<img src="img/boxuniwersal_top.gif" width="210" height="18" alt="" style="margin-top: 5px" />
<div class="boxuniwersal_content">
<div class="boxuniwersal_subcontent">
<div class='menu_m1'><table cellpadding="3"><tr><td><img src="some2.jpg" width="45" /></td><td>Some text2</td></tr></table></div>
<br />
</div>
</div>
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query("//div/div/table/tr/td/a|//table//tr/td//a"); //querying domdocument
foreach($results as $result)
{
$links[]=$result->getAttribute("href");
}
This code returns all links. I want to grab only links from Table1. Is it possible?
Your main problem is just tuning the XPath expression to select the right XML.
If you change your XPath to
//div[text()="Table 1"]/following-sibling::div[1]//table//a
What this does is first find the <div> element whose text is the one your after.
The following-sibling::div[1] part will look at the first <div> element at the same level as the <div> element already selected (this is the one where the <table> is).
The last part just looks for all <a> elements within the enclosing <table>.

how do i make an image to zoom in bootstrap in codeigniter view when click on image?

<h5>
<a href="<?php echo base_url(); ?>/vendor/home/projects" >Back to Projects</a>
<h5>
<div>
<div class="container">
<div class="row">
<?php
foreach ($projects as $value) {
?>
<?php
$project_images = json_decode($value['project_gallery'],true);
$i=0;
foreach ($project_images as $project_image_value) {
$i++;
?>
<div class="col-md-2">
<img src="<?= base_url() ?>assets/uploads/projects/<?=$project_image_value['fname']?>" onclick="openModal(<?= $i?>)" class="img-thumbnail img-responsive " />
<div><?=$project_image_value['title']?></div>
</div> <?php
}
?>
<?php
}
?>
</div>
</div>
this is my codeigniter view to display images. How can i Apply light box so that when you click image, only that image should be zoomed
There are so many easy to use JQuery plugins available for image zoom kind of functionality. Use any of the following:
There use is as simple as:
$(document).ready(function(){
$('a.photo').zoom({url: 'photo-big.jpg'});
});
Reference
Some more reference:
https://i-like-robots.github.io/EasyZoom/
http://www.jqueryscript.net/tags.php?/image%20zoom/
http://www.jqueryscript.net/demo/Simple-jQuery-Magnifying-Glass-Image-Zoom-Plugin-magnify-js/

MYSQL PHP HTML Load image from database to website

Hello I have in my DB a column called capalivro that has my image paths.
I want to make the img src the content of that column, but I'm not succeding in doing this since I don't know how to put that PHP var in the HTML.
This way I tried doesn't work because the php doesn't work because of the quotation marks.
<?php while ($livro = mysql_fetch_assoc($livrotodos)) { ?>
<div class="large-2 columns">
<div class="livro">
<div class="livro-overlay">
<h3><?php echo $livro['nomelivro'] ?></h3>
</div>
<img src= "<?php $livro['capalivro']?>" />
</div>
</div>
<?php }
?>
</div>
<img src= "<?php $livro['capalivro']?>" />
</div>
should be:
</div>
<img src= <?php echo "\"". $livro['capalivro']."\""?> />
</div>
change your line containing the img src= bit to:
<img src="<?php echo $livro['capalivro']?>" />
without the echo part, it won't display the "string" from the database.

get two paths of images from database and loop them to display in slider

Im little bit stuck on a logic.scenario is i have a slider where two images are shown at a time taking 50%-50% part of screen,here is the simple html of it,
<div class="zeus-slider zeus-default" >
<div class="zeus-block">
<div class="zeus-slide s-50"> <img src="slider/slider1.jpg" data-effect="slideRight" alt="" /> </div>
<div class="zeus-slide s-50"> <img src="slider/slider2.jpg" data-effect="slideRight" alt="" /> </div>
</div>
</div>
here every block contains two images i have shown only one.now i have trying to make it dynamic, but i didnt get any idea how it will work, so i created database with two tables, each for individual image in a single block.Both table have fields like simg_id(A_I),img_name,simg_path.
here its my php on slider page.
<div class="zeus-block">
<?php
$slider_slt=getResultSet('select * from slider_img_master1 ORDER BY simg_id');
if(mysql_num_rows($slider_slt))
{
while($slider_data=mysql_fetch_assoc($slider_slt))
{
?>
<div class="zeus-slide s-50"> <img src="slider/first/<?php echo $slider_data['simg_path']?>" data-effect="slideRight" alt="" /> </div>
<?php }
} ?>
<?php
$slider_slt2=getResultSet('select * from slider_img_master2 ORDER BY simg_id');
if(mysql_num_rows($slider_slt2))
{
while($slider_data2=mysql_fetch_assoc($slider_slt2))
{
?>
<div class="zeus-slide s-50"> <img src="slider/second/<?php echo $slider_data2['simg_path']?>" data-effect="slideRight" alt="" /> </div>
<?php }
} ?>
</div>
now the problem is when i try to fetch path of image in slider, images are not changing one by one on both half of screen.instead it showing like both images from from both table top, another two images from both tables below 1st both, and so on , so full page is covered with images.
I know this idea of creating two tables for getting two images at once is silly,but i could not think any better.if any one can suggest any better way, it would be so helpful.
Update:getResultSet is function for mysql_query.
if anybody interested i found an answer for above question.
<div id="slide1" >
<div class="zeus-slider zeus-default" >
<?php
$slider_str="select *from slider_img_master1 where simg_status='Active'";
$i=1;
$result=mysql_query($slider_str);
if(mysql_num_rows($result)>0)
{
echo '<div class="zeus-block">';
while($row=mysql_fetch_assoc($result))
{
if($i%2==1 && $i!=1)
{
echo '<div class="zeus-block">';
}
?>
<div class="zeus-slide s-50"> <img src="slider/<?php echo $row['simg_path'];?>" data-effect="slideRight" alt="" /> </div>
<?php
if($i%2==0)
{
echo '</div>';
}
$i++;
}
} ?>
</div>
<div class="clear"> </div>
<div class="next-block"> </div>
<div class="prev-block"> </div>
</div>

PHP & WP: Render Certain Markup Based on True False Condition

So, I'm working on a site where on the top of certain pages I'd like to display a static graphic and on some pages I would like to display an scrolling banner.
So far I set up the condition as follows:
<?php
$regBanner = true;
$regBannerURL = get_bloginfo('stylesheet_directory'); //grabbing WP site URL
?>
and in my markup:
<div id="banner">
<?php
if ($regBanner) {
echo "<img src='" . $regBannerURL . "/style/images/main_site/home_page/mock_banner.jpg' />";
}
else {
echo 'Slider!';
}
?>
</div><!-- end banner -->
In my else statement, where I'm echoing 'Slider!' I would like to output the markup for my slider:
<div id="slider">
<img src="<?php bloginfo('stylesheet_directory') ?>/style/images/main_site/banners/services_banners/1.jpg" alt="" />
<img src="<?php bloginfo('stylesheet_directory') ?>/style/images/main_site/banners/services_banners/2.jpg" alt="" />
<img src="<?php bloginfo('stylesheet_directory') ?>/style/images/main_site/banners/services_banners/3.jpg" alt="" />
.............
</div>
My question is how can I throw the div and all those images into my else echo statement? I'm having trouble escaping the quotes and my slider markup isn't rendering.
<div id="banner">
<?php if($regbanner): ?>
<img src="<?php echo $regBannerURL; ?>/style/images/main_site/home_page/mock_banner.jpg" />
<?php else: ?>
<div id="slider">
<img src="<?php echo ($bannerDir = bloginfo('stylesheet_directory') . '/style/images/main_site/banners/services_banners'); ?>/1.jpg" alt="" />
<img src="<?php echo $bannerDir; ?>/2.jpg" alt="" />
<img src="<?php echo $bannerDir; ?>/3.jpg" alt="" />
.............
</div>
<?php endif; ?>
</div><!-- end banner -->
If you don't like the offered solution using the syntaxif(...):...else...endif; you also have the possibilty of using heredoc-style to include bigger html-parts into an echo-statement without the need of escaping it.
The code-formatting in here unfortunatly messed up my example, which I wanted to post. But if you know the heredoc-notation, it should not be a problem ;)

Categories