Codeigniter : Parse array passed to view - php

i've to parse an array and print its reslut in a table ,
this is how i am passing array to view in
controller
public function history(){
$history_result= array();
$history_result = $this->user_model->user_history($this->user_id,0);
$this->result_set['data'] = $history_result;
$this->load->view('test', $this->result_set);
}
in view if i var_dump $data i get
array (size=1)
0 =>
object(stdClass)[19]
public 'id' => string '1' (length=1)
public 'user_id' => string '1' (length=1)
public 'sender_name' => string 'test' (length=14)
public 'sender_mobile' => string '12323' (length=10)
public 'receiver_name' => string 'sfsf' (length=4)
public 'sender_location' => string 'sfsf' (length=4)
public 'receiver_location' => string 'sfsfs' (length=5)
public 'receiver_mobile' => string '0' (length=1)
public 'is_urget' => string '0' (length=1)
public 'is_assigned' => string '0' (length=1)
public 'request_type' => string '0' (length=1)
public 'attachment_id' => string '' (length=0)
public 'status' => string '0' (length=1)
my question is how do i access each item out of this result so that may print in a html table like this
<tr>id : 1</tr>
<tr>sender name : name..</tr>

If you have this in your model
return = $this->db->get()->row();//one result
In your view you can display like
$data->id . '<br>' . $data->user_id . '<br' . $data->sender_name ...
if in your model get a multiple rows like
return = $this->db->get()->result();//as many as you have
In your view
foreach($data as $item){
echo '<tr>';
echo '<td>'.$item->id.'</td>';
echo '<td>'.$item->user_id.'</td>';
echo '<td>'.$item->sender_name.'</td>';
...
echo '</tr>';
}

Related

Access Special Character Key from STD array in PHP [duplicate]

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?
<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

how to retrieve constituency area from uk ons open data geography portal boundaries data attribute st_area(shape) [duplicate]

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?
<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

fetching data from JSON string using php * syntax [duplicate]

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?
<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

How to print properties and key value in array

I need to access the keys and properties of the arrays. I am bit confuse, I don't know how to handle this easily.
This is the code I run,
foreach ($posts as $key=> $value){
if($value->total_skill!='na'&& $value->total_skill!='0'){
$selcted = $wpdb->get_results("SELECT `$selection` FROM wp_skilllist WHERE First_name = '$value->First_Name' ");
var_dump($selcted);
}
I get following result. I notice that there are lot of arrays inside the arrays. I need to access
propeties and print their results.
As an example
FMS_Web_tec_HTML 4
FMS_Web_tec_CSS 3
FMS_Web_tec_XML 4
FMS_Web_tec_JavaScript 2
array (size=1)
0 =>
object(stdClass)[257]
public 'FMS_Web_tec_HTML' => string '4' (length=1)
public 'FMS_Web_tec_CSS' => string '3' (length=1)
public 'FMS_Web_tec_XML' => string '4' (length=1)
public 'FMS_Web_tec_JavaScript' => string '2' (length=1)
array (size=1)
0 =>
object(stdClass)[258]
public 'FMS_Web_tec_HTML' => string '3' (length=1)
public 'FMS_Web_tec_CSS' => string '3' (length=1)
public 'FMS_Web_tec_XML' => string '2' (length=1)
public 'FMS_Web_tec_JavaScript' => string '2' (length=1)
array (size=1)
0 =>
object(stdClass)[257]
public 'FMS_Web_tec_HTML' => string '3' (length=1)
public 'FMS_Web_tec_CSS' => string '2' (length=1)
public 'FMS_Web_tec_XML' => string '3' (length=1)
public 'FMS_Web_tec_JavaScript' => string '2' (length=1)
i think your trying to access the data set in $selcted
foreach ($selcted as $sel) {
$vars = get_object_vars($sel);
foreach ($vars as $key => $var) {
echo $sel->$key;
}
}

json decode and property accessing fails [duplicate]

I'm using a PHP class someone wrote to interface with the BaseCamp API.
The particular call I'm doing is to retrieve the items in a todo list, which works fine.
My problem is, I'm not sure how to access just the todo-items property of the object that is returned. Here's the var_dump of the returned object:
object(stdClass)[6]
public 'completed-count' => string '0' (length=1)
public 'description' => string 'Description String' (length=89)
public 'id' => string '12345' (length=7)
public 'milestone-id' => string '' (length=0)
public 'name' => string 'Error Reports' (length=13)
public 'position' => string '1' (length=1)
public 'private' => string 'false' (length=5)
public 'project-id' => string '58904' (length=7)
public 'tracked' => string 'false' (length=5)
public 'uncompleted-count' => string '1' (length=1)
public 'todo-items' =>
object(stdClass)[3]
public 'todo-item' =>
object(stdClass)[5]
public 'completed' => string 'false' (length=5)
public 'content' => string 'content string here' (length=133)
public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
public 'creator-id' => string '23423' (length=7)
public 'id' => string '234' (length=8)
public 'position' => string '1' (length=1)
public 'responsible-party-id' => string '2844499' (length=7)
public 'responsible-party-type' => string 'Person' (length=6)
public 'todo-list-id' => string '234234' (length=7)
public 'complete' => string 'false' (length=5)
How can I access the todo-items portion of this object?
<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);
So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:
echo $x->{'todo-list'};
Another possibility:
$todolist = 'todo-list';
echo $x->$todolist;
If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.
public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
Try this simplest way!
$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

Categories