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">
1
2
3
4
5
</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.
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 { ...
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.
How to use with the CI URL helper is detailed here : Helper
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
}
}
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">
You can do the same for as you did to Load the CSS files.
Create a folder in Root Directory. Create a sub folder(if you have Multisite)
then point your base_url in config file to your base path.
and then do the same
<img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />
Only simple way
<img id="image1" src="<?php echo base_url('1.jpg'); ?>" />
That sit!!!
Related
I have a controller with two functions as below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('User_model');
}
public function index() {
$this->load->view('registration/index');
}
public function login() {
$this->load->view('registration/index');
}
and following in the header.php file
<li class="nav-item">
<a class="nav-link" href="<?php echo site_url("user/login")?>">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo site_url("user")?>">Register</a>
</li>
My problem is when I click on register button with site_url('user'), its working fine but when i click login button with site_ur('user/login') my css are not loaded. I have tried the same view for both the function. But later one is showing problem.
Can anybody help me what configuration mistake I did. My file structure of view folder is:
- views
-registration
-index.php
-index_script.php
templates
-header.php
-footer.php
-html_header.php
index.php has
<?php $this->load->view("templates/html_header");
$data = array();
$data["additional_script"] = $this->load->view("registration/index_scripts", array(), true);
$this->load->view("templates/footer", $data);
?>
html_header.php contains
<!-- include angular js -->
<script src="libs/angular/angular.min.js"></script>
<link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link href="assets/css/clean-blog.min.css" rel="stylesheet">
<?php $this->load->view("templates/header")?>
header.php
The URL you are using is incorrect. You must add <?php echo base_url(); ?> to the script src.
For eg: <script src="<?php echo base_url(); ?>libs/angular/angular.min.js"></script>
Maybe this can work.
I am using CodeIgniter bootstrap stylesheet link URL can't work properly.
Controller code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->helper('url');
$data['title'] = "Login Home";
$this->load->view('home',$data);
}
}
HTML Code
<div class="container">
<div class="row">
<div class="col-lg-offset-4 col-lg-4">
<div class="well">
<h3>welcome</h3>
</div>
</div>
</div>
</div>
CSS link code
<link href="<?php echo base_url("register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
Place assets out side of application. Reason the .htaccess in application folder blocks it.
application
assets
assets > bootstrap > css > bootstrap.css
assets > bootstrap > js > bootstrap.js
system
index.php
First I would suggest
config/autoload.php
$autoload['helper'] = array('url');
then
config/config.php Set your base url
$config['base_url'] = 'http://localhost/projectnmame/';
in config.php set this line
$config['base_url'] = 'http://localhost/register/';
root directory set this way
application
system
bootstrap-3.3.6
ensure that you are using .htaccess file in your codeigniter application.create one folder assets.place your css file in assets folder.after that link that file like this,
in application folder config file paste this,
$root = 'http://' . $_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);;
$config['base_url'] = $root;
<link href="<?php echo base_url("assets/register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
if you are not using htaccess file,
<link href="<?php echo base_url("index.php/assets/register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
try this....
I m learing codeigniter and i want to add an image in my view page.
I am using tag but the image is not added in the page.
So help me how i can add the image in view page of codigniter.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Blog</title>
</head>
<body>
<h2><img src="../images/logo.png"></h2>
<?php $this->load->view('blog/menu');
if ($query):foreach ($query as $post): ?>
<h4><?php echo $post->entry_name; ?> (<?php echo $post->entry_date; ?>)</h4>
<?php echo $post->entry_body; ?>
<?php endforeach;
else: ?>
<h4>No entry yet!</h4>
<?php endif; ?>
</body>
</html>
store images in an images folder at the same level as the application folder . then just link to it like this using code.
<img src="<?php echo base_url('images/logo.png'); ?>" />
Store images in an images folder at the same level as the application folder, Then just link to it like this:
<img src="../../images/image1.jpg" />
See, first you create a file with the name user_profile.php in the controller folder. no uppercase letters.
To call a View page you created in the index:
function index()
{
$this->load_controller('myView');
}
this is an example from the link i sent you that is perfect, that shows peaces of a views and how to combine them to one code from the controller.
Please go to the codeigniter website and start learning the simple stuff. or search for codeigniter via youtube. you will find a lot!
Class User_Profile extends Controller
{
function index()
{
$this->load_controller('Left_Nav');
$this->load_controller('Content_Nav');
$this->load_controller('Login_Name');
$this->load_controller('Leaderboard', 'Board');
$this->Left_Nav->index(array('highlight_selected_page' => 'blah'));
$this->load('User');
$content_data = $this->User->get_profile_details();
$this->view->load('content', $content_data);
$this->Login_Name->index();
$this->Board->index();
}
}
image in assets
<img src="<?php echo base_url(); ?>assets/images/image.png">
I am using a PHP Framework for the first time to get away from writing spaghetti code and so far it is wonderful. I am using Code Igniter and have a base Controller named "Dashboard.php" that loads fine and looks perfect.
When attempting to create some organization, I placed my "Devices.php" into a "Controllers/Devices" sub-folder, then accessed it at "http://domain.com/devices/devices" which loads the text, but the images/css/etc. are missing.
The content of the "Devices" controller is exactly the same as my "Dashboard" so I am not understanding why only the text loads. If I move the "devices.php" file into the root Controllers directory, there are no problems.
class Devices extends CI_Controller
{
public function index()
{
$data['page_title'] = 'Device Portal';
$this->load->view('meta');
$this->load->view('sidebar');
$this->load->view('userbar');
$this->load->view('header');
$this->load->view('devices/content');
$this->load->view('footer');
}
}
Here is the "Sidebar.php" View...
<!-- Left side content -->
<div id="leftSide">
<div class="logo"><img src="assets/images/logo.png" alt="" /></div>
<div class="sidebarSep mt0"></div>
<!-- Search widget -->
<form action="" class="sidebarSearch">
<input type="text" name="search" placeholder="search..." id="ac" />
<input type="submit" value="" />
</form>
<div class="sidebarSep"></div>
<!-- General balance widget -->
<div class="genBalance">
<a href="#" title="" class="amount">
<span>General balance:</span>
<span class="balanceAmount">$10,900.36</span>
</a>
<a href="#" title="" class="amChanges">
<strong class="sPositive">+0.6%</strong>
</a>
</div>
You need to avoid using relative paths when linking to images/css/etc in your views.
As an example, when you use assets/images/logo.png (relative path) from the page http://domain.com/devices/devices, the browser looks for http://domain.com/devices/assets/images/logo.png
Using the same image path with the page http://domain.com/devices the browser will look for http://domain.com/assets/images/logo.png instead.
When specifying urls to pages, images, etc within your domain, use the URL helper functions to automatically format your urls.
You want to avoid writing urls like this:
<script type="text/javascript" src="./assets/js/jquery.js"></script>
<img src="assets/images/logo.png" alt="" />
And instead generate your urls like this:
<script type="text/javascript" src="<?php echo base_url('assets/jquery.js'); ?>"></script>
<img src="<?php echo base_url('assets/images/logo.png'); ?>" alt="" />
Which will output:
<script type="text/javascript" src="http://domain.com/assets/js/jquery.js"></script>
<img src="http://domain.com/assets/images/logo.png" alt="" />
In Codeigniter use base_url() function to print assets.
<img src="<?php echo base_url('assets/images/logo.png');?>" alt="" />
This function automatically generate domain before the url.
I have two controllers in my project. The second controller do not displays in right way. If I open it with in browser line - thats ok, but if i pass it by the link in view its not ok. The second controller resembles on 1st.
Controller(s)
<?php
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');
class Article extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
ini_set('display_errors',1);
error_reporting(E_ALL);
$this->load->view('header_view');
$this->load->view('menu_view');
$this->load->view('categories_view');
$this->load->model('mat_model');
$data = array();
$data['news'] = $this->mat_model->get_latest();
$data['latest'] = $this->mat_model->get_latest();
$this->load->view('useful_sites_view',$data);
$this->load->view('article_view',$data);
$this->load->view('footer_view');
}
}
?>
The view where I have a link:
<?php foreach ($news as $one):?>
<div id="right">
<div id="breadcrumb">Home » Somewhere</div>
<h1><?=$one['title']?></h1>
<p>
<div id="small_img"><?=$one['small_img']?></div>
<?=$one['description']?>
</p>
<div id=""> Читать далее...</div>
<span class="postinfo"> Posted by <?=$one['author']?> on
<?=$one['date']?></span>
<HR ALIGN="center" WIDTH="70%" SIZE="1px" COLOR="black">
</div>
<?php endforeach; ?>
I dont have so much reputation to browse images on the site, but i needs it so much, apologize for using otherwise resource
With line in browser:
With the link:
http://s020.radikal.ru/i710/1301/fd/76f313259454.jpg
With the browser line:
http://s020.radikal.ru/i713/1301/4e/a863cd18184d.jpg
Thanks.
In reply to the comment above...
<link rel="stylesheet" href="<?= base_url() ?>css/style.css">
is an absolute path.
Lets say that your base_url() is 'http://www.example.com/' so the above line will link to your css like so:
<link rel="stylesheet" href="http://www.example.com/css/style.css">
so no matter which page you're on, the CSS will always be linked to that css file.
On the other hand if you were using relative paths like this:
<link rel="stylesheet" href="/css/style.css">
on your site root, the css path would be correct. BUT on some other page, for example
http://www.example.com/somepage/testurl
your CSS would be linked like this:
<link rel="stylesheet" href="http://www.example.com/somepage/testurl/css/style.css">
which is obviously, wrong.
Try to google relative and absolute paths in html, that should make things a lot clearer.