Pass object parameter to buildQuery in action.class - php

I need to pass an object-id to a build query so i can list a collection of items from an admin autogenerated module.
here is the buildQuery:
protected function buildQuery($futbolista_id)
{
$q = parent::buildQuery('p');
$q->leftJoin('p.mdTrayectoriaFutbolista a')
$q->where('a.md_futbolista_id=?', $futbolista_id)
$q->addOrderBy('position asc');
return $q;
}
The thing is that i want to call this list form an editSuccess.php template and pass the object through an array, so the buildQuery get the parameter, like this:
<?php include_partial('trayectoria/list', array('futbolista_id' => $this->object)) ?>
FYI i need to get the football matches of a current football player and embed it to the edit form of the player, so i can add and edit them in the current football player form.

I had the same problem and solved in the following way: I spent Variable not flash but for a session variable as follows
in action
$object = Table::Function ();
$this->getUser->setAttribute('object', $object);
in partial
$object = $sf_user->getAttribute('object');
$sf_user->getAttributeHolder()->remove('object');
I hope this idea will solve

Related

pass data from controller to view inside another view in codeigniter

I fetched data from model in controller . i want to display this data in view inside another view. its showing blank page.
here is my code..
controller -
public function Listblog()
{
$listblog=$this->Login->listblog();
$listblogwithpage=$this->load->view('list_blog',$listblog);
$this->load->view('Welcome_message',$listblogwithpage);
}
model -
public function listblog()
{
$query=$this->db->get('new_employee');
return $query->result();
}
To assign a view to a variable the 3rd param must be true:
$listblogwithpage=$this->load->view('list_blog',$listblog, true);
Further the 2nd param must be an array. E.g. $data['listblog'] = 123;
$var = $this->load->view('somepage', $data, true);
This applies to any usage of view.
If you want to pass data from a controller to the first view and then have the second view pass data to the second view, you should do the following, always remembering that CI expects data passed to a view to be in form of an array. Take this and feel free to adapt it to suit your needs
In controller:
// populate an array and pass it to the first view
$first_view_data = array(
'listblog' => $listblog_query_result,
);
$this->load->view('firstview', $first_view_data);
In the first view, populate a new array with whatever data you need and call the second view from within the first one, passing the second data array:
$second_view_data = array(
'second_data_var' => $variable,
'other_data_var' => $other_var,
);
$this->load->view('second_view', $second_view_data);
CI is intelligent enough to let you call a view from within a view and pass data from each to the next in this way. Just remember, it has to be an array.
Using the data:
In the first view you'd call $listblog
In the second view, you would access $second_data_var and $other_data_var
$listblog $second_data_var and $other_data_var each could be single variables, arrays, objects and mostly anything as long as they are passed to the view as elements of an array
try this way.
//Controller
function Listblog() {
$data = array();
$data['listblog']=$this->Login->listblog();
$this->load->view(''list_blog',$listblog');
}
in view page you have to call that data array $listblog

Use view variable in controller?

In my controller I get all the entries from Athletes table, names, surnames etc... Save them in the array and pass them to my view. In my view file I then list all the entries using foreach . All the Athletes are listed fine...
However I want to check every Athlete and see if they exist in the table My Team! So in my foreach loop in my view I use this function
$this->my_team_model->exist($this->session->userdata("id"), $athletes[$i][6]) === TRUE
First parameter of exist() function is user id I get from session, second parameter is id of Athlete from Athletes table ($athletes[$i][6]).
So you see I really need that second Athlete id parameter! But I can only get it when the Athletes are listed one by one in my view. But I know I shouldn't use functions and logic in my view files. How can I do this in my controller?
you should be using that same foreach loop in your controller and get the required data there itself, append the data to the array you pass to the view:
$array = $this->some_model->all_the_data_of_atheletes();
foreach( $array as $key => $eachAthelete ){
$exists = $this->model->call_to_function( $this->session->userdata("id"), $eachAthelete['id'] );
$array[$key]['exists'] = $exists;
}
$this->load->view('some_view', $array);
Now, in your view you will get an exists element for every iteration. Hope it helps you.

Codeigniter - Use the id from a query result in a model to query in a different function in the same model

