Condition put on images - php

I want to put a condition on images come from directory that if the images count is 1 then show this div. But the condition is not running properly after applying the condition on it. Please help me to resolve this problem: My code is given below:
<?php
$id = 115;
$path_image = "pageimage/".$id."/";
if(file_exists($path_image)){
$dir = dir($path_image);
//List files in images directory
while (($file = $dir->read()) !== false){
if($file>=1){
?>
<div id="middleimg" style="background:url(<?php echo $path_image.$file; ?>); background-repeat:no-repeat;">
<div id="nav">
<div id="navinner">
<ul>
<?php
foreach($pageMenu as $pmenu){
$url = generateURL($pmenu->custom_url, $pmenu->page_friendlyname, $pmenu->page_title, $pmenu->page_id);?>
<li><?php echo $pmenu->page_name; ?> </li>
<?php } ?>
<!--<li>HOME</li>
<li>ABOUT</li>
<li>MENU</li>
<li>LOCATION</li>
<li>DELIVERY</li>
<li>SPECIAL OFFER</li>
<li>CONTACT</li>-->
</ul>
</div>
</div>
<div id="banner-containerhome">
<div id="promoBanner">
<!--<div id="pizza">-->
<div id="wowslider-container1">
<div class="ws_images">
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
</div>
<div class="ws_bullets"><div>
<img src="data1/tooltips/banner.jpg" alt=""/>1
<img src="data1/tooltips/chrysanthemum.jpg" alt=""/>2
</div></div>
<div class="ws_shadow"></div>
</div>
<script type="text/javascript" src="engine1/script.js"></script>
<!-- </div>-->
</div>
<div class="rightcontents">
<?php
echo $page_content[1]->page_content;
?>
</div>
</div>
</div>
<?php }}} ?>

There are a few things...
1.- read() reads the directory as an iterator, returning the file name, have you tried to echo $file; ?
if($file >= 1) will never be true, as $file is always a String, never an integer, you are comparing there: if("myfilename.ext" >= 1).
To count the total files you should do a while through the full directory and use a counter, increasing it for each file.
Or you can use scandir("myPath") function, that get all the files in the path specified as an array. for example:
$dir = scandir("myDir");
$total = count($dir); // this gives you the lenght of the array, therefore the files.
While counting, remember that the parent, and root directory are counted as "files" as well, and with some methods also the thumbs.db(windows) or DS_store(osx) are hidden files that store information about how you sort the folder, folder icons and stuff, will be counted as well, so remember to add an If statement to skip them.

Related

Why my images do not appears in a modal? Codeigniter

I am using codeigniter to develop a web system and I have to add a carousel slide on a modal, but the images does not appears when I'm using a foreach to select an image one by one
I'm running this web system with codeigniter 3.2 and php 7. I'm using a theme from themeforest called 'Pages ADM' which uses owl-carousel plugin.
<div class="container">
<div class="owl-carousel owl-theme">
<?php foreach ($produto['midias'] AS $midia) :?>
<img src="<?=base_url('uploads/midias/').$midia;?>"/>
<?php endforeach; ?>
</div>
</div>
and in my controller
$data['produto'] = $produto;
$data['produto']['midias'] = $this->produto_midia_class->listar('*');
where the function 'listar' was defined in my model
When Im using local images they appears, but when I'm using foreach, these images do not appear.
It would be easier to concatenate the string afterwards. Within "" the variables will be output.
<img src="<?php echo base_url()."uploads/midias/$midia";?>">
You could also do something like this:
<div class="container">
<div class="owl-carousel owl-theme">
<?php
foreach ($produto['midias'] as $midia){
$baseurl = base_url();
echo "<img src='$baseurl"."uploads/midias/$midia'>";
}
?>
</div>
</div>
Also just to note that usually the $midia will need an identifier like the column name if pulled from a database such as
<div class="container">
<div class="owl-carousel owl-theme">
<?php
foreach ($produto['midias'] as $midia){
$baseurl = base_url();
$imageurl = $midia->url;
echo "<img src='$baseurl"."uploads/midias/$imageurl'>";
}
?>
</div>
</div>
Hope this helps.

bootstrap image row out of place

[SOLVED]
I'm have written a horizontal row of 4 images 500x300 using placeholders, this works perfectly fine, but when I try to replace those images with images from a folder (using php) of the same dimensions, the first 2 image containes are knocked out of place while the last 2 are where they belong.. I've attached a screenshot and the code below is what I'm using to get the row of images.
<div class="row" style="margin-top:1px">
<?php
$directory = 'images';
if (! is_dir($directory)) {
exit('Invalid diretory path');
}
$files = array();
foreach (scandir($directory) as $file) {
?>
<div class="col-sm-3 col-xs-6" >
<a href="#" >
<img class="img-responsive portfolio-item" src="<?php echo "images/$file"; ?>" alt="">
</a>
</div>
<?php
}
?>
</div>
this is what i get in the browser
Using glob instead of an array fixed the problem. PHP is including the . and double dot of the directory structure and therefore creating 2 empty images.

