0

I made a header view file and in that header I'm trying to echo a picture from the folder images. This images folder is outside the application folder. Also the name of the picture is logo.jpg

This is the code to echo the picture in the header.php file:

<nav class="nav-bar">
      <img class="logo" src="../../images/logo.jpg" alt="Jongeren kansrijker logo"/>
     </nav>

On another view page called home I'm including the header like this:

<?php   include_once ('templates/header.php');  ?>

When I load the home.php view page the picture does not load. It does echo the alt of the picture: alt="Jongeren kansrijker logo

What am I doing wrong here and how can I echo the logo.jpg picture from the images folder?

Jeremy
  • 35
  • 6

3 Answers3

2

First of all you should load url helper in the autoload.php which is in config folder as following.

$autoload['helper'] = array('url');

Second, you should use $this->load->view ('templates/header'); instead of including files using include_once();

Third, You should config your baseurl in config.php which is also in config folder as below(considering you are working in localhost)

$config['base_url'] = 'http://localhost/projectName/';

The above code takes you to the root of your project.

And the last point to view the image use the following code

<img class="logo" src="<?= base_url('images/') ?>logo.jpg" alt="Jongeren kansrijker logo"/>
Rishi
  • 408
  • 4
  • 13
1

use an absolute path with base_url();

<img class="logo" scr="<?= base_url();?>/images/logo.jpg" />

you can set your base_url in the config.php and it should target the folder where your index.php is in (And I guess the image-folder is also there).

jonas3344
  • 201
  • 1
  • 7
  • I changed it with this line now: and it shows a blank page now.. really weird. My base URL is set correctly like this: $config['base_url'] = 'http://localhost/codeigniter'; – Jeremy Mar 15 '18 at 15:47
  • it shows a blank page because you probably don't show errors? You should change that. You need to load the url-helper to be able to use base_url(). In config/autoload.php you have to add 'url' to $autoload['helper'] – jonas3344 Mar 15 '18 at 15:50
  • How do I change that? – Jeremy Mar 15 '18 at 15:51
  • https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display And please see my edit – jonas3344 Mar 15 '18 at 15:52
0

This usually means your src attribute is wrong. Remember that the value should be the path from the root of your website as it is managed as a separate request by the browser.

I imagine in your case you're actually looking for the following:

<img class="logo" src="/images/logo.jpg" alt="Jongeren kansrijker logo"/>

TLDR: The image is fetched by the browser so you shouldn't use the path from header.php.

Jim Wright
  • 5,256
  • 1
  • 9
  • 30