parsing data from the database

38 views
Skip to first unread message

ste...@gmail.com

unread,
Aug 10, 2016, 8:57:42 AM8/10/16
to Joomla! General Development
Sorry if this is a very basic question. I am trying to add a view to a component. I have a list of users and the data is being shown but it is in raw format.

using
<?php echo $this item->pto;?>

I get the data

{"date_of_hire":["06-05-2012 "],"prior_year_carry_over":["14"],"current_year_accrual":["15"],"pto_days_used":["6"],"pto_days_remaining":["23"]}

But I am having an issue displaying each item individually

I keep receiving a message about trying to get property of a non object when I try to read each item.


<?php echo $this item->pto->['date_of_hire'];?>

I know its probably a simple syntax put I am stumped, I appreciate the help.

Steve



Michael Babker

unread,
Aug 10, 2016, 9:03:17 AM8/10/16
to joomla-de...@googlegroups.com
You need to json_decode() your data first.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send email to joomla-dev-general@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Hannes Papenberg

unread,
Aug 10, 2016, 9:03:28 AM8/10/16
to joomla-de...@googlegroups.com
$item = json_decode($this->item->pto);
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.

ste...@gmail.com

unread,
Aug 10, 2016, 11:43:45 AM8/10/16
to Joomla! General Development
Michael, Hannes thank you for the nudge in the right direction.

I have done this and now get an error  undefined property  Viewpto::$item
$item = json_decode($this->item->pto);



I sure appreciate your helping out I am a complete noob but trying to learn as  I go
Steve


On Wednesday, August 10, 2016 at 9:03:28 AM UTC-4, Hannes Papenberg wrote:
$item = json_decode($this->item->pto);