Hide a <li tab if a condition is met [duplicate]

This question already has answers here:
Hiding a Div using php
(6 answers)
Closed 1 year ago.
I have a foreach look that checks if user has a picture or not. If the user doesn't have the picture I want to hide a <li> tab that shows the picture.
Inside my else how do I say, for this user hide tab 2?
I tried using echo 'id="tabHeader_2" style:"visibility:hidden"; but that doesn't work. I need a reference to that tab2, don't I?
<div id="picture<?php echo $i ?>" style="display: none;">
<?php
$picture = $user->pic;
if(isset($picture))
{
// show picture.
}
else
{
// hide tab2
}
?>
</div>
Then the list of tabs:
<div class="profiles">
<div id="tabContainer">
<div class="tabs">
<ul>
<li id="tabHeader_1"> Profile </li>
<li id="tabHeader_2"> Picture </li>
</ul>
</div>
<div id="tabsContent">
<div class="tabpage" id="tabpage_1"></div>
<div class="tabpage" id="tabpage_2"></div>
</div>
<script src="<?php echo Yii::app()->theme->baseUrl; ?>/js/tabs.js"></script>
</div>
</div>
Picture of what the page looks like:
I am not 100% sure about your code, but you are using isset() which would not be correct, because you set the variable right behind that check so it will be set 100% of the time. You will want to check empty() or is_file() perhaps, depends on what is in that variable.
<!-- This display: none seems strange if you are going to show the wrapped elements -->
<div id="picture<?php echo $i ?>" style="display: none;">
<?php
$picture = $user->pic;
if(!empty($picture)) {
// show picture.
}
else {
// hide tab2
}
?>
</div>
If you put the real code, it would be easier to see how you are laying out your elements.
Do Like This :-
<div class="tabs">
<ul>
<?php
$i = 1;
foreach($yourarray as $key => $value){
if(isset($value) && $value != '')
?>
<li id="tabProfileHeader<?php echo $i;?>"> Profile </li>
<li id="tabPictureHeader<?php echo $i;?>"> Picture </li>
<?php
$i++;
}
?>
</ul>
</div>

How to set two sources to an image in HTML depending on where the file is

I am making a website in PHP and have come across a problem where if I include my head.php file from a file in another directory it cannot find my logo image. I have tried including two images, that worked accept I had the file error image next to the logo as only one of the images are in a correct location. Does anybody know how I can fix this?
head.php code
<div id="head">
<div id="inner_head">
<div id="logo">
<img src="images/logo.png">
</div>
<div id="search">
<form action='search/search.php' method='get'>
<div id="search_feild">
<input type="text" name="input">
</div>
<div id="search_submit">
<input type="submit" VALUE="" /><div id="results"></div>
</div>
</form>
</div>
<a href="user/login.php">
<div id="login_head">
Login
</div>
</a>
<div id="nav">
<ul>
<li>Home</li>
<div class="nav_mid"></div>
<li>About</li>
<div class="nav_mid"></div>
<li>Teachers</li>
<div class="nav_mid"></div>
<li>Links</li>
<div class="nav_mid"></div>
<li>Links</li>
<div class="nav_mid"></div>
<li>Art</li>
<div class="nav_mid"></div>
<li>Subjects</li>
<div class="nav_mid"></div>
<li>Downloads</li>
<div class="nav_mid"></div>
<li>Media</li>
<div class="nav_mid"></div>
<li>Contact</li>
</ul>
</div>
</div>
</div>
It's not good idea to load 2 image for resolve the problem but however, I think you can use this:
$logo_1 = "logo 1 path";
$logo_2 = "logo 2 path";
if (file_exists($logo_1))
{
echo '<img src="$logo_1" />';
}
elseif (file_exists($logo_2))
{
echo '<img src="$logo_2" />';
}
You're probably running into relative path issues. Since the file that you're alternatively calling the head from is referencing assets from its relative path, the new path is now incorrect.
-- index.php
-- head.php
-- directory
---- file.php (includes head.php) any references are now 1 directory deeper and now incorrect
To solve this, serve all assets absolutely, so you avoid any conflicts
<img src="<?php include $_SERVER['DOCUMENT_ROOT'] . "/project-name/images/logo.png"; ?>" />
You should just create a symlink to the image file in the second location. Then you only have to worry about updating one file.
ln -s /myoriginalsite/images/logo.php /myothersite/images/logo.php
Now you just need to update /myoriginalsite/images/logo.php and both will change.
No PHP code needed.

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>

Categories