Codeigniter Get php array from view - php

In Codeigniter I have a view theme-fonts.php that I am loading in another view page.php.
page.php
<?php $this->load->view('theme-fonts'); ?>
<script type="text/javascript">
try{
var fontsArray = <?php json_encode($font_list); ?>;
console.log(fontsArray);
}catch(err){
console.log(err.message);
}
</script>
Following is the definition inside my theme_fonts.php file:
<link href="http://fonts.googleapis.com/css?family=Aclonica" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Michroma" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Paytone+One" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Denk+One" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Wendy+One" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet" />
<?php
$font_list = array();
array_push($font_list,"Aclonica");
array_push($font_list,"Michroma");
array_push($font_list,"Paytone+One");
array_push($font_list,"Denk+One");
array_push($font_list,"Wendy+One");
array_push($font_list,"Fjalla+One");
?>
So my question is how to access php array from view theme-fonts.php in main view page.php?
Regards: Jehanzeb

in MVC frameworks singleton class is available from everywhere, so you need store you variable to some property in your case the best place is config object,
$this->config->set_item('item_name', 'item_value');
read about CI config

You need to pass the array to your view:
<?php $this->load->view('theme-fonts', array('font_list' => $font_list); ?>
I suggest rather declaring the array in your controller and passing it to the main view, that way the array will be available in all the views you load.

Well after I solved the issue, I feel that it was a stupid question. As I am loading theme-fonts view inside page view and not in parallel. I can simply write my script code in theme-fonts view. This will in turn write it to my page view.
page.php
<?php $this->load->view('theme-fonts'); ?>
theme-fonts.php
<link href="http://fonts.googleapis.com/css?family=Aclonica" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Michroma" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Paytone+One" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Denk+One" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Wendy+One" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet" />
<?php
$font_list = array();
array_push($font_list,"Aclonica");
array_push($font_list,"Michroma");
array_push($font_list,"Paytone+One");
array_push($font_list,"Denk+One");
array_push($font_list,"Wendy+One");
array_push($font_list,"Fjalla+One");
?>
<script type="text/javascript">
try{
var fontsArray = <?php print(json_encode($font_list)); ?>;
console.log(fontsArray);
}catch(err){
console.log(err.message);
}
</script>

Related

why does my css nor html load in codeigniter?

When I include the following code within my view file:
<link rel="stylesheet" href="<?php echo base_url();>css/libs/animate.min.css" type="text/css" media="all">
<!-- Font Awesome icon library -->
<link href="<?php echo base_url();?>css/libs/font-awesome.min.css" rel="stylesheet" type="text/css" media="all">
<!-- Load primary stylesheet -->
<link rel="<?php echo base_url();?>stylesheet" href="css/skin.css" type="text/css" media="all">
Nothing loads not even HTML content,
but when I remove these lines HTML content loads properly.
So how do I load my CSS files? They are in my assests/css
You have to load helper in config/autoload.php
$autoload['helper'] = array('url', 'file','form');
in config.php]
$autoload['helper'] = array('url');
and base_url() should
$config['base_url'] = '';
you did mistaken to close the php script in first css linking
you wrote .
<?php echo base_url();>
instead of
<?php echo base_url();?>
and also check that you have loaded url helper in your code or not ?
use this
<link rel="stylesheet" href="<?php echo base_url();?>css/libs/animate.min.css" type="text/css" media="all">//mistake in this line missing '?'
<!-- Font Awesome icon library -->
<link href="<?php echo base_url();?>css/libs/font-awesome.min.css" rel="stylesheet" type="text/css" media="all">
<!-- Load primary stylesheet -->
<link rel="<?php echo base_url();?>stylesheet" href="css/skin.css" type="text/css" media="all">

How to apply css style in codeigniter

I am having trouble with applying CSS style in my view.
I have set base_url in config.php also..
config['base_url']='http://localhost/test_project/'
My code is
<html>
<head>
<title>Untitled Document</title>
<link type="text/css" href="<?php echo base_url()?> css/mycss.css" >
</link>
</head>
<body>
<p> This is test.</p>
</body>
</html>
My CSS code is
p
{
color:#0066CC;
}
But it shows only simple text..
How to apply CSS in codeigniter....
Put the path inside the function. Include rel="stylesheet" in the link tag. You don't need the closing </link> either.
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/mycss.css'); ?>">
View the source of the page, you should see the CSS included like this
<link rel="stylesheet" type="text/css" href="http://localhost/test_project/css/mycss.css">
You need put in html tag link the attribute rel="stylesheet"
<link type="text/css" href="<?php echo base_url('css/mycss.css') ?>" rel="stylesheet" >
Check with your firebug if your css files is load fine.
You could use html tag called "base" for example:
<base href="http://url" />
In your code:
<html>
<head>
<title>Untitled Document</title>
<base href="<?php echo base_url()?>" />
<link type="text/css" href="css/mycss.css" rel="stylesheet"></link>
</head>
<body>
<p> This is test.</p>
</body>
</html>
I use the default function of codeigniter. For example:
echo link_tag('assets/css/bootstrap.css');

joomla 1.5 Custom mobile framework not loading css

