user8054284
user8054284

Reputation:

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;?>">

Upvotes: 0

Views: 1874

Answers (6)

user4419336
user4419336

Reputation:

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();

Upvotes: 1

schellingerht
schellingerht

Reputation: 5796

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

Upvotes: 2

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

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

Upvotes: 1

Bilal Ahmed
Bilal Ahmed

Reputation: 4066

<?php $image = "<img src='../../images/stoel.jpg' alt='img' />";  ?>

you try like this

<?php echo $image; ?>

Upvotes: 1

Autista_z
Autista_z

Reputation: 2541

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;?>">

Upvotes: 1

Carl Binalla
Carl Binalla

Reputation: 5401

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;?>">

Upvotes: -1

Related Questions