When loading one view in to another with jquery i do that.
In file view 5
load("<?php echo site_url("controller/name")?>");
I'm loading the name function that contains the view.But how to get the variables from that name function that is loading a view in to that view ?
function name(){
$a['aa'] = 1;
$this->load->view("file",$a);
}
And i wan to load the $aa variable where i'm loading the function with jquery , this is the loaded function - name, the default view is different and with different function in the controller.
Why don't you pass the $aa through the controller like this?
function name($aa = 1) {
$this->load->view('file', $aa);
}
Then you can do load('<?php echo site_url('controller/name/1') ?>');
You might want to sanitize $aa as well.
Related
How could I send data/variable from controller to other controller in codeigniter without using session?
I know how to send data from controller to other controller using session but I don't want to use it as it's giving me problem. If I use session, data I need to send will be used in many pages.
Example of the Data from controller 1 is $id (which I want to use on the other controller), but in the controller 2. If I open many pages, I only get the same data which is not what I expected. In controller 2, if I'm in page 1 I need $id = 1 while if I'm in page 2 I need the $id = 2. Any help with me appreciated. Thanks in advance.
GET -- It's your choice ?
if yes then.. use URI Class
http://example.com/index.php/news/local/1
segment(1) = "news"
segment(2) = "local"
segment(3) = "1" <-- your $id
Controller
$id = $this->uri->segment(3);
test.php Controller File :
Class Test {
function demo() {
echo "Hello";
}
}
test1.php Controller File :
Class Test1 {
function demo2() {
require('test.php');
$test = new Test();
$test->demo();
}
}
But using require like this in codeigniter is not a good idea...
I'm currently learning the ropes of the MVC pattern and came across a problem I can't seem
to fix in a way I want and is in line with the MVC pattern.
I have set up the router, controllers and views up successfully.
The only thing I don't really get is the use of the Model. I know it's supposed to
serve the Data to the view, and here it is I have a problem.
I want to pass a function thru my view method, but it executes before it should be.
is there a way
I will try to be as specific as possible about the situation so sorry for the long post.
The controller class is this:
class Controller{
private $tpl_name = 'default';
public function model($model){
require('../admin/model/'.$model.'.model.php');
return new $model();
}
public function view($page_title,$file_paths,$params,$data = []) {
// takes an array with the file paths
$this->content = $file_paths;
$tpl_name = $this->tpl_name;
require_once('templates/'.$tpl_name.'/header.php');
require_once('templates/'.$tpl_name.'/nav.php');
require_once('templates/'.$tpl_name.'/content-top.php');
foreach ($file_paths as $content){
require_once('view/'.$content);
}
require_once('templates/'.$tpl_name.'/content-bottom.php');
require_once('templates/'.$tpl_name.'/footer.php');
}
}
The view renders the template I want, takes parameters from the router and, the data that
needs to be handled in the desired view. So far so good.
I want to serve my posts in my admin panel that displays a table of all the posts in the DB.
I have written a method that fetches the data, and a method that writes the data.
class Post{
......
//other functions above
public function displayPosts(){
// get's all the posts form the data base, returns an object array
$posts = Post::fetchContent('posts',0);
// array get's passes to the write function which will write out the data.
$writer = Post::write($posts);
}
static public function write(Array $posts){
foreach($posts as $single){
// for each object in the array, assign the vars so the view can handle them
// to create a single row in the table for each object:
$trashed = $single->getTrashed();
$id = $single->getID();
$title = $single->getTitle();
$category = $single->getCategory();
$content = $single->getContent();
$author = $single->getAuthor();
$date = $single->getDate();
$approved = $single->getApproved();
$dbt = $single->getDbt();
// This is a template which represents a table row with the post data I need.
require('view/content_table.php');
}
//controller file (needs to moved to other file later): handles approve/remove/edit/delete actions.
require('view/manage_content.php');
}
}
Now we have arrived at the problem:
When I call the model in my controller and render the view, it will execute immediatly
before the rest of my view loads, resulting in errors, although it displays the data,
it is not in my template, but above it, just in plain text.
errors:
Notice: Undefined variable: _SESSION in /Volumes/HDD Mac/Websites/server/admin/view/content_table.php on line 8
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at ...)
class Dashboard extends Controller {
public function index($params = null){
$model = $this->model('Post');
$posts = $model->displayPosts();
// view takes: page_title,[array of view files],params from the router,array of data from model
$this->view('Dashboard',['admin.php'],$params,[ 'posts' => $posts]);
}
}
Before I was trying to use MVC I just outputted this in my view:
And it worked just fine.
Non relevant HTML above
$posts = Post::fetchContent('posts',0);
// array get's passes to the write function which will write out the data.
$writer = Post::write($posts);
Non relevant HTML below
But now when I pass the display post function, I just want to do this in my view:
echo $data['posts'];
which doesn't work because it already executed my Write function.
The only way I could work around like this was by adding the content of my write function to the view,
and only pass the fetchContent method to my view method (this will output an array of objects).
But since I need this info in two place I dont want to repeat this code, I would prefer echoing
all out.
Non relevant HTML above
$posts = $data['posts'];
foreach($posts as $single){
// for each object in the array, assign the vars so the view can handle them
// to create a single row in the table for each object:
$trashed = $single->getTrashed();
$id = $single->getID();
$title = $single->getTitle();
$category = $single->getCategory();
$content = $single->getContent();
$author = $single->getAuthor();
$date = $single->getDate();
$approved = $single->getApproved();
$dbt = $single->getDbt();
// This is a template which represents a table row with the post data I need.
require('view/content_table.php');
}
//controller file (needs to moved to other file later): handles approve/remove/edit/delete actions.
require('view/manage_content.php');
Non relevant HTML below
Is it bad practise to just skip the use of the Model here and do it like this:
Non relevant HTML above
$posts = Post::fetchContent('posts',0);
// array get's passes to the write function which will write out the data.
$writer = Post::write($posts);
Non relevant HTML below
Or is there a way to rewrite my Post::Write function? Or just use the foreach loop in the view?
Thank you all for taking the time!
If you need more info, just ask:-)
I have made a helpers file in my /app folder which contains the following:
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
echo $C['business_name'];
This works, but if I try
echo $C['business_name'];
In one of my views I get an error of $C undefined. I have added the helpers file to my start/global file and I know it works...
What steps should I take to use this variable in my views?
You need to pass data directly into the view via the second parameter of View::make or alternatively View::make('someBlade')->with(data);
So in your case it might be something like:
View::make('someBlade', $C);
If you really, really want globals, you can do this for views:
View::share('c', $C);
http://laravel.com/docs/4.2/responses
I think you need to create a function in helper and call that function in view.
it will automatically display value of this variable but you need change "echo" replacing with "return" in function last line.
function xyz()
{
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
return $C['business_name'];
}
Call this function in your view like this. {{xyz()}}
if you are returning array {? $abc=xyz(); ?} make blade filter not echoing value pass this function to array variable and show like this {{$abc['business_name']}}
Hi Here is my Loader and Index Function inside my Controller
While calling the index() function I am assigning the $menu['menu'] and $menu['menu'] at the same time i am the value for $data and sending it to the loader function.
In the Loader function
I am calling the header (which has css,js files)
I am calling the view index and sending the value $data into it
I am calling the footer
But in the index view even i didn't send the value $menu, i am able to print the $menu and $title but i can't able to print the $data.
What is the mistake i am doing. How can i get the value of $data inside the index view
Here is my Code :
public function loader($url,$menu,$data)
{
$this->load->view('assets/header',$menu);
$this->load->view($url,$menu,$data);
$this->load->view('assets/footer');
}
public function index()
{
$menu['menu']="home";
$menu['title']="Home Page";
$data='somedata';
$this->loader('index',$menu,$data);
}
When you pass value at view you should pass it as array and the array key will be received as variable at view.In your case you need to replace the line $data='somedata' with;.
$menu['data']='somedata';
You will receive it as $data inside view
You also need to rewrite the line $this->load->view($url,$menu,$data);
like this
$this->load->view($url,$menu);
3rd parameter of load->view function is either true or false;
you can see documentaion
I just created this function in the model to see who im following in my social network... how do i call it in the view??
function isfollowing($following){
$user_id = $this->session->userdata('uid');
$this->db->select('*');
$this->db->from('membership');
$this->db->join('following', "membership.id = following.tofollow_id");
$this->db->where("tofollow_id","$following");
$this->db->where("user_id", "$user_id");
$q = $this->db->get();
if($q->num_rows() > 0) {
return "yes";
} else {
return "no";
}
}
Now in my VIEW how do i call it being that i had already made a function to get the current logged on user's id and that is equal to $r->id
How do i call it here?? what goes after the "==" in that if statement?
THE VIEW
<?php if ( $r->id == ): ?>
It is not a good practice to call model function from view.
There are some alternatives about it. You can use anyone you like.
First
When you are loading a view call your model function and pass it in a variable
than this variable will be passed to view.
Controller
$following_status = $this->my_model->isfollowing($following);
$data['following_status'] = $following_status;
$this->load->view('my_view',$data);
View
<p>$following_status</p>
Secound
If you want to be independent of model you can create helper which you can
use anywhere in the application. You will have to create a CI instance to
get it working.
custom_helper.php
function isfollowing($following)
{
$CI = get_instance();
$user_id = $CI->session->userdata('uid');
$CI->db->select('*');
$CI->db->from('membership');
$CI->db->join('following', "membership.id = following.tofollow_id");
$CI->db->where("tofollow_id","$following");
$CI->db->where("user_id", "$user_id");
$q = $CI->db->get();
if($q->num_rows() > 0) {
return "yes";
} else {
return "no";
}
}
View
//load the custom helper before using it (you can autoload of in autoload.php)
//or use common way $this->load->helper('custom');
<p>isfollowing($yourparameter)</p>
You do the following:
(1) Load your model in the controller that creates your page or auto load it
(2) In your view, type something like:
$this->The_custom_model->isfollowing($theinputvariable)
where The_custom_model is the model where you defined the isfollowing() function.
$theinputvariable is the appropriate argument value for your function. Keep in mind that you have specified an object as the argument to your function so you need to think about that.
this is an amended version to what raheel posted showing an if check - probably not necessary for your question, but to give you some things to think about...
// check to see if anything come back from the database?
if ( ! $data['following_status'] = $this->my_model->isfollowing($following) ) {
// nothing came back, jump to another method to deal with it
$this->noFollowers() ; }
// else we have a result, and its already set to data, so ready to go
else {
// do more here, call your view, etc
}
databases can go down even if the web page is working so its good to get in the habit of checking the results. the more error checks you can do in your controller and models, the cleaner your view files will be.
To access model into your view you first load it into autoload file like this
$autoload['model'] = array('model_name');
then in view you can get it by using this line of code
$this->model_name->isfollowing($following)
in isfollowing you will pass your tofollow_id