Only index function is working inside a controller - php

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.

Related

Base_url() not working on codeigniter

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

Unable to resolve the request "Customerlifecycle/Customerlifecycleanalytic"

Its give me this error
Unable to resolve the request
"Customerlifecycle/Customerlifecycleanalytic".
Here is my code:
Costomerlifecycle Controller
Yii::app()->clientScript->scriptMap = array('jquery.js' => false, 'jquery-ui.min.js' => false, 'jquery-ui.css' => false);
class Customerlifecycle extends Controller {
public $layout = '//layouts/column2';
public function actionCustomerlifecycleanalytic() {
}
}
index.php
<li class="">
<a class="subpageaccess" href="<?php echo CHtml::normalizeUrl(array('Customerlifecycle/Customerlifecycleanalytic')); ?>">
Customer Life Cycle Analytics</a>
</li>
try with first character lowercase.
Because the name of the controller and of the action in url is lowercase. this is the yii convention..
<li class="">
<a class="subpageaccess" href="<?php echo
CHtml::normalizeUrl(array('customerlifecycle/customerlifecycleanalytic')); ?>">
Customer Life Cycle Analytics</a>
</li>

Codeigniter undefined variable site in views

Views:
I got the error when i run the program : undefined variable site in views
This is the following code:
<ul class="nav nav-pills">
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES</li>
<li>PHOTO GALLERY</li>
<li>PROMO GALLERY</li>
<li>CONTACT</li>
</ul>
Moreover i got the error in views undefined variable image in the following code:
<img src="<?php echo $base?>/<?php echo $image?>/fb.png" alt="" title="" />
Also i got the error like undefined variable css, base
<link rel="stylesheet" href="<?= base_url() ?>assets/css/style.css" />
<link rel="stylesheet" href="<?= base_url() ?>assets/css/bootstrap.min.css" type="text/css" />
Please provide solution for this issue.
Its because $site is not contain any value.
this should be
<?php echo $site ?>
this
<?php echo base_url() ?>
And do this Configuration as well (This will help to use base_url function)
In config/config.php
$config['base_url'] = '';
In config/autoload.php
$autoload['helper'] = array('url')
class Timelinestudio extends CI_Controller{
var $data;
function __construct(){
parent::__construct();
//session_start();
$this->load->model('timeline_model');
$this->load->library('form_validation');
$this->data = array(
'site' => $this->config->item('site_url'),
'base' => $this->config->item('base_url'),
'css' => $this->config->item('css'),
'js' => $this->config->item('js'),
'img'=>$this->config->item('img'),
'image'=>$this->config->item('image')
);
// $data = $this->data;
//print_r($data); die();
}
public function index()
{
$this->load->helper(array('form','url'));
$data = $this->data;
//print_r($data); die();
$this->load->model('timeline_model');
$data['slideimg']=$this->timeline_model->get_slideimg();
$this->load->view('index',$data);
}

controller do not displays in right way

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.

How to load Images in Codeigniter?

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

Categories