I have this code in my php file.
<div class="the-avatar">
<div class="flash"></div>
<div class="avatar">
<img src="avatar/default-avatar.jpg" alt="">
</div>
I want to do this:
if file upload/user_avatar.jpg exist
show upload/user_avatar.jpg
else
show avatar/default-avatar.jpg
Save it to a dB if you ask me, checking everytime seems awkward.
Anyways,
$filename = 'upload/user_avatar.jpg';
if (file_exists($filename)) {
echo '<img src="'. $filename .'" alt="" />';
} else {
echo '<img src="avatar/default-avatar.jpg" alt="" />';
}
Straight from http://nl3.php.net/file_exists
You can use
<?php
if (file_exists('upload/user_avatar.jpg')) {
echo "<img src='upload/user_avatar.jpg'>";
} else {
echo "<img src='avatar/default-avatar.jpg'>";
}
?>
Your question shows no effort of research, so it is off-topic, though, here's the solution.
<div class="the-avatar">
<div class="flash"></div>
<div class="avatar">
<img src="<?php
If (file_exists('upload/user_avatar.jpg')) {
echo 'upload/user_avatar.jpg';
} else {
echo 'upload/default_avatar.jpg';
}
?>" alt="">
</div>
Related
i have a little script for my cms. I'm trying to create a default image (default.png) when image linked not exist. I have all images in another website and i created this code:
<?php
$immagine_e = $_GET('http://www.website.com/images/'.$provincia'.png');
?>
<?php if ($immagine_e ==''){ ?>
<img src="images/default.png" class="margin-top-negative-70" alt="">
<?php } else { ?>
<img src="$immagine_e" class="margin-top-negative-70" alt="">
<?php } ?>
$provincia = name of city. For example milano and if http://www.website.com/images/milano.png not exist i need default images. If exist i need this image.
Can anyone help me?
You can use this API: https://placehold.it/
Example:
<?php
$immagine_e = $_GET('http://www.website.com/images/'.$provincia'.png');
?>
<?php if ($immagine_e ==''){ ?>
<img src="https://placehold.it/350x150" class="margin-top-negative-70" alt="">
<?php } else { ?>
<img src="$immagine_e" class="margin-top-negative-70" alt="">
<?php } ?>
Here is another solution that you can consider.
$immagine_e = "http://www.website.com/images/$provincia.png";
$size = getimagesize($immagine_e);
if(isset($size[0]))
{
?>
<img src="$immagine_e" class="margin-top-negative-70" alt="">
<?
}else
{
?>
<img src="images/default.png" class="margin-top-negative-70" alt="">
<?
}
I have the following foreach:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246"></div>
</div>
<?php } ?>
But thus appears only a div .album within each div .wrapper. How do I see two divs .album within each div .wrapper?
UPDATE
Guys, found the solution:
<?php
$total = 0;
foreach($result15 as $row15){
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
if($total == 0){
echo '<div class="wrapper">';
}
?>
<div class="album" data-disco="disco<?php echo $id15; ?>">
<img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
</div>
<?php
$total = $total + 1;
if($total % 2 == 0){
echo '</div>';
$total = 0;
}
}
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
<div class="album"><img src="img/..." alt="" width="246" height="246"></div>
</div>
Something like this?
EDIT
I dont understand what you want. But you can do this to get two divs, but you will need to get your image path for the second div image:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
Try this:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
A nice solution could be to use array chunk considering you want to treat the data in 'chunks' of 2 images at a time. This way, if you want to alter how many images appear in a wrapper you only need to change your chunk size.
$chunkSize = 2;
foreach (array_chunk($result15, $chunkSize) as $wrapperImages) {
echo '<div class="wrapper">';
foreach ($wrapperImages as $image) {
$thumb = $image->thumb;
echo '<div class="album"><img src="img/'.$thumb.' alt="" width="246" height="246"></div>';
}
echo '</div>';
}
I want to use the <?php bloginfo('stylesheet_directory'); ?> inside my wordpress loop to reference an image but unsure how to do this. My image code is as follows:
<?php
if (is_category('Events')) {
echo '<img src="http://localhost/mmj/wp-content/themes/child-theme/img/live-banner.jpg" class="live-holder-img" />';
} else if (is_category('News')) {
echo '<img src="http://localhost/mmj/wp-content/themes/child-theme/img/live-banner.jpg" class="live-holder-img" />';
} else {
echo '<img src="" class="default" />';
} ?>
I would rather replace http://localhost/mmj/wp-content/themes/child-theme with <?php bloginfo('stylesheet_directory'); ?> but Im aware that I cant include <?php inside <?php so I was wondering how I could do this?
You can use like this :
echo '<img src="'.get_bloginfo('stylesheet_directory').'/img/live-banner.jpg" class="live-holder-img" />';
Full code :
<?php
if (is_category('Events')) {
echo '<img src="'.get_bloginfo('stylesheet_directory').'/img/live-banner.jpg" class="live-holder-img" />';
} else if (is_category('News')) {
echo '<img src="'.get_bloginfo('stylesheet_directory').'/img/live-banner.jpg" class="live-holder-img" />';
} else {
echo '<img src="" class="default" />';
} ?>
In shortly,
I'm using get_bloginfo() instead of bloginfo() for getting stylesheet directory, not printing out. And then using it like this :
echo 'Some strings here ' . get_bloginfo() . ' another strings';
Goal:
I am trying to show an image in the view.ctp. I am trying to grab the image that was saved with the rest of the information. I am pretty sure the code is 100% correct in the ProductsController, if not that'll be posted next.
Here is the code in the view.ctp for products:
<h1>Viewing Product</h1>
<div><b>Name:</b> <?php echo $Product['Product']['name']; ?></div>
<div>
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
</div>
<div><b>Description:</b> <?php echo $Product['Product']['description']; ?></div>
<div><b>Price:</b> <?php echo $Product['Product']['price']; ?></div>
Read down a bit to see the actual solution.
Shoudln't
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
be:
<img src="<?php echo $path .'/'. $Product['Product']['filename']; ?> " width="300px" />
?
Since the original poster actually answered their own question I thought I'd elaborate.
The Solution:
Replacing:
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
with:
<?php echo $this->Html->image('$Product['Product']['filename']') ?>
I was trying to change the style of only a part of php. This is my codes;
if($fetch_array)
{
$foto_destination = $fetch_array['foto'];
echo "<img src = '$foto_destination' height='150px' width='150px'>";
}
else
{
?>
<div style= "position:absolute; left:350px; top:70px;">
<?php
echo "<img src = 'images/avatar_default.png' height='150px' width='150px'>";
?>
</div>
But, this php part is inside if. This is why i could not change it? I want to display the image where i define it inside the div tag if the statement of "if" is true. How can i do this? Where am i missing?
Thanks
If I understand you correctly, it should be:
<?php
if($fetch_array){
?>
<div style= "position:absolute; left:350px; top:70px;">
<?php
$foto_destination = $fetch_array['foto'];
print " <img src = '$foto_destination' height='150px' width='150px'>";
}else{
?>
<div style= "position:absolute; left:350px; top:70px;">
<img src = 'images/avatar_default.png' height='150px' width='150px'>
<?php
}
?>
</div>
It shows the $foto_destination, if there is one.
HTH
Did you mean like this?
<?php
if($fetch_array) {
$photo = $fetch_array['foto'];
$styles = 'position:absolute; left:350px; top:70px;';
} else {
$photo = 'images/avatar_default.png';
$styles = 'position:absolute; left:350px; top:70px;';
}
?>
<div style="<?php echo $styles; ?>">
<img src="<?php echo $photo; ?>" height="150" width="150" />
</div>
That is correct or you can do an isset()
if (isset($fetch_array) {
...
The only advantage being that it will not error if the variable is undefined
Here's a more compact version, shorttags must be enabled.
<div style="position:absolute; left:350px; top:70px;">
<img src="<?= isset($fetch_array['foto']) ? "images/avatar_default.png" : $foto_destination['foto'] ?>" height="150px" width="150px" />
</div>
otherwise:
<div style="position:absolute; left:350px; top:70px;">
<img src="<?php echo isset($fetch_array['foto']) ? "images/avatar_default.png" : $foto_destination['foto'] ?>" height="150px" width="150px" />
</div>