How to display image path dynamically? - php

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']

Related

display images with online source in foreach loop in php, repeating loop issue

I have A problem While displaying Images using foreach loop in php.
I am Passing Online http:// Source To The Image Tag
But My loop execute Only For One Time.
here is my code:
$popularmovies contain multiple values But loop execut only once.
<?php foreach ($popularmovies as $popmovies); ?>
<img class="img-responsive" src="<?php echo 'http://'.$popmovies-
>Movie_cover; ?>" alt="" width="210" height="315" >
<? endforeach; ?>
this loop show only one image. but does not repeates.
help me please
Try this:
<?php foreach ($popularmovies as $popmovies): ?>
<img class="img-responsive" src="<?php echo 'http://'.$popmovies-
>Movie_cover; ?>" alt="" width="210" height="315" >
<?php endforeach; ?>
...also check your $popularmovies array for length.

I'm trying to echo an image in a PHP variable in CodeIgniter

Hello I'm trying to echo an image on a view page in CodeIgniter but nothing is displayed on the page.
Here I'm making the variable:
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
And here I'm trying to echo the image:
<img src="<?php echo $image;?>">
Intro
This is the most basic php, so what's the addition of this question? Please read the basics of echo here: http://php.net/manual/en/function.echo.php (Example 1).
Learn the basics first!
Solution
1. Assign full html tag to variable and echo full html:
<?php
$image = '<img src="../../images/stoel.jpg" alt="Foo">';
echo $image;
?>
2. Or assign image path to variable and echo concat string:
<?php
$path = '../../images/stoel.jpg';
echo '<img src="' . $path . '" alt="Foo">';
?>
3. Or assign image path to variable and echo only this with php:
<?php
$path = '../../images/stoel.jpg';
?>
<img src="<?= $path; ?>" alt="Foo">
You are inserting full Image tag into src attribute.
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
and
<?php echo $image; ?>
or
<?php $image = "../../images/stoel.jpg"; ?>
and
<img src="<?php echo $image;?>">
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
you try like this
<?php echo $image; ?>
You are already assign full image code in variable so you just need to print your variable:
<img src="<?php echo $image;?>">
To
<?php echo $image;?>
Make sure your images out side of the application folder then you can do something like
application
images
images > stoel.jpg
system
index.php
Use Base url from the url helper
<img src="<?php echo base_url('images/stoel.jpg');?>" />
Make sure you have set the base_url in config.php
$config['base_url'] = 'http://localhost/yourproject/';
You can also use HTML Helper img();
I think this is what you mean. Since you are echoing inside the src attribute, you do not need to store the whole <image>, just the path will do.
<?php
$image = "../../images/stoel.jpg";
?>
<img src="<?php echo $image;?>">

Display images in codeigniter with full_file path

I'am using codeigniter file upload class to upload images and save full_path in to the database.
But when displaying the images i can't see it.
Here is my code
<?php foreach($images as $img) {?>
<img src="<?php echo $img;?>" width="150">
<?php }?>
From view-source i will open the image link it will mixed with base-url and image full_path.
http://localhost/holiday/admin/profile/package_explained/C:/xampp/htdocs/holiday/uploads/package-images/img-holoday-sample.png
the value of $img is (ie fullpath of the image) C:/xampp/htdocs/holiday/uploads/package-images/img-holoday-sample.png.
How can i display image with full_path?
Only get image name form Controller. Do not track full path.
Bcz your host server doesn't have path like this C:/xampp/htdocs/
<?php foreach($images as $img)
{
?>
<img src="<?php echo base_url()?>uploads/package-images/<?php echo $img;?>" width="150">
<?php
}
?>
Assume that you are not saving the path like that you have mentioned in DB:
Try this (add http:// before print):
<?php foreach($images as $img) {?>
<img src="http://<?php echo $img;?>" width="150">
<?php }?>

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

PHP echo function inside a HTML link

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"/>

Categories