How to set this if isset function - php

I have the following code that sets a background if user has uploaded to database. If user has NOT uploaded an image then the result is a blank img src=''
I need to set this as an if isset function so I can plug in an alternate image if user has not uploaded anything.
Here is the current code:
<div id="background"><?php echo isset($background_image) && file_exists(ROOT.$background_image)?"<img src='$background_image' alt='' />":'';?></div>

Your code's a little dirty, opening php and closing it mid-html tag is only going to make it confusing for you in the future.
You're echoing back an isset which is just echo'ing back a boolean.
Try this;
$background_image = ""; // Not sure what you're using here - their username? Dump it in here anyway.
if (file_exists($background_image))
{
echo " <div id=\"background\">
<img src=\"{$background_image}\" alt=\"\" title=\"\" />
</div>";
}
Hope this helps.
Eoghan

I'm not sure what you mean, but a another aproach to what you are trying to do would be:
<div id="background">
<?php
$optionalImage = 'background.png';
$userImage = getUserImage();
if(empty($userImage)) {
$userImage = $optionalImage;
}
?>
<img src="<?php echo $userImage; ?>" />
</div>

Is it really neccessary to set the blank source of the image?
But a understandable and corrected code of what you are attempting is this
<div id="background">
<?php
echo "<img src='";
echo isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '';
echo "' alt='' />";
?>
</div>
The problem was that, either you were echo entire <img> tag, or just display ' '(Blank) with attached endings.
The short form:
echo "<img src='".(isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '')."' alt='' />";

Related

Trying to use in line styling in the middle of a PHP echo

