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="">
<?
}
Related
I am trying to display images on my web page, the content is getting fetched from my database, but the issue I'm facing is in displaying the image. please, can anyone guide me how should I display the image?
I mean to say the path what I should give
here 'image' is my column name and this is my view
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/');$image?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
Hope this will help you :
Use this : <img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
The whole code should be like this :
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
If $image is the column name(which contains image name) then Replace
<img src="<?php echo base_url('uploads/');$image?>" alt="">
with
<img src="<?php echo base_url();?>uploads/<?php echo $row->image;?>" alt="">
If $image has image path then remove ';' and add '.' on echo base_url('uploads/');$image?> line
You issue here is the following code;
<?php echo base_url('uploads/');$image?>
This is because you are not concatenating the string, rather, just saying it's variable name, so, use of the following will provide the output;
<?php echo base_url('uploads/') . $image' ?>
This replaces the semi-colon part way through for a period (.) which is the PHP concatenation operator
Obviously, there is the issue you are not setting $image, which would require (before usage) of;
$image = $row['image_column_name'];
// Or
$image = $row->img_column_name
I want to add a script after showing 10 videos in my website. But I don't understand how to use an if condition for this. This is my php code which displays all the videos from my database. I want that after displaying 10 videos it display this script.
<script>
This may contain the code of chitika code.
<script>
This is PHP code.
<section class="videos">
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section> <hr>
<?php } ?>
<?php } ?>
</section>
Inside your loop you just need a simple condition and a counter.
Here's an example. This is a loop that runs 12 times using a while loop like your code.
$i is an incrementing variable ($i++ increments it's value at the end of each loop cycle). The if statement says if $i is equal to 10 then do something. In this case it echos "10 records reached".
Make sure that when you define your counter first ($i = 1) it is before the while loop starts.
<?php
$i = 1;
while ($i <= 12) {
echo "record $i<br/>";
if ($i == 10) {
echo "10 records reached<br/>";
}
$i++;
}
?>
So adapting that thought process to your code would look like this:
<section class="videos">
<?php $i = 1; // This is where the counter is defined ?>
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section><hr>
<?php } ?>
<?php if ($i == 10) { // This is the new condition ?>
<script>
This may contain the code of chitika code.
<script>
<?php } // condition ends here ?>
<?php $i++ // increment the counter ?>
<?php } ?>
</section>
If you don't want to include your null values in the count (your 'coming soon' videos), you can simply move the counter increment $i++ up into the else { ... } portion of your condition.
<?php if (res === NULL) {
...
} else {
...
$i++;
} ?>
I want to change some php code to select a random image from a specified directory rather than the same default image it currently selects. This is the piece of code that currently selects a default fallback image - if there is no image available:
<?php
$postCover = '';
if ($post->hasImage()) {
$postCover = $post->getImage($photoSize);
}
if (!$post->hasImage() && $params->get('photo_legacy', true)) {
$postCover = $post->getContentImage();
}
if (!$postCover && $params->get('show_photo_placeholder', false)) {
$postCover = $post->getImage($photoSize, true);
}
?>
<?php if ($postCover) { ?>
<div class="eb-mod-thumb<?php if ($photoAlignment) { echo " is-" . $photoAlignment; } ?> <?php if (isset($photoLayout->full) && $photoLayout->full) { echo "is-full"; } ?>">
<?php if (isset($photoLayout->crop) && $photoLayout->crop) { ?>
<a href="<?php echo $post->getPermalink();?>" class="eb-mod-image-cover"
style="
background-image: url('<?php echo $postCover;?>');
<?php if (isset($photoLayout->full) && $photoLayout->full) { ?>
width: 100%;
<?php } else { ?>
width: <?php echo $photoLayout->width;?>px;
<?php } ?>
height: <?php echo $photoLayout->height;?>px;"
></a>
<?php } else { ?>
<a href="<?php echo $post->getPermalink();?>" class="eb-mod-image"
style="
<?php if (isset($photoLayout->full) && $photoLayout->full) { ?>
width: 100%;
<?php } else { ?>
width: <?php echo (isset($photoLayout->width)) ? $photoLayout->width : '260';?>px;
<?php } ?>"
>
<img src="<?php echo $postCover;?>" alt="<?php echo $post->title;?>" />
</a>
<?php } ?>
</div>
<?php } ?>
I think it is this line I need to change:
<img src="<?php echo $postCover;?>" alt="<?php echo $post->title;?>" />
I have found this solution on here: PHP pull random image from folder
<?php
$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?> <img src="images/<?php echo $images[$i]; ?>" alt="" />
Which seems to be able to do what I want, but I am unsure how to incorporate it (or where) in the code I have supplied (not having php experience, but trying to learn).
Can anyone help me?
Kind regards
Mel
Let's say the link to your default image is : /path/to/default_imge.jpg
So a little solution for someone who doesn't like to create a big mess for this is :
.....
if (!$postCover && $params->get('show_photo_placeholder', false)) {
$postCover = $post->getImage($photoSize, true);
}
?>
// New Code Starts
$path='/path/to/default_imge.jpg';
if(stristr($postCover,$path) !==false){
$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
$postCover="images/".$images[$i];
}
// New Code Ends
<?php if ($postCover) { ?>
.......
In this case you can go back to the normal behavior just be removing the new code.
This will not answer your question, but this will help you while learning PHP/HTML
You mix up html and PHP in a way that makes the code unreadable.
In place of doing this:
if ($var) { ?> <div>Some HTML</div> <?php }
else { ?> <div>Some other HTML</div> <?php } ?>
Do this:
if($var){
echo "<div>Some HTML</div>";
}
else{
echo "<div>Some other HTML</div>";
}
It's a common beginner mistake, that makes it more difficult to learn coding because it "obfuscate" your code.
This will make your code more understandable for you and for us :)
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>
I'm trying to output an image with SimpleXML, but the image tag doesn't appear in the source code.
Can anyone help me outpout this image:
Here's my XML and code:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->location; ?></p>
</a>
</li>
<?php } ?>
Your issue is here:
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
You're trying to access url as though it were an attribute, you need to access it as a child element by using ->url instead.
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
EDIT: By the way, == !null works as you expect, but != null is a bit friendlier and less confusing
Your if statement is incorrect. It should be:
if ($event->Media['url'] != null)