PHP Echo Empty Check with Photos - php

i have issue with my php code .
First i have Database with Photo link then i add this with echo .
I Fill the Database with /upload/photo-1.png or what ever from the User !
<?php echo$photo; ?>
I add this with :
<?php if(!empty($photo)) {echo 'uploads/photodefault.png'; } ?>

you can write like this
<?php if(!empty($photo))
{
echo $photo;
}
?>
Edit 1:
If photo is not empty then it will show link.

Change your code to this
<?php
if(empty($photo)) {
echo "uploads/photodefault.png";
} else {
echo $photo;
}
?>
With an <img> tag example
<img src="<?php if(empty($photo)) { echo "uploads/photodefault.png"; } else { echo $photo; } ?>">

try the following code
if ($photo != ''){
echo $photo;
}else{
echo "uploads/photodefault.png";
}

<?php if(!empty($photo)) { echo $photo; } else { echo 'default/default_large.png'; } ?>
This fixed my issue.

Related

How to add a else command in php?

I have an attribute called "Merchant_Name".
The following code will place the "Merchant_Name" Just below the product name in product page.
Now I need to add an else command to this code like this:
If Merchant_Name is there then it should be "By Merchant_Name" else "XXXXX".
Hope you get me...
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){?>By <?php echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product); } ?>
</div>
Thanks in Advance...
You mean, with an if/else?
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name) {
echo "By " . $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo "By XXXXX";
}
?>
Or maybe this would be cleaner:
<?php
if($_product->getAttributeText('merchant_name')) {
$name = $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
$name = "XXXXX";
}
?>
<div class="Merchant_Name">By <?php echo $name ?>
Once you start the php tag try to finish your task. Don't switch in and out again and again.
<div class="Merchant_Name">
By
<?php
if ($_product->getAttributeText('merchant_name')){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>
It is pretty simple and it would be simpler if you didn't switch in and out of PHP so often:
<div class="Merchant_Name">
By
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>

Select a random image from a folder instead of the same default placeholder image

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 :)

PHP MYSQL <?php echo $row[$variable]; ?>

I have the following code that does what I want it to do, but the ones with the backslashes <?php echo $row[\'t_id\']; ?>, the <?php echo $row[\'t_type\']; ?> and the <?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?> do not output their respective variable.
Instead, their output is literal like
view_t_profile.php?tutor_id=<?php echo $row['t_id']; ?>&t_type=<?php echo $row['t_type']; ?>
The <?php echo ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']); ?> doesn't show up on the web page like the previous code, but its in the code italicized and in red text. I'm not a programmer/coder, so if someone can correct my code, I sure would appreciate it. Usually, I can figure it out, but on this one, I cannot.
<?php
if($row['t_type'] == 1)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Contact</strong></center>';
echo '<center><strong><font color="#3BB9FF"><?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?></font></strong></center>';
}
if($row['t_type'] == 0)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Sponsor</strong></center>';
echo '<center><strong><font color="#3BB9FF"><?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?></font></strong></center>';
}
?>
You don't use <?php echo ... ?> when you're already in PHP mode and echoing something. Just concatenate the variable.
echo '<center><strong><font color="#3BB9FF">' . ucfirst($row['t_fn']) . ' ' . ucfirst($row['t_ln']) . '</font></strong></center>';
<?php echo ... ?> is used when you're just outputting HTML directly, and you want to insert a bit of PHP. For instance, like this:
if($row['t_type'] == 1)
{ ?>
<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>
<center><strong>Main Contact</strong></center>
<center><strong><font color="#3BB9FF"><?php echo ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']); ?></font></strong></center>
<?php
}
Try this:
<?php
if($row['t_type'] == 1)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Contact</strong></center>';
echo '<center><strong><font color="#3BB9FF">'.ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']).'</font></strong></center>';
}
if($row['t_type'] == 0)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Sponsor</strong></center>';
echo '<center><strong><font color="#3BB9FF">'.ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']).'</font></strong></center>';
}
?>
When you open <?php once, you can't use this tags again, before you close with ?> tag.

PHP spits actual HTML instead of image

Having a small issue here but cannot figure out where I have gone wrong. I have the following code which should show an image depending on the condition but instead it shows the HTML in the browser
if ($this->id_status == 2)
{
$this->id_status = "<img src='../_lib/img/gray_dot.png' />";
}
elseif ($this->id_status == 1)
{
$this->id_status = "<img src='../_lib/img/green_dot.png' />";
}
elseif ($this->id_status == 3)
{
$this->id_status = "<img src='../_lib/img/orange_dot.png' />";
}
Can anyone help?
try this:
<?php
if ($this->id_status == 2)
{
?>
<img src='../_lib/img/gray_dot.png' />
<?php
}
elseif ($this->id_status == 1)
{
?>
<img src='../_lib/img/green_dot.png' />
<?php
}
elseif ($this->id_status == 3)
{
?>
<img src='../_lib/img/orange_dot.png' />
<?php
}
?>

Div not showing in wordpress

<?php
if (is_user_logged_in()) {
echo '<div id="signin-box"> ' . wp_login_form() . ' </div>';
} else {
echo 'hi';
}
?>
Is what I've got. The login form is working, but it is not being wrapped in the div. Without the else/if statement, it works.
Does this work for you?
<?php
if (is_user_logged_in()) {
?>
<div id="signin-box">
<?= wp_login_form(); ?>
</div>
<?php
} else {
echo 'hi';
}
?>
http://codex.wordpress.org/Function_Reference/is_user_logged_in states it returns true if logged in and false if not.
You are probably looking for:
if(!is_user_logged_in()){
echo '<div id="signin-box">' . wp_login_form(array('echo' => false)) . '</div>';
}else{
echo 'hi';
}
Adding ! inverses true to false so if user is not logged in show login form else say hi. Also adding array('echo' => false) will give a return instead of echoing, removing array('echo' => false) you'll have to put wp_login_form() on its own line without a echo:
if(!is_user_logged_in()){
echo '<div id="signin-box">';
wp_login_form();
echo '</div>';
}else{
echo 'hi';
}
You want to add this code it seems work like this please add this code and check it.
<?php
if (is_user_logged_in()) { ?>
<div id="signin-box" class="test"><?php wp_login_form()?> </div>
<?php } else {
echo 'hi';
}
?>
In your code "echo" is not working in login form ..

Categories