How to concatenate base url with another variable in code igniter - php

I need to concatenate the base url with another variable which should be public in the class. How can I do this?
Here is my code :
class site extends CI_Controller
{
public $site_favicon = base_url().'img/favicon.ico';
// I tried even public $site_favicon = base_url();.'img/favicon.ico';
}
Where i will call the variable $t publicly any where from the site.
How can i do this ?
This is how I will call:
<link rel="shortcut icon" href="<?php echo $this->site_favicon; ?>">
I am getting:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /opt/lampp/htdocs/ci/application/controllers/site.php on line 23
What mistake am I doing in concatenation and how can i fix this?

Try with this
class site extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('url'));
$url=$this->config->base_url();
$this->site_favicon=$url.'img/favicon.ico';
}
}
Now you can access site_favicon in functions with echo $this->site_favicon;
<link rel="shortcut icon" href="<?php echo $this->site_favicon ?>">
Note : If you want variables set in the constructor accessible, then you have to set them as a class property ie., like $this->variablename and access them in the same way with $this->variablename in your functions.

You can't assign function inside class varable, base_url is helper function, that is initialized in system autoload
So you can use directly in views
Try this way
<link rel="shortcut icon" href="<?php echo base_url("img/favicon.ico"); ?>">
OR
<link rel="shortcut icon" href="<?php echo base_url()."img/favicon.ico"; ?>">
UPDATE: You should assign favicon value in view variable, the view variables are defined in controller -> action method
In controller
$favicon = base_url().'img/favicon.ico';
$this->load->view("viewName", array("favicon" => $favicon));
In view you can access variable
<link rel="shortcut icon" href="<?php $favicon; ?>">
NOTE: you can't access controller class variable value directly in view

Related

