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>';
?>
Related
What I am trying to do is something like below:
The buttons at the side can be ignored. So the idea is that the big square is the main video playing and the little ones underneath will be the thumbnails of the remaining videos. The main video playing is randomly picked when the site is loaded and then the ones that are not playing, the thumbnails will be shown underneath. I am doing this like so:
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
echo $video;
array_push($items, $video);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $items[0];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
foreach($restVideos as $videoThumbnail) {
try {
$stmt = $dbconn->query("SELECT thumbnail, videoTitle FROM videos WHERE video = '$videoThumbnail'");
while($row = $stmt->fetch()){
$thumbnail = $row['thumbnail'];
array_push($thumbnails, $thumbnail);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
}
foreach($thumbnails as $nonPlaying) {
?>
<img src=<?php echo $nonPlaying ?> id="thumbnails" width="200" height="100">
<?php } ?>
what is happening at the moment is below:
Each image is just stacking up against each other how can I change it so this is not the case?
Also is there a more efficient way of doing this than what I am currently doing?
Edit:
$thumbnails = array();
$videoTitles = array();
$test = 700;
echo "<div>";
foreach($restVideos as $rVideo) {
$test = 200 + $test;
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100" style= "left: <?php echo $test?>px">
<?php
echo "</div>";
}
You not required to two query two times to db .
Use key value and store the entire video. While printing thumbnail based on the key (unique id ex. videoid). You can retrieve the content when you print the thumbnail list and can avoid querying again in a loop.
Ref to code snipp
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoMap[$row['videoid']] = $row;
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
if (count($items) == 0) echo $video;
array_push($items, $row['videoid']);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $videoMap[$items[0]]['video'];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
echo "<div>";
foreach($restVideos as $rVideo) {
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100">
<?php
}
echo "</div>";
}
?>
Use a div tag and print the image thumbnails.
I've used the below code to store and get the images from the backend. The images are getting stored but it is not getting converted to base64 format...
$fruit_img = addslashes($_FILES['fruit_img']['tmp_name']);
$name = addslashes($_FILES['fruit_img']['name']);
$fruit_img = file_get_contents($fruit_img);
$fruit_img = base64_encode($image);
while($row = mysqli_fetch_array($res))
{
echo '<img height ="300" width="300" src="data:image/jpg;base64,'.$row[2].' "> ';
echo $row[0];
}
I presume your code represents storing the data here:
$fruit_img = addslashes($_FILES['fruit_img']['tmp_name']);
$name = addslashes($_FILES['fruit_img']['name']);
$fruit_img = file_get_contents($fruit_img);
$fruit_img = base64_encode($image);
And retrieving the data here:
while($row = mysqli_fetch_array($res))
{
echo '<img height ="300" width="300" src="data:image/jpg;base64,'.$row[2].' "> ';
echo $row[0];
}
If that is the case, you have simply mislabeled a variable in this line:
$fruit_img = base64_encode($image);
$image has not been assigned any value previously.
$fruit_img = base64_encode($fruit_img);
If that is not the case, please post additional (complete insert and read) code.
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'> ";
I'm trying to display image in 5 by 3 table.
I'm able to display the images if all empty(blank.png).
Here is the code
<?PHP
$ds ='\image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
echo "<pre>"; print_r($imagefile);
$file = 'blank.png';
$d = $ds.$file;
echo "<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">";
for($row=1;$row<=5;$row++){
echo "<tr>";
for($col=1;$col<=3;$col++){
// echo"<td height=60px>W$row</td>";
//if()
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
echo "</tr>";
}
echo "</table>";
?>
I want to display the images base on middle file name array $imagefile eg W1, W2 and if not in array, I will display the blank.png.
I was able to get the middle file name by this code, but I cannot display the images in correct row/col.
for($i=0;$i<count($imagefile); $i++) {
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
}
Can you try this,
Based on your code add these when you echo the image,
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
if($wd == *the increment either row or col*)
{
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
else
{
echo"<td height=60px>No image</td>" .PHP_EOL;
}
See if it works.
Here it is
<?php
$ds ='/image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
//echo "<pre>"; print_r($imagefile);
$default = 'blank.png';
?>
<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">
<?php
for($row=1;$row<=5;$row++){
?>
<tr>
<?php
for($col=1;$col<=3;$col++){
// construct the file name
$filename = 'EX_W' . $row . '_0' . $col . '.png';
// set the default image file
$imgPath = $ds . '/' . $default;
// in case the file name exists in your array with images,
// set the correct path to the image
if (in_array($filename, $imagefile)) {
$imgPath = $ds . '/' . $filename;
}
?>
<td height=60px>
<img border="1" height="120" width="120" src="<?php echo $imgPath; ?>"/>
</td>
<?php } ?>
</tr>
<?php
}
?>
</table>
As you can see I also prefer to "embed" the php code in the HTML. It makes no sense to me outputting HTML code through the PHP engine if it can be parsed as is ;-)
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
?>