Reputation: 1167
Can anyone can help here, I am using an array to get some data split up and on the view this is attached to I get this error above.
here is my code:
PHP for ajax view:
$images = array();
$data_link = array();
$data_id = array();
$data_likes = array();
$data_text = array();
foreach ($media as $data) {
//dd($data->caption);
$images[] = array(
//dd($data->caption),
"data_url"=>$data->images->standard_resolution->url,
"data_link"=>$data->link,
"data_text" => $data->caption['text'],
"data_id"=>$data->getId(),
"data_likes"=>$data->likes->count,
"data_like"=>$current_user->likes($data),
"data_token"=>$token
);
}
echo json_encode(array(
'next_id' => $media->getNextMaxTagId(),
'images' => $images
));
I then access the data with a load more button. When I debug the code for $data->caption
I get this:
object(stdClass)#62 (4) {
["created_time"]=>
string(10) "1377775401"
["text"]=>
string(48) "New arrival #folkclothing reversible knit/jacket"
["from"]=>
object(stdClass)#63 (4) {
["username"]=>
string(18) "vanmildertclothing"
["profile_picture"]=>
string(76) "http://images.ak.instagram.com/profiles/profile_24567722_75sq_1342623790.jpg"
["id"]=>
string(8) "24567722"
["full_name"]=>
string(11) "Van Mildert"
}
["id"]=>
string(18) "533141169038331174"
}
Does anyone know why I get this error?
Edited below here as the error is not as expected
The ajax jquery call for this is as below:
$('#more').click(function() {
var tag = $(this).data('tag'),
maxid = $(this).data('maxid');
$.ajax({
type: 'GET',
url: '/ajax',
data: {
tag: tag,
max_tag_id: maxid
},
dataType: 'json',
cache: false,
complete: function(){
},
success: function(data) {
// Output data
$.each(data.images, function(i, src) {
var $content = $('<article class="instagram-image"><form id="'+ data.images[i].data_token +'" class="forms status-'+ data.images[i].data_like +'" action="'+base+'" method="post"><a class="fancybox" rel="folk-1" href="' + data.images[i].data_url + '"><img alt="' + data.images[i].data_text + '" src="' + data.images[i].data_url + '" alt="' + data.images[i].data_text + '" /></a><div class="formSubmit-feedback"></div><input type="hidden" name="id" value="'+ data.images[i].data_id +'"><p>'+ data.images[i].data_likes +'</p></form></article>');
var duration = 1000, n = 0.1;
$content.appendTo('section#images');
$item = $content.find('form');
if( $item.hasClass("status-false") ){
$item.addClass("notLiked");
//$('.notLiked').find('button.unlike').hide();
$item.find('a').after('<button class="ajax instabtn button-like like icon-heart" type="submit" name="action" value="Like"></button>');
}
if( $item.hasClass("status-true") ){
$item.addClass("Liked");
//$('.Liked').find('button.like').hide();
$item.find('a').after('<button class="ajax instabtn button-unlike unlike icon-heart" type="submit" name="action" value="Unlike"></button>');
}
});
// Store new maxid
$('#more').data('maxid', data.next_id);
},
error:function(){
$('#error-panel').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
}
});
});
On the page I have a button that loads more instagram images into the container, it works okay with everything else but will not work with the caption->text which I am trying to access. If I go to the url /ajax I get the data I want and it shows the text in the text_data array but when it comes to loading that into the images from the load more I get that error.
This is the exact error I get:
Error rendering view: [instagram.ajax]Trying to get property of non-object
Not as simple as I first thought.
Upvotes: 3
Views: 26268
Reputation: 1167
Right the answer was this:
foreach ($media as $data) {
$title = (isset($data->caption))?mb_substr($data->caption->text,0,70,"utf8"):null;
$images[] = array(
"data_url"=>$data->images->standard_resolution->url,
"data_link"=>$data->link,
"data_text" => htmlspecialchars($title),
"data_id"=>$data->getId(),
"data_likes"=>$data->likes->count,
"data_like"=>$current_user->likes($data),
"data_token"=>$token
);
}
So I declared the variable before the images array and then accessed it in the array.
Upvotes: 0
Reputation: 33573
$data->caption
is an object, and you are accessing it as an array.
Use $data->caption->text
instead.
Upvotes: 3
Reputation: 522626
Exactly as it says: $data->caption
is an object, but you're trying to access it like an array using $data->caption['text']
. Access it like an object instead:
$data->caption->text
Upvotes: 7
Reputation: 48887
$data->caption
is an object but your code is trying to access it as if it were an array.
Change $data->caption['text']
to $data->caption->text
Upvotes: 5