I need to change my controller so that I can output the language file with in my view with this syntax echo $lang->line ('') instead of the CI standard echo $this->lang->line (''). I may not change the code in my view.
View :
<div id="back-to-top"><?php echo $lang->line('general_back_to_top'); ?></div>
Controller :
public function __construct()
{
parent::__construct();
$this->lang->load('general', 'en');
}
public function footer()
{
$this->load->view('templates/bmain_plain_footer');
}
You can resolve this in the controller, where you are calling your view (assuming it is the function you mentioned in your question), adding Dynamic Data to the View:
public function footer()
{
$data['lang']=$this->lang;
$this->load->view('templates/bmain_plain_footer', $data);
}
Now you can display your data in your view as pretended:
echo $this->lang->line('general_back_to_top');
note: as you are using the internationalization within a link, you should prepare a route for that link so that all language versions point to the same controller.
This is the answer for my question! :)
public function __construct()
{
parent::__construct();
$this->lang->load('general', 'en');
}
public function footer()
{
$data['lang'] = $this->lang;
$this->load->view('templates/bmain_plain_footer', $data);
}
Related
I ham writing the follow Codeigniter code, but I keep getting a 404 error. Any thoughts?
funding_controller.php
class Funding extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function index()
{
$this->load->model('funding_model');
$data['funds'] = $this->funding_model->getAll();
$this->load->view('funding_list', $data);
}
}
funding_model.php
class Funding extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
$query = $this->db->get('funds');
return $query->result();
}
}
funding_list.php
foreach($funds as $row) {
echo $row->opportunity_name;
echo "<br />";
}
The default route points to funding_controller. Thoughts?
controller name and class name should be same and file name in lower case
funding.php
And try following url
http://domain.com/funding/index
Model class name different from controller name like funding_model or anything else and no need to suffix _model in ->load->model()
Use first letter in capital for controller n model file name.
For example:
Founding_controller.php
Founding_model.php
And class name should be filename (as it is).
I have an app using CI 1.7.2.
In controller i have $data['title'] = 'Sample Title';
In view
<?php echo $title; ?>
Should print it.
But not working.
I have another app with latest CI, in that passing like this works fine.
Anybody know reason for this strange issue?
You should pass the data to view like,
$this->load->view('view_file', $data);
There is another way to pass data to view use the library parser
class home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('parser');
}
public function index() {
$this->sssssssss();
}
function sssssssss($data = '') {
$header = $this->parser->parse('interface', array(), TRUE);
$data['interface'] = $header;
//bind all together and parse to template view
$this->parser->parse('template_view', $data);
}
}
I'm not CI programmer, just trying to learn it. Maybe this is wrong approach, please advice.
my controller(not in sub directory) :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index($msg = NULL) {
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function process_logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
And a route for login :
$route['user/login'] = 'users/index';
Problem is when I wanna logout, it shows me 404 because I do not have it in my route :
$route['user/process_logout'] = 'users/process_logout';
and in my view I put logout
When I add that, it works, and that is stuppid to add a route for everything. What I'm I doing wrong, please advice.
Thank you
Don't know why you are trying to implement login feature in index() function. However since you said you are learning CI I'm telling something about _remap() function.
Before that. You can try the following routing:
$route['user/:any'] = 'users/$1';
$route['user/login'] = 'users/index';
If you want to take value immediately after controller segment you need to use _remap() function and this function may be solve your routing problem, i mean you don't need to set routing. Lets implement your code controller 'users' using _remap() function.
class Users extends CI_Controller {
private $sections = array('login', 'logout');
function __construct() {
parent::__construct();
}
public function _remap($method)
{
$section = $this->uri->segment(2);
if(in_array($section, $this->sections))
call_user_func_array(array($this, '_'.$section), array());
else show_404(); // Showing 404 error
}
private function _login()
{
$msg = $this->uri->segment(3);
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function _logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
I am using HMVC with CodeIgniter.
I have this in my testmodule controller:
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
And this in my view template.php of that controller that is loaded by this controller:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>
but, when I var_dump($main_content) in the view and die() it shows null instead of frontpage
How, come? I don't get it at all.
If you want to use $this->view_data you have to declare $view_data as a property first (at the top of your controller):
class TestModule extends CI_Controller
{
public $view_data = array();
public function index()
{
// Now you can use $this->view_data in this function:
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}
CodeIgniter 2.1.2
I have a class that contains two methods, for the purpose of this questions showone and view. The latter returns all items of a small database and can also perform a search. The other one is for permalinks like domain.com/showone/firstname-lastname
<?php
class Pages extends CI_Controller {
public function view($page)
{
//this includes a mysql search
}
public function showone($slug)
{
//abbreviated version:
$query = "SELECT * FROM mytable WHERE slug = '" . $slug . "'";
$result = $this->db->query($query);
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
?>
So if a user decides to directly enter a URL that doesn't return anything from the database, I would like to direct him to search results instead of showing a 404.
So how do I set up a function searchdatabase($query) to be used by both showone and view?
You can use your controller functions inside your controller:
public function view($page)
{
$this->showone($slug);
}
Define that function in your model, load your model and then call the model->method.
<?php
//Your Model would look something like this.
class Search_Model extends CI_Model {
public function __construct(){
parent::__construct();
}
public function showone($slug)
{
//abbreviated version:
//Its best to use active record for building your queries
$this->db->where->('slug', $slug);
$result = $this->db->get('mytable');
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
And then in your controller you would do this:
<?php
//Your Controllerwould look something like this.
class Index extends CI_Controller {
public function __construct(){
parent::__construct();
//model will be loaded for each method.
//if you're going to use this model across several controllers
//its best to autoload it, set that in autoload.php under /app/config
$this->load->model('search_model');
}
public function index(){
$searchResults = $this->search_model->showone('slugone');
}
Update
I just realized you are wanting to show all results if no results were returned. In which case you would perform that logic in your model as well..
In your conditional statement you would do the following:
if ($result->num_rows() == 0)
{
return $this->showall();
}
else
{
return $this->view($slug);
}
}
your showall() and view() methods would return $result->result();