code igniter - loading a controller and its data within another controller / view - php

I'm a bit confused on including a controller / view within another controller/view. I'm doing the following and getting funny rendering issues:
//CONTROLLER About
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function about(){ //the standalone about page
$data['about_inner'] = $this->about_inner();
$this->load->view('pages/about',$data);
}
public function about_inner(){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
$this->load->view('pages/about-inner',$data);
}
}
//view about.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container about" data-page="about">
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
</body>
</html>
The issue I'm getting is that $about_inner does not end up loading inside of the "scroll-container" - it loads and renders before everything else, as shown in the screenshot.
What's the best way to get the about_inner view and all of its associated data to load within the about view? I need all the content ($this->about_model->get_content()) to come from about_inner since it can also be loaded by other pages via ajax.
SOLUTION
public function about(){ //the standalone about page
$data['nav'] = $this->load->view('templates/nav', NULL, TRUE);
$data['about_inner'] = $this->about_inner(true);
$this->load->view('pages/about',$data);
}
public function about_inner($print =false){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
return $this->load->view('pages/about-inner',$data, $print);
}
//HTML
//view about:
<div class="container about" data-page="about">
<?=$nav; ?>
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>

You just need to tell the about_inner function to return the data and not print the data. That's the 3rd argument to function view($template, $data, $return);
$this->load->view('pages/about-inner',$data, true);
If you need to do either or, just set a boolean flag as the argument to the function
function about_inner($print = false){
...
$this->load->view('pages/about-inner', $data, $print);
}
Then when you call the function you can simply pass true to the function in order to get it to return the HTML instead of printing the HTML.
$this->about_inner(true)

if your JS folder is in root you have to define it with base_url() function. So correct all the path in get_content method
Ex
<script src ="<?php echo base_url() ?>path/to/your/folder/aaa.js" .....
And i think you can keep one controller method and remove one.(You can keep about and remove about_inner)

Related

how to change the function name in the url using codeigniter?

I want to change the function name in the url, i searched for this a lot but unable to find any solution, can any one help me out what should i do
consider for example i have a url like
http://localhost/codeIgniter_try/index.php/Blog/blogvieww ,
so here "Blog" is the controller name and "blogvieww" is the function name so if i want to change the function name from "blogvieww" to "blogvieww_all" what can i do?
Blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
$this->load->view('blogview');
}
public function blogvieww()
{
$this->load->view('blogvieww');
}
}
?>
blogview.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
blogvieww.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 2nd Blog!</h1>
</div>
<div>
<h1>Welcome to my 3rd Blog!</h1>
</div>
</body>
</html>
Hope this will help you :
Add the below line of code in your route.php
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
Your anchor should be like this :
<a href=<?=site_url('Blog/blogvieww_all');?>
You can set it through routes of CodeIgniter.
Your path for routes will be application/config/routes.php
See this below may help.
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
For more detail: https://www.codeigniter.com/userguide3/general/routing.html
1) You can use URI routes. In routes.php, You can specify like below:
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
Check here for more info.
2) Write same function again in controller with the name of 'blogvieww_all' .
Change this in your controller:
public function blogvieww()
{
$this->load->view('blogvieww');
}
to
public function blogviewW_all()
{
$this->load->view('blogvieww');
}
You just need to change the name of the function in your controller, to the name you want to display on ur url.
Or
As Pradeep has commented, you can change to routing also. But the best way is to change the function name, unless it is being referenced or called from somewhere else.

How can I set facebook meta tags dynamically?

I have an opencart site, and I'm trying to configure facebook share options of my products.
Since everything is loaded as a separate module, I can't set the facebook meta tags like this (header.tpl):
<meta property="og:description" content="<?php echo $description; ?>" />
Because the $description doesn't exist while the header module is loading.It is created in the controller of product module. So I tried to change the content value dynamically (product.tpl):
$("meta[property='og:description']").attr('content','<?php echo $description; ?>');
And it worked, I can see that the value is changed (in the page source), then I debugged my page but I couldn't get the value.. I think I know the reason, I have to set the value before the page load but I'm not sure how can I do that.. do you have any idea ?
You could use the Document class to add those facebook tags (as many as you want). Just add two extra methods setFacebookDescription and getFacebookDescription, so you have to add the followings:
<?php
class Document {
private $facebook_description;
public function getFacebookDescription() {
return $this->facebook_description;
}
public function setFacebookDescription($facebook_description) {
$this->facebook_description = $facebook_description;
}
}
On each controller, you will find at the end of each method, a call which loads the header of Opencart, something like this $data['header'] = $this->load->controller('common/header'); (example). Note that it might differ from yours, it depends on Opencart version.
Now, in header.php controller you add:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['facebook_description'] = $this->document->getFacebookDescription();
}
}
this will get the facebook_description variable and pass it to the view header.tpl. Next, add the facebook tags between your <head> tags in header.tpl file:
<!DOCTYPE html>
<head>
<?php if ($facebook_description != '') { ?><meta property="og:description" content="<?php echo $facebook_description; ?>" /><?php } ?>
</head>
Finally, you can set facebook_description in each controller, by calling $this->document->setFacebookDescription('my description');.
Example: in product.php controller you add
<?php
class ControllerProductProduct extends Controller {
public function index() {
// code...
$this->document->setTitle($product_info['meta_title']);
$this->document->setDescription($product_info['meta_description']);
$this->document->setFacebookDescription($product_info['meta_description']);
// the rest of the code...
}
}
here you set the $product_info['meta_description'] as facebook description tag, however you could also use $product_info['name'] or other variable.
Final note: you can change all the Opencart system classes with the vqmod.

