PHP spits actual HTML instead of image - php

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
}
?>

Related

Style the label in php

Scenario :
To Style label
Here is my code to view status
<?php
if ($shift['isActive'] == 1) {
echo 'Active';
} else {
echo 'Inactive';
}
?>
Problem :
Is there have any way to style the label Active & Inactive separately(background color).
Try This
if ($shift['isActive'] == 1) {
echo "<lable style='background-color: green;'>Active</lable>";
} else {
echo "<lable style='background-color: red;'>Inactive</lable>";
}
Use this code:
<div class="<?php $shift['isActive'] == 1 ? 'Active' : 'Inactive' ?>">
something
</div>
<label class="<?php if ($shift['isActive'] == 1) { echo 'Active'; } else {echo 'Inactive'; } ?>">
</label>

PHP Echo Empty Check with Photos

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.

If else php statement

If there is an image I want it to show along with the content, if there is no image for the content, then show nothing. Currently, with the code below if there is no image then I get a broken image on my page. If there is an image it works great. Any suggestions would be much appreciated. Thank you.
<?php if ($totalRows_rsPress > 0) { ?>
<img class="img-responsive" src="images/<?php echo $row_rsPress['image']; ?>" alt="Image text" />
<?php } elseif ($totalRows_rsPress == 0) { return "" ; } ?>
Try this:
<?php if ($totalRows_rsPress > 0 && isset($row_rsPress['image']) && strlen($row_rsPress['image']) > 0) { ?>
<img class="img-responsive" src="images/<?php echo $row_rsPress['image']; ?>" alt="Image text" />
<?php } elseif ($totalRows_rsPress == 0) { return "" ; } ?>
Check if the image exists:
if ( ! empty($row_rsPress['image']) ) {
echo '<img class="img-responsive" src="images/' . $row_rsPress['image'] . '" alt="Image text" />';
}
<?php if($totalRows_rsPress > 0) : ?>
<img class="img-responsive" src="images/<?=$row_rsPress['image'];?>"/>
<?php endif; ?>
There was no need for the elseif statement.
Without knowing what totalRows_rsPress is, I can't tell you why it might be failing that if condition..
Try using count, i.e.:
if (count($totalRows_rsPress) > 0) {
echo <<< EOF
<img class="img-responsive" src="images/{$row_rsPress['image']}" alt="Image text" />
EOF;
}
<?php
try{
if ($totalRows_rsPress > 0) {
echo "<img class=\'img-responsive\' src=\'images/$row_rsPress['image']\' alt=\'Image text\' />"};
} elseif ($totalRows_rsPress == 0) { return "" ; }
}
catch(err) {
//do nothing or do in case 004
}
?>

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

Thumbs up or thumbs down on PHP

what i'm basically trying to do is make a 'thumbs up' appear if the user scores >= to 2 and shows a 'thumbs down' if the user scores < 2
Here is the code i tried to use..
<?php
//Recieves form..Form ID
$fid = $_GET['id'];
//Recieves answers
$answer1= $_POST['answerOne'];
$answer2= $_POST['answerTwo'];
$answer3= $_POST['answerThree'];
$score=0;
?>
<?php //Gets thumbs up if did well, gets thumbs down if not so good
if ($score>=2){
echo "<center><img src='Images/thumbsup.png' height='295' width='295' /> </center>";
}
elseif ($score<2) {
echo "<center><img src='Images/thumbsdown.png' height='295' width='295' /> </center>";
}
?>
<body>
<!--Answers for Quiz 1-->
<?php
if ($fid == 1){
if ($answer1 == "B") {$score++;}
if ($answer2 == "B") {$score++;}
if ($answer3 == "A") {$score++;}
}
?>
<?php
if ($fid == 1){
echo "
<p id='YourScore'> Your score is: </p>
<p id='YourScore'>$score/3 correct answered </p>";}
?>
What happens is that it always show up 'thumbs down'. I think its because $score is equals to 0 and what its NOT doing is using the incremented value after adding the score as the value to use to define whether to give a thumbs up or down.
Any help would be appreciated.
As mentioned in my comment: you calculate score after your thumbs, so turn those around:
<?php
//Recieves form..Form ID
$fid = $_GET['id'];
//Recieves answers
$answer1= $_POST['answerOne'];
$answer2= $_POST['answerTwo'];
$answer3= $_POST['answerThree'];
$score=0;
?>
<body>
<div id="Wrapper">
<div id="Container">
<!--Answers for Quiz 1-->
<?php
if ($fid == 1) {
if ($answer1 == "B") {$score++;}
if ($answer2 == "B") {$score++;}
if ($answer3 == "A") {$score++;}
}
?>
<?php
//Gets thumbs up if did well, gets thumbs down if not so good
if ($score >= 2) {
echo "<img src='Images/thumbsup.png' height='295' width='295' /> ";
}
elseif ($score < 2) {
echo "<img src='Images/thumbsdown.png' height='295' width='295' /> ";
}
if ($fid == 1) {
echo "<p id='YourScore'> Your score is: </p>
<p id='YourScore'>$score/3 correct answered </p>";
}
?>
Thats because you aren't checking the answers therefore the score will always be 0 and elseif ($score<2) will always be true.
You have to check if the answers where right BEFORE printing the 'thumbs'

Categories