Is there anyway I can get in-line styling within a PHP echo statement. I want to set the height and the width of an image, as when I try to apply the height and `width externally it makes no difference as the image loads before the style sets in.
I have tried this but doesn't seem to be making any difference what so ever...
<p><b>Profile Picture: </b>
<?php
$picture = $row['imagePath'];
if (empty($picture)){
echo "<img src='profiles/no-image.png' 'width=500' 'height=600' >";
} else {
echo "<img src='".$row['imagePath']."' 'width=500' 'height=600' >";
};
?></p>
That doesn't work because you are not setting the quotes right.
This should do the trick:
echo "<img src='profiles/no-image.png' width='500' height='600' >";
You can apply styles the same way:
echo "<img src='profiles/no-image.png' style='width:500px;height:600px;'>";
Here is an alternative. Use PHP like a template engine. This approach does not use echo statements to output HTML. Instead, dynamic elements are introduced as needed.
<p><b>Profile Picture: </b>
<?php
$picture = $row['imagePath'];
if (empty($picture)) :
?>
<img src="profiles/no-image.png" width="500" height="600" >
<?php else : ?>
<img src="<?= $row['imagePath'] ?>" width="500" height="600" >
<?php endif ?>
</p>

I'm trying to echo an image in a PHP variable in CodeIgniter

Hello I'm trying to echo an image on a view page in CodeIgniter but nothing is displayed on the page.
Here I'm making the variable:
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
And here I'm trying to echo the image:
<img src="<?php echo $image;?>">
Intro
This is the most basic php, so what's the addition of this question? Please read the basics of echo here: http://php.net/manual/en/function.echo.php (Example 1).
Learn the basics first!
Solution
1. Assign full html tag to variable and echo full html:
<?php
$image = '<img src="../../images/stoel.jpg" alt="Foo">';
echo $image;
?>
2. Or assign image path to variable and echo concat string:
<?php
$path = '../../images/stoel.jpg';
echo '<img src="' . $path . '" alt="Foo">';
?>
3. Or assign image path to variable and echo only this with php:
<?php
$path = '../../images/stoel.jpg';
?>
<img src="<?= $path; ?>" alt="Foo">
You are inserting full Image tag into src attribute.
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
and
<?php echo $image; ?>
or
<?php $image = "../../images/stoel.jpg"; ?>
and
<img src="<?php echo $image;?>">
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
you try like this
<?php echo $image; ?>
You are already assign full image code in variable so you just need to print your variable:
<img src="<?php echo $image;?>">
To
<?php echo $image;?>
Make sure your images out side of the application folder then you can do something like
application
images
images > stoel.jpg
system
index.php
Use Base url from the url helper
<img src="<?php echo base_url('images/stoel.jpg');?>" />
Make sure you have set the base_url in config.php
$config['base_url'] = 'http://localhost/yourproject/';
You can also use HTML Helper img();
I think this is what you mean. Since you are echoing inside the src attribute, you do not need to store the whole <image>, just the path will do.
<?php
$image = "../../images/stoel.jpg";
?>
<img src="<?php echo $image;?>">

Show image, if it's set in database

I'm trying to show an image (or rather a link to an image) stored in a database and I'd like to get the image to show only if the link is set in the database.
Currently, if the link is not set (value is null), it shows a broken link.
Is there a way to for example use an if-statement and echo a HTML-code?
Something like this:
(The value have been fecthed to array $current in this example:)
<?php
if(isset($current['image']) {
echo "<img src='<?php echo $current['image'];
?>' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'})">
You used <?php twice, you have problem with quotes, brackets, etc.
<?php
if (!empty($current['image'])) {
echo "<img src='" . $current['image'] . "' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'>";
} else {
// here you can write for example default no-image image or whatever yo want, if you want
}
Nevermind, got it.
-Solution:
<?php if(isset($current['image'])): ?><img src="<?php echo $current['image']; ?>" class="left" style="max-height:20em; max-width:15em; margin-right:1em; margin-top:0;})">
<?php endif; ?>
<?php
if(isset($current['image'])) {
?>
<img src='<?php echo $current['image'];?>' class='left' style='max-height:20em; max-width:15em;
margin-right:1em; margin-top:0;'>
<?php
}
?>

Content showing PHP

I have a problem in a file : movies.php
I want to show all movies on the files when there is no id, and if the id exists, i want to show the movie with that id , i used :
echo "<div id='head'>$title</div>";
echo "<div id='bodyar'>$content</div> <br />
<hr>Category : <span class='date'>$moviecategory</span></hr>
<hr>Views : <span class='date'>$views_numimg</span></hr>
<hr></hr> <br />"; exit;}
$orderposts = mysql_query("select * from movie ");
echo "<div class='bodypanelposts'>";
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo "<div id='movieall'><table id='classing' border='0'
cellspacing='2'><tr> <td>";
echo "<a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><br /></div><div class='movies'>$title</div></a><br />LIKE BOX GOES HERE</tr></td></table></div>";
}
The problem is , after using that , the footer is not appearing anymore ..
I want it to appear.
To let PHP know it has to start interpret the code, you need start tags:
<?php
// PHP code here
?>
You should also concat variables by a dot instead of putting into the quotes:
echo "<div id='head'>" . $title . "</div>";
(Some might say this is not important but it is IMO, PHP can't handle it properly in every case.)
When using exit;, you tell PHP to quit and flush the result to the browser.
There is also a closing } bracket after the exit, but I don't see any opening { bracket.
A better way to handle your HTML is to do it like this:
<div id='head'><?=$title?></div>
<div id='bodyar'><?=$content?></div>
<br />
<table>
<tr><td>Category</td><td><span class='date'><?=$moviecategory?></span></td></tr>
<tr><td>Views</td><td><span class='date'><?=$views_numimg?></span></td></tr>
</table>
<div class='bodypanelposts'>
<?php
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo <<<HTML
<div id='movieall'>
<table id='classing' border='0' cellspacing='2'>
<tr><td><a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><div class='movies'>$title</div></a>
<br />LIKE BOX GOES HERE
</td></tr>
</table>
</div>
HTML;
?>
</div>
Notice the <?= tags to do inline PHP echo statements, allowing you to write HTML without having to wrap them in echo statements.
You can also use HEREDOC syntax to echo out a large chunk of HTML with variables inline.
These two methods make it much easier to reason about what your code is outputting.

Hide non-existing image from page using echo function

I have the following line:
<td><img src="Photos/<? echo $rows['photo1']; ?>" height="200" /></td>
I don't always have a photo. I would like to hide the image space. It looks like it is broken or if the url is wrong.
<td><?php echo (!empty($rows['photo1']) ? '<img src="Photos/' . $rows['photo1'] . '" height="200" />' : '') ?></td>
To have no <img> tag appear for blank values, I'd use something like this:
<?php if (($rows['photo1'] !== "") || ($row['photo1']))
{
echo "<td><img src='Photos/" . $rows['photo1'] . "' height='200' /></td>";
}
?>
if (image is not blank, and it exists) {
then, echo the img tag and the variables
}
Alternatively, If you want to display a different image for blank values:
<td>
<img src="Photos/<?php if ($rows['photo1'] !== ""){echo $rows['photo1'];}
else {echo "defaultimg.jpg";}?>" height="200" />
</td>
Hope this helps :)
Perhaps this is more to your liking:
<img src="Photos/<? echo $rows['photo1']; ?>" height="200" onerror="this.style.display='none'" />
It sets the image to not show when there is a problem (such as a broken URL).
This has the advantage of avoiding ternaries, separates PHP from HTML, and keeps things clear without excessive documentation. Hope it helps.

Categories