PHP echo function inside a HTML link - php

I have a PHP echo function inside of a HTML link, but it isn't working. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this.
<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>">
<img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" />
</a>

Turn
<?php $image=troll/GrannyTroll.jpg?>
into
<?php echo "troll/GrannyTroll.jpg"; ?>
?
Or provide more details on what you are trying to achieve.
Also, you might consider urlencode-ing some of those URL parameters.
Edit:
So you might try setting the variable beforehand:
<?php $image = "troll/GrannyTroll.jpg"; ?>
<img src="<?php echo $picture; ?>" width="100" height="94" />

So now i understand what you are trying to do.
One error is that you didn't enclose $image=troll/GrannyTroll.jpg with quotes like this:
$image = 'troll/GrannyTroll.jpg';
The second error is that you do it in the wrong order, you have to define $image first, before you use it.
That's what I believe you want to do:
<?php
$image = "troll/GrannyTroll.jpg";
?>
<img src="<?php echo $image; ?>" width="100" height="94"/>

Related

How to display image path dynamically?

I have the following code
<figure>
<?php if ($data[$i]->thumbnail) { ?>
<img src="<?php echo $img ?>" width="465" height="300" alt="news-report-juli19" title="" />
<?php } ?>
</figure>
I want to display images from mysql database dynamically,
the image_field_name=thumbnail.
I have 10 records in db and all have different images.
You should have to given the image path as well when you want to display.
<?php if ($data[$i]->thumbnail) { ?>
<img src="<?php echo base_url('path_of_img/$img'); ?>" width="465" height="300" alt="news-report-juli19" title="" />
<?php } ?>
If you use the Codeigniter framework, please try this.
In controller
$data['images'] = $this->model->get_images();
$this->load->view('view_name', $data);
In model you should make get_images() method that get image urls.
In view
<?php
foreach ($images as $value) {
echo '<img src="'.$value['thumbnail'].'" width="465"
height="300" alt="news-report-juli19" title="" />';
}
?>
Good luck.
use query variale with db colmn where images are present
use this in source img src
echo $query_variable['db_coln_name']

How to have image src a variable?

I want to keep src attribute in a variable. I am working on php and tried the following method but it doesn't display the image on html page.
Code:
<?php $path="C:/horizontal.jpg"; ?>
<image src="<?php echo $path; ?>" style="width:304px;height:228px" />
I think you can use this technique:
<?php $path="http://" . $_SERVER["SERVER_NAME"];?>
<img src="<?php echo $path.'/projectName/image.jpg'; ?>" style="width:304px;height:228px" />
Where:
$_SERVER["SERVER_NAME"]: to get the server name, for example: www.example.com.
Also you can use $_SERVER["SERVER_ADDR"], in this case you can get the address IP of your server, for example: "127.0.0.1".
I hope this information helps you.
Good Luck.
<?php $path="localhost/projectName/";?>
<img src="<?php echo $path.'image.jpg'; ?>" style="width:304px;height:228px" />

show another image when image is not available

i have a code which gets images from a database. I need to tell it to to display another image (which says no image available) if an image is not found. How would I do this????
Your suggestions would be very much appreciated
<a class="thumbimage" href="<?PHP mrd("$MyProductTitle", "$row[LID]", "$_GET[category]", "$rowxxx[MR]", "index.php?page=detail"); ?>"><img src="images/thumb/<?php echo "$row[IMAGENAME]"; ?>.jpg" border="1" /></a>
Try this:
<?php
$currentImage = "images/thumb/".$row[IMAGENAME].".jpg";
if(!file_exists($currentImage))
{
$currentImage = "PATH_TO_IMAGE_UNAVAILABLE";
}
?>
<a class="thumbimage" href="<?PHP mrd("$MyProductTitle", "$row[LID]", "$_GET[category]", "$rowxxx[MR]", "index.php?page=detail"); ?>"><img src="<?=$currentImage?>" border="1" /></a>
You can use onerror attribute.
Replace the error image (3331913_orig.gif) with yours error image on the code below:
<img src="{some_error_src}" onerror="this.onerror=null;this.src='http://availableservicesllc.weebly.com/uploads/2/2/3/9/22390468/3331913_orig.gif'">
Click here to see this example