Hi Guys I have been tasked with moving a custom framework from joomla 1.5 to 3.0 I have gotten the framework to load the homepage perfectly however when I go to other pages the css wont load here is the code:
PHP:
<link rel="stylesheet" href="<?php echo $this->baseurl(); ? >templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl(); ?>templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->templateurl(); ?>/css/addons.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->templateurl(); ?>/css/layout.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->templateurl(); ?>/css/template.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->templateurl(); ?>/css/typo.css" type="text/css" />
HTML Output:
<link rel="stylesheet" href="http://flyvail/index.php/templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="http://flyvail/index.php/templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" href="http://flyvail/index.php/templates/FlyVail2/css/addons.css" type="text/css" />
<link rel="stylesheet" href="http://flyvail/index.php/templates/FlyVail2/css/layout.css" type="text/css" />
<link rel="stylesheet" href="http://flyvail/index.php/templates/FlyVail2/css/template.css" type="text/css" />
<link rel="stylesheet" href="http://flyvail/index.php/templates/FlyVail2/css/typo.css" type="text/css" />
<!-- CSS for handheld devices -->
<link rel="stylesheet" href="http://flyvail/index.php/templates/FlyVail2/css/handheld/layout.css" type="text/css" />
<link rel="stylesheet" href="http://flyvail/index.php/templates/FlyVail2/css/handheld/template.css" type="text/css" />
<!-- CSS for handheld devices -->
<link rel="stylesheet" href="<?php echo $this->templateurl(); ?>/css/handheld/layout.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->templateurl(); ?>/css/handheld/template.css" type="text/css" />
<!-- //CSS for handheld devices -->
I am not sure why it isn't loading right
Assuming this is in your template's index.php file, then try:
$doc = JFactory::getDocument();
$doc->addStyleSheet($this->baseurl.'/templates/'.$this->template . '/css/NAMEOFSTYLESHEET.css', $type = 'text/css', $media = 'screen,projection');
to add your style sheets instead.
My guess is that $this->baseurl() does not work with the () as it is a parameter, not a function. As such, your link is shown without the baseurl is generated by a relative link which also takes into account the base href that joomla sets. This means when you are not in the root of the site, it the link is wrong.
(Your code would probably work if you just removed all the ()s, but the addStyleSheet method is better practise)
If that doesn't work, can you take a look to see if the URL generated is different on your homepage where you say it works and the sub-page where you say it does not, and add this additional info to your question.

Replacing php links to static links

I have script which uses absolute addressing in most areas, now I need to change them to relative addressing. But then the issue I am facing here is that, if for instance I have a website named www.example.com Now I want to change the links in php from
<link href="<?php print $theme; ?>style.css" type="text/css" rel="stylesheet" />
<link href="<?php print $theme; ?>css/colorpicker.css" type="text/css" rel="stylesheet" />
to
<link href="http://www.example.com/<?php print $theme; ?>style.css" type="text/css" rel="stylesheet" />
<link href="http://www.example.com/<?php print $theme; ?>css/colorpicker.css" type="text/css" rel="stylesheet" />
But I end up getting this result
<link href="http://www.example.com/http://www.example.com/themes/in/style.css" type="text/css" rel="stylesheet" />
<link href="http://www.example.com/http://www.example.com/themes/in/css/colorpicker.css" type="text/css" rel="stylesheet" />
I know something is wrong somewhere, but I still can't get it to work.. Any help with correct formatting will be highly appreciated. Sorry if my title for question is inappropriate.
Judging by the url setup:
<?php
$path = explode('/', $theme);
$theme = end($path);
?>
<link href="http://www.example.com/themes/<?= $theme ?>/style.css" type="text/css" rel="stylesheet" />
This will split the url in pieces and picks the last item which is I think is the theme name you want.
Your $theme contains this url 'http://www.example.com/themes/in/'. So if you just want the url to be like this
http://www.example.com/style.css instead of
http://www.example.com/http://www.example.com/themes/in/style.css
then remove the $theme from the href section.
hope this helps
Their would be two possible solutions, either you remove the
http://www.example.com/
from starting of <?php print $theme; ?> OR only include
theme name rather than with base bath.

PHP stylesheet print switcher problem revisited?

Okay I have this style sheet switcher which will only works if I leave out the media="print" from the style sheet link.
I was wondering how can I fix this problem without leaving out the media="print" attribute.
Here is the PHP code.
<!-- Print Script -->
<?php if (isset($_GET['css']) && $_GET['css'] == 'print') { ?>
<meta name="robots" content="noindex" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<script type="text/javascript">
//<![CDATA[
if(window.print())
onload = window.print();
else
onload = window.print;
//]]>
</script>
<?php } else { ?>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<?php } ?>
<!-- End Print Script -->
And here is the link you click to change the style sheet.
Print This Page
You don't need PHP code for determining whether to output the CSS file for print. By default the browser won't render the "print" stylesheet and should ignore the "screen" stylesheet when printing.
There may be some rendering issues: maybe the browser hasn't had enough time to render the page and feed it correctly to the printer.
A simplified solution would be:
<head>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<script type="text/javascript">
function print_it() {
if(window.print())
onload = window.print();
else
onload = window.print;
}
</script>
</head>
<body>
Print This Page
</body>

Categories