How to codeigniter Sending Parameters Between Controller and View different function

I'm new codeigniter developer.
I trying sending controller between views but only index function sending.
When I try different function, I get this error.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: sample
Filename: views/merhaba_sayfasi.php
Line Number: 8
My controller file = merhaba.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Merhaba extends CI_Controller {
public function index()
{
$data["title"]="İlk codeigniter sayfam";
$data["giris_baslik"] = "Merhaba Dünya";
$this->load->view('merhaba_sayfasi',$data);
}
public function example(){
$data["sample"] = "Sample php";
$this->load->view("merhaba_sayfasi",$data);
}
}
?>
My view file = merhaba_sayfasi.php
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<h3><?php echo $giris_baslik; ?></h3>
<?php echo $sample; ?>
</body>
</html>
My explorer image
http://www.medyasef.com/questions/codeigniter_function_error.png
It depends what URL you are calling?
If you call "/app/Merhaba" or "/app/Merhaba/index" you will not see the sample variable.
If you call "/app/Merhaba/example" then you should see it. It all depends what function of the controller you are calling - by default its only the index function
You're trying to display a variable, whose value hasn't been set, that's why the error is being thrown.
You could check in your view to ensure that a variable has a value before attempting to echo it.
So, instead of:
<?php echo $sample; ?>
You could use:
<?php if(isset($sample)) {echo $sample;} ?>
Although, I'd recommending keeping the logic in your controller and setting all of the values in your controller before loading the view

codeigniter load view specific javascript