CodeInteger Error Message: Message: Call to undefined function link_tag(

<title>Login</title>
<!-- CSS -->
<?php echo link_tag('assets/css/bootstrap.min.css')?>
<?php echo link_tag('assets/font-awesome/css/font-awesome.min.css')?>
<?php echo link_tag('assets/css/form-elements.css')?>
<?php echo link_tag('assets/ico/favicon.png')?>
<?php echo link_tag('assets/ico/apple-touch-icon-144-precomposed.png')?>
<?php echo link_tag('assets/ico//apple-touch-icon-114-precomposed.png')?>
<?php echo link_tag('assets/ico//apple-touch-icon-72-precomposed.png')?>
<?php echo link_tag('assets/ico//apple-touch-icon-57-precomposed.png')?>
</head>
i did not write any Controller or Model class , just to include the above Styles and Images in page , it show the Message: "Message: Call to undefined function link_tag()"Error even i have wrote the following code also in config/autoload file $autoload['helper'] = array('url');"" Error e
link_tag() is a function which defined in HTML helper. You should load HTML helper first.
You have 2 options, first one:
Open, application/config/autoload.php and add 'html' value in helper array.
Second one:
Add this line top of your code.
$this->load->helper('html');
Then, edit your lines like this:
$link = array
(
'href' => 'assets/css/bootstrap.min.css',
'rel' => 'stylesheet',
'type' => 'text/css',
);
echo link_tag($link);
But I think you can just use site_url() function. If you want to use site_url(), you should load url helper and edit your lines like below:
<link href="<?php echo site_url('assets/css/bootstrap.min.css'); ?>" rel="stylesheet" type="text/css" />
You using wrong helper. You should add to your autoload 'html'
Please check documentation
https://ellislab.com/codeigniter/user-guide/helpers/html_helper.html#link_tag
Try using base_url(); instead.
<?php echo base_url('assets/css/bootstrap.min.css')?>

How can I include css and js file in codeigniter

I am trying toinclude css file in the following code.
Config :
$config['base_url'] = 'http://localhost/ASOFT/Projects/CI_search';
$config['site_url'] = 'http://localhost/ASOFT/Projects/CI_search/index.php';
$config['js'] = 'assets/js';
View:
<link rel="stylesheet" href="<?php echo $base?>/<?php echo $css?>/style.css">
<link rel="stylesheet" href="<?php echo $base?>/<?php echo $css?>/bootstrap.min.css">
<script src="<?php echo $base?>/<?php echo $js?>/jquery.min.js"></script>
<script src="<?php echo $base?>/<?php echo $js?>/jquery.js"></script>
<script src="<?php echo $base?>/<?php echo $js?>/bootstrap.min.js"></script>
I put the css file in CI_search/css and js file in CI_search/assets/js
I got the error undefined variable css and undefined variable js.Please provide solution for this problem.
UPDATE
Controller:
public function index() {
$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'),
'image'=>$this->config->item('image')
);
$data = $this->data;
$data['error'] = '';
$this->load->view('index',$data);
}
As far as i can see, you din't declared $config['css'] in your config file, so it's normal to get undefined variable error for css. But you shouldn't have problem with js.
When declaring your base_url use trailing slash at the end (eg. "http://localhost/fancysite/")
Also you can use CI's url helper to use functions like base_url() or site_url() and many more. (as #Likee suggested).
config.php
// this should be the first variable in CI's config.php
$config['base_url'] = "http://localhost/ASOFT/Projects/CI_search/";
// many more lines with other configuration variables
// ..................................................
// ..................................................
// ..................................................
// your own configuration variables at the end of file
$config['css'] = 'css/';
$config['js'] = 'assets/css/';
$config['image'] = 'images/';
controller
public function index() {
// loading url helper to use base_url() function in the view,
// if you load this helper in autoload.php you don't need to load it here again
$this->load->helper('url');
// if you didn't declare data as class property
// you can simply use
// $data = array(
// 'css' => $this->config->item('css'),
// 'js' => $this->config->item('js'),
// 'image'=>$this->config->item('image')
// );
$this->data = array(
'css' => $this->config->item('css'),
'js' => $this->config->item('js'),
'image'=>$this->config->item('image')
);
// you really don't need the line below
// $data = $this->data;
$this->data['error'] = '';
$this->load->view('index',$this->data);
}
view
<link rel="stylesheet" href="<?php echo base_url($css . 'style.css'); ?>">
<link rel="stylesheet" href="<?php echo base_url($css . 'bootstrap.min.css'); ?>">
<script src="<?php echo base_url($js . 'jquery.min.js'); ?>"></script>
<!-- do you really need to include jquery.js while you included jquery.min.js above? but here we go :) -->
<script src="<?php echo base_url($js . 'jquery.js'); ?>"></script>
<script src="<?php echo base_url($js . 'bootstrap.min.js'); ?>"></script>
Probably you will need to use url helper a lot. So you can autoload it in autoload.php file in config folder. it must be somewhere around line 90
$autoload['helper'] = array('url');
There are many ways to include css and js file in codeigniter.
This how I do it.
FIRST: Create a folder outside the application folder and named it any named you liked but I prefer to named it as resources. Create a sub folders like js, css, images and others. Place all your files in here for future references.
SECOND: Open the the contants.php saved in application/config and paste this code.
define('CSS', 'http://localhost/yourfolder/resources/css');
define('JS', 'http://localhost/yourfolder/resources/js');
define('IMAGES', 'http://localhost/yourfolder/resources/images');
NOTE: Update this code depends on your needs.
THIRD: On your view file, you can access this files by,
<link rel="stylesheet" href="<?php echo(CSS.'bootstrap.min.css'); ?>">
<script type="text/javascript" src='<?php echo(CSS.'bootstrap.min.css'); ?>'></script>
Refer to this link. How can I include the CSS file in CodeIgniter?
I hope this helped.
Bind your variables to view like this:
$this->load->view('viewName', $config);
And array $config should be available in your controller method.
Instead setting $config['base_url'] you can use function base_url().
well if at all possible determine what your path structure will be and then just use it in the view with base_url() .
<link href="<?php echo base_url();?>assets/css/bootstrap.min.css" rel="stylesheet">
<script src="<?php echo base_url();?>assets/js/blahblah.js"></script>
you can even have a few simple templates, and then call the appropriate template.
if that is impossible - then push this logic and output to a model or similar so that it can be called by different controllers. otherwise you are going to have to update folder names and file paths in your controllers.