I'm using codeigniter to code my site and I'm running into a roadblock. I know how to do this in regular, non "MVC", not OOP PHP, but am struggling on it in Codeigniter.
I have blog_model.php, which has a function to retrieve the datetime from my database, explode it into an array so that I can work with it outside the model and feed it into CSS where I have individual calendar icons for it. Each calendar icon is loaded by the month number in the view (<div class='calendar-icon-$stats['date']). This function also pulls the amount of comments from that individual post and outputs it into an array so that I can show it in the view.
public function get_stats($id) {
$this->db->select('id,datetime')->from('blog_posts')->where('id',$id);
$dquery = $this->db->get();
$dquery = $dquery->row_array();
$date = date('Y-m-d', strtotime($dquery['datetime']));
$stats = explode("-", $date); // This makes $stats[0] the year, $stats[1] the month and $stats[2] the day.
$stats['time'] = date('H:i', strtotime($dquery['datetime']));
$stats['comcount'] = $this->db->get_where('blog_comments', array('blogid' => $id));
$stats['comcount'] = $stats['comcount']->num_rows();
return $stats;
}
There is also a function to retrieve the three most recent entries:
public function get_blog_last() {
$query = $this->db->order_by('id desc')->get('blog_posts',3);
return $query->result_array();
}
This code is then loaded into my controller and sent to the view to be displayed:
$data['blog'] = $this->blog_model->get_blog_last();
$data['stats'] = $this->blog_model->get_stats($data['blog']);
$this->load->view('index',$data);
The problem I face is how to get the get_stats() function to run for every entry I have on the index page, where the last three entries are displayed. So far I can only get it to run for one of them, therefore all three of the entries on my front page have the same date, the same time and the same amount of comments. I figured putting the code in a model would save myself from repeating myself when I had to load it for the archives page (where I display all the posts from the month) and the main entry page where I just display that entry and its comments.
So, the ultimate question here is:
How do I run get_stats for every entry I have on a given page?
I'm also having a bit of issue figuring out the correct value to pass to my get_stats() function.
Any guidance would be appreciated. Thank you in advance.
If I'm understanding correctly, you need to call get_stats for each of the three entries that you receive in get_blog_last. If that is the case, just change get_blog_last to this:
public function get_blog_last() {
$query = $this->db->order_by('id desc')->get('blog_posts',3);
$entries = $query->result_array(); // get the latest entries array
foreach ($entries as $index => $entry) { // loop through those entries
$stats = $this->get_stats($entry['id']); // call this model's `get_stats` method
$entries[$index]['stats'] = $stats; // add a `stats` key to the entry array
}
return $entries;
}
Why don't you put
$this->blog_model->get_stats($data['blog']);
inside loop ? ( i'd rather use normal loop )
example :
$stat_list = array();
for($i=0;$i<count($data['blog']);$i++){
$stat_list[] = $this->blog_model->get_stats($data['blog'][$i]);
}
$data['stats'] = $stat_list;
and in your view, you should try the same to print out each $stat_list

PHP MVC loop in the view

I consider myself as a php beginner, so it may be possible that this question is too easy for someone, but I got really confused on how to solve it. I am trying to loop something from the database in my views. So, in a quick way I solved it like this:
I've created a function in my model that does the loop and in the same time is creating the html and saves it in a variable. Then, I get that variable from my controller and I pass it in my view. But, it seems that this is not a good way to solve it, since if I want to change my html I need to enter my model function instead some of the view files.
Then, I've created another function in my model that looks like this:
function displayUsers() {
$sql = $this->pdo->prepare('select * from user');
$sql->execute();
while($row = $sql->fetch())
$results[] = $row;
return $results;
}
Now... I take the result in my controller, and send it in the view, but then... I don't know how to extract the results from my variable. I have done something like this:
while($output) {
foreach($output[$i] as $key => $value)
$data[$key] = $value;
echo $data['email'];
$i++;
}
But then, in the end it says to me undefined offset, which means I am referring to an array key that doesn't exist. Can anyone help me on how to solve this issue?
Proper MVC shouldn't have any output in the model or the controller.
Ideally you would have a model that just gets the raw data and returns it in the controller. The controller can then build up an array of values that we'll call data. For example:
Controller
$data['users'] = $this->MyModel->getusers(); // Getting the users from your model
$data['posts'] = $this->MyModel->getposts(); // Getting the posts from your model
$this->getTemplate('userdisplay', $data); // Get the template userdisplay and pass data
This gets the data from the model, and then assigns it to a key within the "data" variable. You can then pass the data variable into the template. You'll then have two variables to work with in the template, $users and $posts.
You'll need a "getTemplate" function that properly maps the data array to individual variables for use in the template, but all of the display should be located in the template.
To answer your specific question at the end, something like this should work in the template:
if (count($users) > 0) {
foreach ($users as $person) {
echo $person['email'];
}
}
You should be able to do this:
foreach($output as $row) {
echo $row['email'];
}

Making anchor load page containing data from referenced row in DB

I'm trying to learn the code igniter library and object oriented PHP in general and have a question.
I've gotten as far as making a page which loads all of the rows from my database and in there, I'm echoing an anchor tag which is a link to the following structure.
echo anchor("videos/video/$row->video_id", $row->video_title);
So, I have a class called Videos which extends the controller, within that class there is index and video, which is being called correctly (when you click on the video title, it sends you to videos/video/5 for example, 5 being the primary key of the table I'm working with.
So basically all I'm trying to do is pass that 5 back to the controller, and then have the particular video page output the particular rows data from the videos table. My function in my controller for video looks like this:
function video()
{
$data['main_content'] = 'video';
$data['video_title'] = 'test';
$this->load->view('includes/template', $data);
}
So ya, basically test should be instead of test, a returned value of a query which says get in the table "videos", the row with the video_id of "5", and make $data['video_title'] equal to value of video_title in database...
Should have this figured out by now but don't, any help would be appreciated!
I don't know if I'm too late but maybe this can solve your problem...
put this in your video() function
data[$query] = $this->db->query("SELECT * FROM videos WHERE video_id = 5");
and then that in your video_view file...
if ($query->num_rows() > 0)
{
$row = $query->row_array();
echo $row['title'];
echo $row['something'];
echo $row['somethingElse'];
}
this is a good resource: http://codeigniter.com/user_guide/database/index.html
hope that helps...
and please someone edit the question because it's too hard to read...
What you need is to understand how the URI Class works
Basically:
$default_url_args = array('video');
$url_args = $this->uri->uri_to_assoc(3,$default_url_args);
$video_UID = $url_args['video'];
and then something like
$the_video = $this->videos_model->get_video_by_UID($video_UID);
You could use the URI Class, or you can do the following:
function video($video_id)
{
$data['main_content'] = $this->videoprovider->get_video( $video_id );
$data['video_title'] = 'test';
$this->load->view('includes/template', $data);
}
In other words, with functions inside classes that extend Controller, you can add parameters to those functions and CI will automatically pass in the URI items in order to those parameters.
function generic_function_in_controller($item1, $item2, ...)
{
// would receive as: http://example.com/controller/generic_function_in_controller/item1/item2
}

Categories