in php how to call img with user id - php

img name is $user->id.jpg or 543.jpg
<?php
$logo = "<img src='images/jbjobs/$user->id.jpg'>";
?>
after here
$logo = "<img src='images/jbjobs/
I dont know what to do and I have to escape ' or ".

Any of the following will do:
$logo = "<img src='images/jbjobs/{$user->id}.jpg'>";
$logo = "<img src='images/jbjobs/".$user->id.".jpg'>";

Try
<?php
$logo = "<img src='images/jbjobs/{$user->id}.jpg'>"; // {} is like concatenate
?>

Related

Unable to display images from a comma separated string

I'm trying to display images from database but unable to show .
Please help me.
My code
$id = $_GET['reg_id'];
$sql13 = "select * from contacts where reg_id=" . $id;
$result13 = mysqli_query($conn, $sql13);
if (mysqli_num_rows($result13) > 0) {
while($documents = mysqli_fetch_array($result13))
{ ?>
<li class="make_text1" style="font-size:16px">
<span class="definition"><b>
<?php if(!empty($documents["name"])){ echo
$documents["name"]; }?><br><?php
if(!empty($documents["image"])){
$upload_dir = 'uploads/';
// echo "<img src='uploads/".$documents["image"]."' width='800' height='500'> ";
echo "<img src='uploads/".$documents["reg_id"]."/".$documents["image"]."' width='800' height='500'> ";
}
You need to loop over the values in $documents["image"]. Try this:
$images = explode(',', $documents["image"]);
foreach ($images as $image) {
echo "<img src='uploads/".$documents["reg_id"]."/$image' width='800' height='500'> ";
}
or if you only want do display one of them e.g. the first, something like this:
$images = explode(',', $documents["image"]);
echo "<img src='uploads/".$documents["reg_id"]."/".$images[0]."' width='800' height='500'> ";

Displaying default image when there is no image to display

I am having problem with displaying default image. I do not know where exactly to place it.
$result = $conn->query("SELECT * FROM adoption;");
if($result->num_rows !=NULL){
while($rows = $result->fetch_assoc()) {
$AAnimalName = $rows['AAnimalName'];
$Abreed = $rows['Abreed'];
$Asex = $rows['Asex'];
$Acolor = $rows['Acolor'];
$image = $rows ['image'];
?>
<div class="container-custom1">
<?php echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>
<?php echo "<i><h1 class='junction'><a style='cursor:pointer' class='junction'>".$AAnimalName."</a></h1></i>"."<br>".$Asex." /".$Abreed."<br>".$Acolor."<br>"?></div>
<?php
}
}
try this inside while loop :
$image_location = "admin/function/upload/".$image;
if(file_exists($image_location )) {
echo '<img src = "'.$image_location .'" width = "248" height="190" class="age1" title>';
}
else {
echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}
Change the line like this:
<?php echo '<img onerror="this.src=\'img/logo.png\'" src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>
img/logo.png would be the default image
Try this:
If(file_exist('your_file_path'))
{
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>';
}
else
{
echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}
This will check if the file exist or not. If exist, it will display given file. Otherwise a default file.
$image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;
Use ternary operator and set image
<?php
$image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;
$image_location = "admin/function/upload/".$image;
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>';
?>

If image exists show else hide it

I have the following situation.
If there isn't an image in the DB, the page it's on shows a big image placeholder. What is the best way to hide the image placeholder if an image doesn't exist?
<img src="<?php echo '../img/artists/' . $row_rsAccents['artistPhoto']; ?>" width="100%"/>
http://westerndesignconference.com/intheloop/
You can do this with a simple if/else statement like so:
//I prefer to set things with variables
$placeholder_img = "../img/artists/placeholder.jpg";
$db_img = $row_rsAccents['artistPhoto'];
if($db_img){
$img_src = $db_img;
} else {
$img_src = $placeholder_img;
}
echo "<img src='$img_src' alt='' width='100%' />";
If there is a value returned - show an image. If the condition fails, no <img> will be displayed, preventing the blank gap
if (isset($row_rsAccents['artistPhoto'])) {
echo '<img src="../img/artists/' . $row_rsAccents['artistPhoto'] . '" width="100%"/>'
}
if (file_exists('artist.jpg') {
echo "<img src='artist.jpg'>";
}
else {
echo "<img src='default.jpg'>";
}

HTML Iframe get src with php

I have an webpage where I want to have another html page displayed. I used iframes. The page gets to know what to load is via the get procedure. But there is an fault in this coding I think...
<iframe src="
<?
$file = ($_GET['ti'])
if ($title = '')
echo "information.html";
else echo "$file";
?>
"></iframe>
The url the page would recieve looks like this:
http://www.website.com/reference.html?ti=unlimited.html
http://www.w3schools.com/php/php_if_else.asp
It's your if / else syntax and over all php code. It's not very well written.
<?php
$file = $_GET['ti'];
if ($title = '') {
echo "information.html";
} else {
echo "$file";
}
?>
Need semicolon:
$file = ($_GET['ti']);
Use empty(), like this:
<iframe src="
<?
$file = ($_GET['ti'])
if (empty($title))
echo "information.html";
else echo $file;
?>
"></iframe>
<?php
$possible = array("information.html", "home.html", "test.html");
$file = isset($_GET['ti']) &&
in_array($_GET['ti'], $possible)? $_GET['ti'] : "information.html";
?>
<iframe src="<?php echo $file;?>"></iframe>

quotes in html in php problem

I am trying to generate a specific link and accompanying html depednant on the existance of a file. The code I am using to do so:
if(file_exists('../images/'. $pk . '.jpg'))
{
$imageSrc = "../images/". $pk . ".jpg";
$imagehtml = htmlentities(json_encode("<img src=\"".$imageSrc."\" >"));
$screenshotLink = "<p>View Screenshot";
}
else {
$screenshotLink = '';
}
This results in the following, useless html:
View Screenshot
I don't understand this, because the above is essentialy the same code as:
$html = htmlentities(json_encode($ARTICLE_DESC));
$imagehtml = htmlentities(json_encode("<img src='".$imageSrc."' >"));
echo "<a href='#' onclick=\"makewindows(" . $imagehtml . "); return false;\">
<img src='".$imageSrc."' width='".$imageSize["width"]."' height='".$imageSize["height"]."'></a>
<p>Click for full description </p>";
which produces the following html which works fine:
<a href="#" onclick='makewindows("<img src=\"..\/images\/160329461329.jpg\" >"); return false;'>
<img src="../images/160329461329.jpg" width="199" height="300"></a>
I know it has something to do with quotes, but I am not sure what exactly.
Try this:
$imagehtml = htmlspecialchars(json_encode("<img src=\"".$imageSrc."\" >"), ENT_QUOTES);
$screenshotLink = "<p>View Screenshot";
$imagehtml = htmlspecialchars(json_encode('<img src="'.$imageSrc.'" >'), ENT_QUOTES);
$screenshotLink = '<p>View Screenshot';
Why not use ticks?
Lookup the ENT_NOQUOTES parameter in the php manual
And htmlspecialchars() != htmlentities() btw.

Categories