22196

How to load Images in Codeigniter?

Question:

Hey i am totally new in Codeigniter. In the Controllers folder I created a file named caller.php and I created a file home1.php in \Views. In root directory i created an image folder named \Images and also created a css folder named \css. In images Folder there are 6 picture. In css folder style.css file exist.

In caller.php i write

<? class caller extends CI_Controller { function index() { $this->load->view('home1'); // what i have to write here lo load images.... } }

In home1.php i write

<html> <head> <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css"> // what i have to write here to load images </head> <body> <div id="outer"> <div id="container"> <div id="images"> <img src="new.jpg" width="960" height="400"/> <img id="image1" src="1.jpg" /> <img id="image2" src="2.jpg" /> <img id="image3" src="3.jpg" /> <img id="image4" src="4.jpg" /> <img id="image5" src="5.jpg" /> </div> <div id="slider"> <a href="#image1">1</a> <a href="#image2">2</a> <a href="#image3">3</a> <a href="#image4">4</a> <a href="#image5">5</a> </div> ................................................... .................................................... </div> </div> </body> </html>

Now for the above code how i load the images .Please experts help me. if additional config's are needed please mention that.

Answer1:

As you're already using the url helper I suggest wrapping your image src attributes with base_url, such as:

<img src="<?php echo base_url('images/1.jpg'); ?>" />

And as mentioned in the other answer it's best (mandatory?) to capitalize the class name in your controller

class Caller extends CI_Controller { ...

Answer2:

First of all, you need to capitalize your class name.

class Caller extends CI_Controller { public function index() { // method code goes here } }

Then, you need to link to those images with absolute links or with the CI URL helper: "/images/1.jpg" and so on. <strong>How to use with the CI URL helper is detailed here</strong> : <a href="https://www.codeigniter.com/userguide3/general/helpers.html" rel="nofollow">Helper</a>

EDIT

Load the URL helper with this in your constructor method:

$this->load->helper('url');

You can create a URL like this:

echo base_url("blog/post/123");

That will make:

http://example.com/index.php/news/local/123

Or

http://example.com/news/local/123

If you've taken out the index.php in your config file.

Here's a class with the constructor that calls the URL helper:

class Caller extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url'); } public function index() { // method code goes here } }

Answer3:

You can do the same for as you did to Load the CSS files.<br /> Create a folder in Root Directory. Create a sub folder(if you have Multisite)<br /> then point your base_url in config file to your base path.

and then do the same<br /><img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />

Answer4:

Only simple way

<img id="image1" src="<?php echo base_url('1.jpg'); ?>" />

That sit!!!

Answer5:

Make folder in root directory and code easily in views html file.

if directory name is directoryName and image name imageName.jpg

you can call like this.

<img src="directoryName/imageName.jpg">

Recommend

  • Why doesn't the GCC assembly output generate a .GLOBAL for printf
  • Google Text To Speech as a sperate class that can be called when ever needed?
  • Expand in the same as
  • Codeigniter 2.1.4 installation in sub directory
  • How to insert arrays into MySQL using Codeigniter?
  • java.io.EOFException: End of input at line 1 column 1
  • How can I change config file path in Codeigniter?
  • Ransack search string arrays stored in db
  • NetBeans doesn't see style.css [duplicate]
  • How to add regEx in angular filter
  • pillow imaging ImportError
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Simulate click Geckofx vb,net
  • Adjust width of select element according to selected option's width
  • Loading .coffee files via a view in Rails
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Create DicomImage from scratch using Dcmtk
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Play WS (2.2.1): post/put large request
  • Jquery UI tool tip close icon
  • HTML download movie download link
  • MySQL WHERE-condition in procedure ignored
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Web-crawler for facebook in python
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Cannot Parse HTML Data Using Android / JSOUP
  • trying to dynamically update Highchart column chart but series undefined
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?