Use view variable in controller? - php

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.

Related

Dynamic accessing of the php list

I have 2 tables in the database employee(Ename,id,manager_id) and manager(name,id).1 manager has multiple employees under him.
The first line extracts all the employees under 1 manager and creates a list of it. Now, I wish to extract the name of each employee name from the retrieved employee id. How do i access each element of the list? This is what i have tried and it throws errors
$emplyeeId=DB::table('employee')->where('manager_id', $givenManagerId)->lists('id');
for ($i=0;$i<listCount;$i++)
{
$Ename = DB::table('employee')->where('id', $emplyeeId($i))-> value('Ename');
}
By looking at the docs (and scroll down a little bit, you find a method named pluck. This will return an array with all the values of the given column.
In your case this would be:
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
// This will return the following array:
['Kevin', 'Tom', 'Tina', ...]
Laravel pluck method ,for example get just name from data
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
Second method with array_filter
$names = DB::table('employee')->where('manager_id', $givenManagerId)->get()->toArray();
$justNames=array_filter($names, function ($ar) { return $ar['Ename']; });

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

Foreach loop seems to destroy the array in Laravel / PHP 7

I have a simple CRUD item called Filters. In here each filter is assigned to a category with a foreign key. What I am trying to do is loop through each foregin key to get the category name rather than id to display to the user.
I get all filters first and performed a die/dump to check all results were there and they are.
When trying to assign the category name to the correct array item I get this error:
"Indirect modification of overloaded element of App\Filter has no effect"
So to check what's happening I have die/dumped inside the foreach loop and the exact same data has now disappeared. Even if I just put a foreach loop in with no modiifcaiton of the original array, when I pass this back to the view it has been unset.
AM I being very naive and not realising something this foreach loop does that destroys this data???
I have copied my code below and commented where the dd works and doesn't;
public function show()
{
$filter = [];
$filter['filters'] = Filter::all();
//dd($filter['filters']); --this works fine here
foreach($filter['filters'] AS $key => $filter){
//dd($filter['filters']); --this returns null here
$category = Category::where('id', $filter->category)->first();
$filter['filters'][$key]->category = $category->category;
}
return view('admin.crud.filters.index')->with('filter', $filter);
}
Don't re-initialize a variable or different data-type with same name
Try to change the $filter to something else in foreach loop. Because you already have an array with same name.
Do something like:
foreach($filter['filters'] AS $key => $f){...}

Replacing a value in an associative array?

I have this:
foreach ($books as $book)
{
$catname = get_category_name($book['category']);
$book['category'] = $catname;
}
print_r($books);
Right now, $book['category'] represents an integer value, which references the ID of a row in a category table of the database (e.g. ID 1 = Fiction). As you can guess, get_category_name($id) takes an ID and finds the correct row, and returns the name of the category. The function works correctly, and if i print_r($catname) in the foreach it prints the names of the categories, like it should.
I have an associative array called $books that gets filled from every row in the book table in the database. What I want to do, is take the category integer value of each book element and use get_category_name($id) to replace that integer value with the actual category name.
The problem I am having is that I cannot replace the integer value that already exists at $book['category'] with the actual category name which resides in $catname. When I use print_r($books) to see if the changes were made in the foreach, they confirm the changes do not get made.
How can I fix this?
I think you want something like this?
foreach ($books as &$book)
{
$catname = get_category_name($book['category']);
$book['category'] = $catname;
}
print_r($books);
Notice the &$book.
You either use foreach($books as &$book) and edit $book. This way you have a reference to the real book element inside the array in stead of a copy of it. Beware that if you want to do this multiple times on the same array, you will need to reset the array each time after use. See: http://nl1.php.net/reset
Or you use foreach($books as $key=>$value) and edit $books[$key]. This way you get copies of the books and their keys. But you then use the key on the original array.
THe problem is that in fact you do not change $books but $book, that is why you cant see changes in $books. Possible solution:
foreach ($books as $booksId => $book) //$booksId will contain current index in $books
{
$catname = get_category_name($book['category']);
// now you can change correct index in $books:
$books[$booksId]['category'] = $catname;
}
Edit: #Cardinal solution with refference is also correct.

Pass object parameter to buildQuery in action.class

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

Categories