2012-11-19 39 views
1

嘿,我是全新的Codeigniter。在Controllers文件夹中,我创建了一个名为caller.php的文件,并在Views中创建了一个文件home1.php。在根目录下我创建了一个图片文件夹名称Images并且还创建了一个名为css.In的图片文件夹,其中有6张图片。在css文件夹中存在style.css文件。如何在Codeigniter中加载图像?

在caller.php我写

<? 
class caller extends CI_Controller 
{ 
function index() 
{ 
$this->load->view('home1'); 

// what i have to write here lo load images.... 

} 
} 

在home1.php我写

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

现在上面的代码我如何加载图像。请专家帮我。 如果需要额外的配置,请提一提。

+0

就像你在css的情况下一样。完全一样。只需将链接中的名称和html标签更改为img。 – itachi

回答

4

,如:

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

而且在对方的回答中提到这是最好的(强制?)在你的控制器大写类名

class Caller extends CI_Controller { ... 
2

首先,你需要大写你的类名。

class Caller extends CI_Controller { 

     public function index() 
     { 
      // method code goes here 
     } 

    } 

然后,您需要链接到带有绝对链接或CI URL助手的图像:“/images/1.jpg”等等。如何使用CI URL助手使用在这里详细描述:http://ellislab.com/codeigniter/user_guide/helpers/url_helper.html

编辑

装入URL助手本在你的构造方法:

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

您可以创建这样一个URL:

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

这将使:

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

或者

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

如果你已经采取了index.php文件在您的配置文件。

下面是与调用的URL帮手的构造函数的类:正如您已经使用的网址助手,我建议包装与BASE_URL您的图片src属性

class Caller extends CI_Controller { 

     public function __construct() 
     { 
      parent::__construct(); 
      $this->load->helper('url'); 
     } 

     public function index() 
     { 
      // method code goes here 
     } 
    } 
+0

请在这里发布答案,而不是链接它,以避免链接腐烂。 –

+0

如果你稍微解释一下,它会对我更有帮助。 –

+0

在您的控制器中,您需要添加一个构造函数方法来调用URL库。我编辑了答案给你看。 – Kenzo

0

您可以像加载CSS文件一样执行相同的操作。
在根目录中创建一个文件夹。创建一个子文件夹(如果你有多站点)
然后将你的base_url配置文件指向你的基路径。

,然后做同样的
<img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />

+0

你的意思是“将你的base_url配置文件指向你的基路径”。你能解释一下吗? –

+0

base_url到你的根路径的路径。您已经创建了应用程序,系统和其他文件夹(样式,脚本和图像)。 –

0

只有简单的方法

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

坐!