Am 10.08.2016 um 14:57 schrieb ste...@gmail.com:
> Sorry if this is a very basic question. I am trying to add a view to a
> component. I have a list of users and the data is being shown but it is
> in raw format.
>
> using
> |
> <?php echo $this item->pto;?>
> |
>
> I get the data
>
> |
> {"date_of_hire":["06-05-2012
> "],"prior_year_carry_over":["14"],"current_year_accrual":["15"],"pto_days_used":["6"],"pto_days_remaining":["23"]}
> |
>
> But I am having an issue displaying each item individually
>
> I keep receiving a message about trying to get property of a non object
> when I try to read each item.
>
>
> |
> <?php echo $this item->pto->['date_of_hire'];?>
> |
>
> I know its probably a simple syntax put I am stumped, I appreciate the help.
>
> Steve
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send

Viper

unread,
Aug 10, 2016, 2:11:10 PM8/10/16
to Joomla! General Development
According to you question... After decoding json string using json_decode() you can access:
objects like $json->date_of_hire
arrays like $json['date_of_hire']

Do and see
$json = '{"date_of_hire":["06-05-2012 "],"prior_year_carry_over":["14"],"current_year_accrual":["15"],"pto_days_used":["6"],"pto_days_remaining":["23"]}';
echo
'<pre>';
print_r
(json_decode($json));
echo
'</pre>';

For accessing a date value you need just do $json->date_of_hire[0]

More info about json format you can fine here json.org

ste...@gmail.com

unread,
Aug 10, 2016, 2:43:18 PM8/10/16
to Joomla! General Development
Thank you for the link Viper.
As Hannes and Michael alluded too I added the $item = json_decode($this->item->pto);

But it gave the error
undefined property  Viewpto::$item


If I try to dump the json it is empty.

Viper

unread,
Aug 10, 2016, 3:31:37 PM8/10/16
to Joomla! General Development
It's because $this object does not contain an item object. Add in the view class right after the
class Viewpto .... {
line
protected $item;

and if you want to access $this->item in the template you should assign this in the view method right before the parent::display(); like
$this->item = &$json;

I think that you need refer to the docs at https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part
See the part where the view described and how the msg($this->msg) assigned to the view/template.

Hannes Papenberg

unread,
Aug 10, 2016, 4:00:23 PM8/10/16
to joomla-de...@googlegroups.com
Based on this line
<?php echo $this item->pto;?>
I thought it should be $this->item->pto, but apparently that is not the
case. Please look up how the code should actually look like. Just adding
random class members and overwriting variables will not help.


Am 10.08.2016 um 21:31 schrieb Viper:
> It's because $this object does not contain an item object. Add in the
> view class right after the
> |
> classViewpto....{
> |
> line
> |
> protected$item;
> |
>
> and if you want to access $this->item in the template you should assign
> this in the view method right before the parent::display(); like
> |
> $this->item =&$json;
> |
>
> I think that you need refer to the docs at
> https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part
> See the part where the view described and how the msg($this->msg)
> assigned to the view/template.
>
> On Wednesday, August 10, 2016 at 9:43:18 PM UTC+3, ste...@gmail.com wrote:
>
> Thank you for the link Viper.
> As Hannes and Michael alluded too I added the $item =
> json_decode($this->item->pto);
>
> But it gave the error
> |
> undefinedproperty Viewpto::$item
> |
>
>
> If I try to dump the json it is empty.
>
>
> On Wednesday, August 10, 2016 at 2:11:10 PM UTC-4, Viper wrote:
>
> According to you question... After decoding json string using
> json_decode() you can access:
> objects like $json->date_of_hire
> arrays like $json['date_of_hire']
>
> Do and see
> |
> $json ='{"date_of_hire":["06-05-2012
> "],"prior_year_carry_over":["14"],"current_year_accrual":["15"],"pto_days_used":["6"],"pto_days_remaining":["23"]}';
> echo '<pre>';
> print_r(json_decode($json));
> echo '</pre>';
> |
>
> For accessing a date value you need just do $json->date_of_hire[0]
>
> More info about json format you can fine here json.org
> <http://json.org>
>
> On Wednesday, August 10, 2016 at 6:43:45 PM UTC+3,
> ste...@gmail.com wrote:
>
> Michael, Hannes thank you for the nudge in the right direction.
>
> I have done this and now get an error undefined property
> Viewpto::$item
> |
> $item =json_decode($this->item->pto);
> joomla-dev-gene...@googlegroups.com
> > <mailto:joomla-dev-gene...@googlegroups.com>.
> > To post to this group, send email to
> joomla-de...@googlegroups.com
> > <mailto:joomla-de...@googlegroups.com>.
> > Visit this group at
> https://groups.google.com/group/joomla-dev-general
> <https://groups.google.com/group/joomla-dev-general>.
> > For more options, visit
> https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.

ste...@gmail.com

unread,
Aug 10, 2016, 6:55:55 PM8/10/16
to Joomla! General Development
Thanks Hannes
That code snippet does actually post the raw data but for some reason when I added the json_decode it gave that error.
thanks again for the help
Steve
>                 joomla-dev-general+unsub...@googlegroups.com
>                 > <mailto:joomla-dev-general+unsub...@googlegroups.com>.
>                 > To post to this group, send email to
>                 joomla-de...@googlegroups.com
>                 > <mailto:joomla-de...@googlegroups.com>.
>                 > Visit this group at
>                 https://groups.google.com/group/joomla-dev-general
>                 <https://groups.google.com/group/joomla-dev-general>.
>                 > For more options, visit
>                 https://groups.google.com/d/optout
>                 <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send

ste...@gmail.com

unread,
Aug 10, 2016, 6:57:26 PM8/10/16
to Joomla! General Development
Thank you viper I will give it a try and see if I can work thru the adding a view docs.
Steve

ste...@gmail.com

unread,
Aug 12, 2016, 10:59:29 PM8/12/16
to Joomla! General Development
Got it guys. I had to do a json_encode. of the data then it worked.
Thanks for pointing me in the right direction.

Steve
Reply all
Reply to author
Forward
0 new messages