Im trying to send 2 values to the view to be used from the controller.
$data1= $this->datalib->is_data();
$data2['name']=$this->namelib->getName();
Im looking to send data1 and data2 so i can use both in two places. What changes i need?
$this->load->view('person_view', array('value'=> $data1));
thanks.
You just add $data1 to the $data array, like this;
$data['name'] = $this->namelib->getName();
$data['is_data'] = $this->datalib->is_data();
Then, pass the $data array to the view;
$this->load->view('person_view', $data);
You will be able to access the data array from the view, like this;
print_r($name);
print_r($is_data);
Hope this helps.
try this,
$data['info1']= $this->datalib->is_data();
$data['info2']=$this->namelib->getName();
Pass array $data to view,
$this->load->view('person_view',$data);
In your view person_view.php you can get the values of info1 and info2. Like,
print_r($info1);
print_r($info2);
This will do the job
$this->load->view('person_view', array('data1'=> $data1,'data2'=>$data2));
You might want to do something like this
$data['temp']= $this->datalib->is_data();
$data['name']=$this->namelib->getName();
Then pass data to view
$this->load->view('person_view', $data);
In view you can use that two variables as $temp and $name.
echo $temp;
echo $name;
Change the code to
$data['data1']= $this->datalib->is_data();
$data['name']=$this->namelib->getName();
Then pass $data to the view
$this->load->view('person_view', $data);
In the view, use $data1 and $name to access these values.
use this one
$dataToShow['data'] = $this->datalib->is_data();
$dataToShow['name'] = $this->namelib->getName();
then to load view
$this->load->view('person_view', $dataToShow);
so you can access your data on your view with using this variable
$data and $name
hope will help cheers
figured it out. this was the change i needed, ty #Rupam was looking for something like that.
$this->load->view('person_view', array('value'=> $data1,'name'=>$data2['name']));
Related
Normally data retrieved as an object from database in Laravel when we use get() function. But I want to retrieved data as an array.
In CodeIgnitor we use get_array(). What we use in Laravel ?
I have already tried with toArray(). But no result.
$doctor_result = Doctor::select('*')->toArray();
How to solve that?
I got result. just change the code
$doctor_result = Doctor::all()->toArray();
You can also do like this.
$result = DB::table('tableName')->get();
$resultArray = $result->toArray();
second way, but some tricky.
$resultArray = json_decode(json_encode($result), true);
I am sending array through the controller in compact.
$medicines = Medicine::all()->pluck('medicine_name')->toArray();
return view('orders.order',compact('medicines'));
I want to display array in the blade without iterating,
output should be like $medicines = ['medicine1','medicine2','medicine3']
how to achieve it?
if I return $medicine variable I am getting the expected output how do I pass into the view?
If you want just output you can try as
<?php echo "\$medicines = ['".implode("','",$medicines)."']"; ?>
Here is Live demo : https://eval.in/915767
Try to implode the result first then assign that
$medicines = Medicine::all()->pluck('medicine_name')->toArray();
$medicines = implode(",",$medicines);
return view('orders.order',compact('medicines'));
in your view add this code
{{ $medicines}}
If i'm understanding you...
you can use json_encode to "convert" the array to string and then print it ... ( use the {!!} to print and not {{}} )
in the tempalte add this:
$medicines = '{! json_encode($medicines) !}'
you can event use implode if you want ...
#php echo '$medicines = '[\''.imlode("', '", $medicines).'\']' #endphp
So I have this line of code in my controller:
$this->load->view('sampleview',$result);
So when I go into my view and try to echo result, either by foreach or directly writing
print_r($result);
it shows an error that it is an undefined variable.
But when I put the print_r($result); on my controller like this below:
function show()
{
$this->load->view('sampleview',$result);
print_r($result);
}
It would print on my sampleview page where I was redirected. I’m confused why this is happening.
EDIT
The whole controller would be: I have another view which I click an anchor tag with segment(3) as an id. Then I query that id to my model then pass it to another view for display.
$id = $this->uri->segment(3);
$result = $this->model->show($recipe_id);
if($result!=null)
{
$this->load->view('Admin_recipe_edit',$result);
}
You $result array will be extracted in your view. if you want as an array, then do it as
$data["result"] = $result;
$this->load->view('sampleview',$data);
Then do print_r($result); in view.
This is how you should have to make a array of data that you want to access on your view
// Your array
$result['Data_Array'] = array(....);
$this->load->view('sampleview',$result);
print_r($result);
Now Access like this in your View Section:
View
print_r($Data_Array);
Dude try to put $result in an array example$view_data['$result']=$result; then load the view exmaple $this->load->view(your_view,$view_data);. and in the view page try to print $result.exmaple print_r($result); .It will work I think so.
In CodeIgniter
$Data['result'] = $ArrayData;
$this->load->view('viewfile',$Data);
In View
print_r($result);
I am little bit struggling in setting array in session.
Here is my code:-
Controller:
function taketest(){
$this->load->model('', '');
$questions_for_test = $this->session->userdata('questions_for_test');
$difficulty_level = $this->session->userdata('difficulty_level');
$question_id = $this->session->userdata('question_id');
if($question_id==""){
$question_id = $this->myjcat->returnRandomQuestion($questions_for_test,$difficulty_level);
$this->session->set_userdata('question_id',$question_id);
}
$question_details = $this->myjcat->getQuestion($question_id);
}
Model:
function returnRandomQuestion($questions_for_test, $difficulty_level){
$question_id = array_rand($questions_for_test[$difficulty_level], 1);
$used_question=array();
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
return $questions_for_test[$difficulty_level][$question_id];
}
But when I call:
$used_questions = $this->session->userdata('used_questions');
in controller
in the controller it will not return me an array.It gives me last value stored in it.
I could be misreading things, but it looks like you are only storing one value.
// this code:
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
// is the same as this code
$this->session->set_userdata('used_questions',$questions_for_test[$difficulty_level][$question_id]);
You're probably looking for this:
// fetch the stored copy first.
$used_questions = $this->session->userdata('used_questions');
if(!is_array($used_questions)) $used_questions = array();
// note the []
$used_questions[] = $questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
You can set array values in session data like this :-
$this->session->set_userdata('used_questions', json_encode($used_questions));
And retrieve the data as :-
json_decode($this->session->userdata('used_questions'));
If you want the retrieved array data as associative array :-
json_decode($this->session->userdata('used_questions'), true);
Hope it hepls you :)
The problem is that $used_questions is storing the value stored in $questions_for_test[$difficulty_level][$question_id] and not the array.
So do this $this->session->set_userdata('used_questions',$questions_for_test);
This is because the data you pass to the $used_questions is a value.
You might want to do something like this:
array_push($used_questions, $questions_for_test[$difficulty_level][$question_id]);
*appending / adding a new value to an array
I want to capture all posted data in one single line like name=asd&age=12&city=something and as an array while the data were posted by using form. (I don't wanna capture values like "$name=$_POST['name']")
(Ques-1: as a single line.
Ques-2: as an array.)
How can I do that ?
-Thanks.
I don't quite understand what you want, but I think you're looking for the following:
On a single line:
$raw_data = file_get_contents("php://input");
in an array
$array_data = $_POST // this is already an array?
$postAsLine = file_get_contents("php://input");
$postAsArray = $_POST;
echo $_SERVER['QUERY_STRING'] for current GET query
http_build_query for any array
in your case http_build_query($_POST)
You can use http_build_query($yourKeyValArray) which creates a query string from an array.
it sound like it:
$getAll = serialize($_POST);
$getAsArray = $_POST;
GOOD LUCK