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);
Related
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
I have a codeigniter problem. Im trying to send data from a controller , to a library , to a view.
i get this error in the view:
Message: Undefined variable: crimes
FileName: views/crime_view.php
Line: 45
while debugging , i dump the $data variable, and get:
that shows that my variables exist.
in the library , im getting the controller data by using:
$data[] = $componentData;
that would not work in this case. but if i in the library do:
$data['crimes'] = "test";
then it will work. for some reason it wont process the incomming arrays from the controller.
how can i get this to work?
full code:
function renderComponent($componentData = array())
{
$data[] = $componentData; // stores controller variables.
$data['rankDetails'] = $this->CI->user->rank_for_xp($userId);
var_dump($data);
$this->CI->load->view('components/crime/views/crime_view', $data);
}
example from the controller:
Q: How can i fix this to get it passing the variables needed? so i acually can get to use the $wait variable in the view?
You have a two dimensional array.
I think somewhere $data[] = ... must be $data = ...
to debug your arrays you could do like this:
echo '<pre>';
print_r($data);
echo '</pre>';
This shows clearly that your array is in another array...
Hello I am new to codeigniter framework, and I need help do get some data from array that is forwarded to my view.
In my controller's method I was load model who returns me data array from db, and than I pass that data to my view, and that looks like this:
function deleteAccount($data){
$this->load->model('model_admin');
$dataFromModel = array();
if($query = $this->model_admin->retrieveAccount ($data)){
$dataFromModel['records'] = $query;
$this->load->view('admin/test',$dataFromModel);
} else {
echo "No data was returned";
}
}
and in my view i manage to retrieve that data and to echo it like this:
<?php
echo "TEST page<br/>";
if(isset($records)) {
foreach ($records as $row) {
echo $row->username;
}
} else {
echo "No data";
}
?>
But I am certain that if I retrieve data from db it is either one line or no lines at all, so in my view page i would like to echo that data without foreach loop on some way, for example like this:
echo $records->username;
or:
echo $records[0]; //because it is the first argument in line
but in both ways it throws an error :
1 - first case: Trying to get property of non-object
2 - second case: Object of class stdClass could not be converted to string
This is my model:
function retrieveAccount($data){
$query = $this->db->get_where('table_users', array('username' => $data[2]));
return $query->result(); }
Can someone help me, and show me how to retrieve that data.
If you want to use $records->username this means $records must contain only one value. In this case instead of using $query->result(), use $query->row() in your model code.
You cannot echo stdClass object. $records[0] is stdClass object thus Php is giving this error. Instead this would work
echo $records[0]->username
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']));
I have the following in php:
$query = mysql_query($sql);
$rows = mysql_num_rows($query);
$data['course_num']=$rows;
$data['course_data'] = array();
while ($fetch = mysql_fetch_assoc($query) )
{
$courseData = array(
'course_name'=>$fetch['course_name'],
'training_field'=>$fetch['training_field'],
'speciality_field'=>$fetch['speciality_field'],
'language'=>$fetch['language'],
'description'=>$fetch['description'],
'type'=>$fetch['type'],
);
array_push($data['course_data'],$courseData);
}
echo json_encode($data);
when I receive the result of this script in jquery (using post)
I log it using :
console.log(data['course_data']);
and the output is :
[Object { course_name="Introduction to C++", training_field="Engineering" , speciality_field="Software", more...}]
But I can't seem to figure out how to access the elements.
I tried
data['course_data'].course_name
data['course_data']['course_name']
Nothing worked. Any ideas
When you array_push($data['course_data'],$courseData); you are actually putting $courseData at $data['course_data'][0] and therefore you would access it in JavaScript as data['course_data'][0]['course_name'].
If you only intend to have one result, instead of array_push($data['course_data'],$courseData); you should just specify $data['course_data'] = $courseData. Otherwise, you should iterate over data['course_data'] like so:
for (i in data['course_data']) {
console.log(data['course_data'][i]['course_name']);
}
You should specify the index in the first array for instance
data['course_data'][0]['course_name'];
you could make it better if you had defined the first array just as variable not a variable within an array
$data['course_data'][0]['course_name']
should do the trick. If not please send the output of var_dump($data)
Assuming the PHP code is correct, you will receive a JSON data like:
{
"course_num":34,
"course_data":[
{
"course_name":"name_value",
....
},
....etc (other object based on SQL result)
]
}
So, if you want to access to the total number of result:
data.course_num
If you want to access to the first element of the list of result:
data.course_data[0]
If you want to access to the name of the first element of the list of result:
data.course_data[0].course_name
or
data.course_data[0]['course_name']
use jquery's parseJSON method to get all the goodies out of the json object...
http://api.jquery.com/jQuery.parseJSON/