I have a register view file on which there is a very big form. Hence I plan to use a form wizard plugin along with the jquery form validation plugin. I only want these scripts loaded on specific pages. How do I do this ? Here is my controller method.
public function index(){
$data['title'] = "Register";
$this->load->view("site_header", $data);
$this->load->view("site_nav");
$this->load->view("content_register");
$this->load->view("site_footer");
}
I saw a similar post on stackoverflow here but I don't understand what to do. Please help.
You can send which js to load as a variable. Example is here;
Sample controller
class mypage extends CI_Controller{
public function index(){
$data['js_to_load']="index.js";
$this->load->view('header',$data);
$this->load->view('my_html_page');
}
public function second_page(){
$data['js_to_load']='second.js';
$this->load->view('header',$data);
$this->load->view('my_second_html_page');
}
}
header.php - View file
<!-- Needed html code here -->
<? if ($js_to_load != '') : ?>
<script type="text/javascript" src="assets/js/<?=$js_to_load;?>">
<? endif;?>
<!-- Other html code here -->
Whats happening?
We are setting the js file into $data variable, and passing the $data variable to header.
In header file we are checking if js_to_load is set? If yes, we are including the js file.
Also you can pass multi js files by using arrays.
Sample controller
class mypage extends CI_Controller{
public function index(){
$data['js_to_load']=array("index.js","index1.js","index2.js");
$this->load->view('header',$data);
$this->load->view('my_html_page');
}
public function second_page(){
$data['js_to_load']=array("second.js","second2.js","second2.js");
$this->load->view('header',$data);
$this->load->view('my_second_html_page');
}
}
header.php - View file
<!-- Needed html code here -->
<? if (is_array($js_to_load)) : ?>
<? foreach ($js_to_load as $row):
<script type="text/javascript" src="assets/js/<?=$row;?>">
<?endforeach;?>
<? endif;?>
<!-- Other html code here -->
Pass an array of js you want added to the header in the data, and add in in the view, there are probably other ways of doing it (this kind smears the controller/view line, so it's a bit dirty, but it works).
public function index(){
$data['title'] = "Register";
$data['js'] = array('link', 'link', etc);
$this->load->view("site_header", $data);
$this->load->view("site_nav");
$this->load->view("content_register");
$this->load->view("site_footer");
}
It is better to load your .js in your footer as your HTML should fully load before your start running script over it.
see here for reasons
i use a universal footer to handle the .js files
public function index()
{
$this->load->view('template/header');
$this->load->view('template/nav');
$this->load->view('thing/landing');
$this->load->view('template/footer');
}
my footer view is
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$caller_class = $this->router->class;
$caller_method = $this->router->fetch_method();
?>
<script type="text/javascript" src="<?php echo base_url(); ?>public/js/jquery.min.js"></script>
<?php
//look for JS for the class/controller
$class_js = "public/js/custom-".$caller_class.".js";
if(file_exists(getcwd()."/".$class_js)){
?><script type="text/javascript" src="<?php echo base_url().$class_js; ?>"></script>
<?php
}
//look for JS for the class/controller/method
$class_method_js = "public/js/custom-".$caller_class."-".$caller_method.".js";
if(file_exists(getcwd()."/".$class_method_js)){
?><script type="text/javascript" src="<?php echo base_url().$class_method_js; ?>"></script>
<?php
}
?>
</body>
</html>
using $this->router->class; or $this->router->fetch_method(); to detect what controller or method in the controller is being used. then detecting if a .js file exists for that controller/method
with little management you can have controller and/or method specific scripts without fiddling with the footer.
in file pub/js/custom-mycontrollername-mymethod.js you would then use (jquery);
$(document).ready(function() {
if ( $( "#mydivthing" ).length ) {
$('#mydivthing').html("fetching..");
$.ajax({
........
});
} else {
console.log("skipped js segment");
}
});
here the jquery only runs when the document is ready (as jquery recommends)
and you can also use if ( $( "#mydivthing" ).length ) { to check that your div is actually on the page. so you are not firing events when they are not loaded (for speed). but as jquery is likely doing some checking - this is probably only helpful for larger runs of code than just single button clicks events.
while the question was for views, this answers gets to a controller/methods level. so while a method can have multiple views - this still a view/screen as seen by the user.
added bonus that you can associate .js files to controller/methods just by its filename.
you can use a variable as a controller name and pass this variable to your view like this
Here is Controller
class Blog extends CI_Controller{
public function index(){
$data['controller']="blog_index";
$this->load->view('header',$data);
}
public function second_page(){
$data['controller']="blog_second_page";
$this->load->view('header',$data);
}
}
Header.php File
<? if (isset($controller) && in_array($controller,array('blog_index'))) : ?>
<script type="text/javascript" src="assets/js/custom_blog_index.js">
<? endif;?>
<? if (isset($controller) && in_array($controller,array('blog_second_page'))) : ?>
<script type="text/javascript" src="assets/js/custom_blog_second_page.js">
<? endif;?>
<!-- Other html code here -->
hope this will answer your question
I don't understand your problem. I suppose you load your js files in view site_header or view site_footer, why don't you load another view for these pages? for example:
public function index(){
$data['title'] = "Register";
$this->load->view("site_header_extra_js", $data);
$this->load->view("site_nav");
$this->load->view("content_register");
$this->load->view("site_footer_extra_js");
}
Another solution is to put an if in your view to load extra js files checking the path, you can use $this->uri->segment() for this.

codeigniter pass data to nested view

I have done very much research on this topic but I cannot find how i can do this. I am trying to add data to the $data parameter in a view that is being called from the controller of another view. However, any data i add to the subview via the subcontroller cannot be accessed by the subview. However, when I try to pass data to the subview via the client view, it works just fine. Most of the fixes on SO seem to reference just calling the $key in $data['key'] rather than $data so that doesn't seem really relevant here...
I have two classes:
welcome.php - a page
welcomemenu.php - a set of controls intended to
be loaded into welcome.php
Here is my Client Controller (the page that it's on, welcome.php), which stores the return val from subview $welcomemenu in its own $data array...:
<?php
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
//echo 'this is the Welcome index function';
$data['clienttestdata'] = 'data from welcome.php!';
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('pages/welcome', $data);
$this->load->view('templates/footer');
}
}
And here is the client view ("welcome_view.php" - seems simple enough. The $welcomemenu var is where I put the returns from my component class...):
<section id="allWelcomeContent" class="mainBody">
<header id="mainPageHdr" class=mainPageHdr>
<!-- other stuff from my header -->
</header>
<!-- this is where i want to put the welcome menu... -->
<section id="mainWelcomeContent" class="mainContent">
<div>
<?php echo $welcomemenu;?>
</div>
</section>
</section>
And here is the Controller for my sub-component welcomemenu.php:
<?php
class Welcomemenu extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['menu_items'] = array('About', 'Portfolio', 'Resume', 'Fun', 'Blog');
$data['testdata'] = 'data from welcomemenu.php!';
$this->load->view('welcome/welcomemenu', $data);
}
}
And lastly: Here is the sub-view that is supposed to get data from its own controller, but cannot, even though it can take data from a calling client (i.e., the $clienttestdata shows up fine but $testdata doesn't)!
<section>
<!-- TODO: make this element repeatable so content can load from controller and/or model. -->
<div id="divMenuItems">
<?php echo $clienttestdata;?>
<?php echo $testdata;?>
</div>
</section>
Still i couldn't find any proper solution. if anyone then please give me
When you're including the welcomemenu partial in your Welcome/index method, you have to remember that the view does not go through its own controller. Instead, its contents are returned as a string and stored as a parameter. It gets all of its own parameters through the ones you send to it via $data:
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
This view will thus have access to everything in $data so far - nothing extra is added through the Welcomemenu controller. So, in the above case, it will have:
array
(
'clienttestdata' => 'data from welcome.php!'
)
If you add the parameters you need to $data (as $data['testdata']), your sub-view will have what it needs.

Categories