Displaying an image using a php variable

I'm trying to display images by taking their file path from an sql table, but i'm having a lot of troubles.
Here is whats going on:
$image is a variable containing the text "itemimg/hyuna.png" which is path to an image.
$image = 'itemimg/hyuna.png';
I assumed I would be able to display the image outside of the php block like so:
<img src= "<? $image ?>" alt="test"/>
This doesn't work though for some reason.
So I thought maybe it's not able to read the variable outside the php block(i'm a beginner), so for testing i did:
<h1> "<? $image ?>" </h1>
It displays itemimg/hyuna.png as a h1 banner.
Meaning it's accessing the varible fine.
So I thought maybe the path is wrong. So I tried:
<img src= "itemimg/hyuna.png" alt="test"/>
This displays the image perfectly.
So now I'm stuck scratching my head why the first bit of code displays nothing but the text "test" from "alt="
Extra question:
How do I go about assigning a value from an sql cell to a variable?
I attempted the following with no luck:
$q = "select * from item where id=$id";
$results = mysql_query($q);
$row = mysql_fetch_array($results, MYSQL_ASSOC);
$image = ".$row['image'].";
item is a table with a collumn: image which contains file paths to images
First of all, you should not use PHP Shorttags.
When you use the PHP Shorttags you have to say:
<img src="<?=$image ?>" alt="test" />
But i would encourage to escape the Content off the variable like this:
<img src="<?php echo htmlspecialchars($image); ?>" alt="test" />
Your extra question:
This should lead to an syntax error because the string could not be parsed, just use $image = $row['image'];
Try this
<img src= "<?php echo $image ?>" alt="test"/>
try
<img src= "<?= $image ?>" alt="test"/>
or
<img src= "<? echo $image; ?>" alt="test"/>
try this
<img src= "<?php echo $image ?>" alt="test"/>

Error displaying blob image in php/html

I am trying to display image from a blob field of a MySQL table. Looks like I have some sort of error in the following line. As soon as I put "header("Content-type: image/jpeg")" things get messed up and instead of displaying webpage, all source code of the page is displayed.
Please let me know how to correct.
<div class="image" align="left">
<a href="<?php header("Content-type: image/jpeg"); echo $rec['image']; ?>">
<img src="<?php echo $rec['image']; ?>" width="150" border="0"/>
</a>
</div><!-- image -->
You normally don't put the actual image contents in the src= attribute of the image tag. Instead, you point to the URL of an image file.
(There are ways to include the image source directly in the HTML, but it doesn't work consistantly with all browsers, and you still won't have your <a> link working properly.
Instead, the best way to do this is to create a separate PHP file to serve the image.
Your HTML:
<div class="image" align="left">
<img src="myimage.php?key=<?php echo($key) ?>" width="150" border="0"/>
</div><!-- image -->
myimage.php:
<?php
header("Content-type: image/jpeg");
$key = $_GET['key'];
// todo: load image for $key from database
echo $rec['image'];
You're trying to put the image data inline inside the content. The only feasible way to do this is via a Data URI data URI. Something like:
<img src="data:image/jpeg;base64,<?= base64_encode($rec['image']) ?>" width="150" border="0" />
However, what you probably want to do is put it into a separate script. So your HTML would be:
<img src="showimage.php?id=XXX" width="150" border="0" />
And your showimage.php script would be:
<?php
// Get $rec from database based on the $_GET['id']
header('Content-Type: image/jpeg');
echo $rec['image'];
?>
I've done something like that retrieving blob from my database in another way that you may find useful, here is the code example.. see if it suits your needs and if you needed anymore help let me know.
while ($row = mysql_fetch_array($hc_query2)) {
$title = $row['title'];
$text = $row['text'];
$image = $row ['image'];
$output ='<div class="HCInstance"><img src="data:image/jpeg;base64,' . base64_encode($image) . '" alt="High Council" width="100px" height="100px"/>
<div class="HCHeader"><h2>'.$title.'</h2></div><br/><div class="HCDetails"><p>'.$text.'</p></div></div>';
echo $output;
}

Categories