how and where to include js and css for modules?

I have modules for category:
+modules/
+category/
+assets/
+css/
+js/
+images/
+components/
+controllers/
+models/
+views/
-CategoryModule.php
What is the best way to includes the css and jss to all views?
Publish and register in CategoryModule init method - this will make available your css and js in category module.
Something like this:
public function init() {
$path = $this->assetManager->publish(Yii::getPathOfAlias('application.modules.category.assets'));
$this->clientScript->registerCssFile($path . '/css/some-css.css', 'screen, projection');
$this->clientScript->registerScriptFile($path . '/js/some-js.js');
}
create module layout file under views/layout and call it in module config file as
$this->layoutPath = Yii::getPathOfAlias('application.modules.moduleName.views.layouts');
$this->layout = '/layouts/layoutname';
register all the js and css file as #PeterM mentioned
Create a folder "layouts" in your views folder. Create main.php in that layouts folder.
In your , add following code:
<link rel="stylesheet" href="<?php echo Yii::app()->request->baseUrl; ?>/css/style.css" type="text/css" media="screen"/>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/cycle.js"></script>
This is your default layout. So add,
<?php echo $content; ?>
This is another method to include css and js in all views.
Easy using this static method
<?php Yii::app()->clientScript->registerCssFile(Yii::getPathOfAlias('application.modules.curriculo.assets') . '/bootstrap/datepicker/css/datepicker.css'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::getPathOfAlias('application.modules.curriculo.assets') . '/bootstrap/datepicker/js/bootstrap-datepicker.js'); ?>
This should help you:
http://www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/
All three sections (beginContent, container and endContent) are explained in detail.

How to append style sheet with close element using zend?

I am using zend. When i use below code,
<?= $this->headLink()
->appendStylesheet(BASE_URL . 'css/css.css');
?>
This outputs like below with out close element.
<link href="/ecomos3/css/css.css" media="screen" rel="stylesheet" type="text/css" >
so this one fails in w3 validation and getting "end tag for "link" omitted, but OMITTAG NO was specified"
i want the link tag should be
<link href="/ecomos3/css/css.css" media="screen" rel="stylesheet" type="text/css" />
How can i do it in zend ? Kindly advice on this.
You need to specify the docType using the doctype helper
You can do it in your bootstrap if you wish:-
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
}
Or add this line to your config.ini file:-
resources.view.doctype = "XHTML1_STRICT"

PHP Constants in Classes

I have the following code sample, which produces an error with the included constant when it is run. Could someone please show me where this is going wrong?
class Template {
private $headers = "<link rel=\"stylesheet\" type=\"text/css\"
href=\"" . ROOT . "system/stylesheets/universal.css\" />";
... More variables and methods
}
Here is the error I receive. I am sure that the ROOT constant is defined, just outside of this class:
Parse error: syntax error, unexpected '.', expecting ',' or ';' in <page.php> on line <line number>
Thank you for your time,
spryno724
You cannot specify non-static values as your members default values. And you're trying to perform dynamic (runtime) things - concatenation of the strings.
class Template
{
public static headers()
{
return '<link rel="stylesheet" type="text/css" href="' . ROOT . 'system/stylesheets/universal.css" />';
}
}
Usage:
$headers = Template::headers()
Also - I missed that you're using just instance variables, not constants. In this case you can also use initialization in constructor:
class Template
{
private $headers;
public static __construct()
{
$this->headers = '<link rel="stylesheet" type="text/css" href="' . ROOT . 'system/stylesheets/universal.css" />';
}
}

Categories