I need help with figuring out how to call some depth elements in API call. We're connecting API which company gave us and this is the code I have
<?php
include("O3ON_API.php");
header('Content-type: text/html; charset=utf-8');
$apiclient = O3ON_API_Connector::getInstance();
$models = $apiclient->CallAPI('Model', 'sr-Latin-CS', '');
$categories = $apiclient->CallAPI('Category', 'sr-Latin-CS', '');
?>
So when I want to call it, I do it this way
<?php
foreach($models as $model){
echo '<div>'.$model->Name.'</div>';
}
?>
This all works well, but I am struggling on how to call field which has some depth. This is an example of their API
Model
Name
Image
Category
Price
So with the $model I can call first level which is Name and Image. But how can I call sub category Price or Category? I found some examples online, but since I got the documentation from company it's different then what I want to do.
I tried this but with no success
$model->Category->Name
$model->Category[0]
I am thankful to anyone who helps!
If I understood. The Category is nested inside Image This should work:
$model->Image->Category
$model->Name
Check the structure with var_dump()
Related
I am beginner in SocialEngine and I want to know if there is any way to get the photo of the object in the widget.
Like i know we can get the photo of the object in view using following code.
//in controller
$table = Engine_Api::_()->getDbTable("mytable","module");
$select = $table->select()->where->("table_id", 1);
$fetch = $table->fetchRow($select);
$this->view->fetch = $fetch;
//in view
echo $this->itemPhoto($this->fetch, "thumb.normal");
above line would product the image if there is any otherwise will load the default image.
But this line of code is not working in widget.
I really appreciate your help.
You need to make sure that first param you're passing to $this->itemPhoto is a User instance:
$this->itemPhoto($user, 'thumb.normal', $user->getTitle())
Seems that in your case $this->fetch is simply a Row. You can create user using this code: Engine_Api::_()->getItem('user', $user_id);. Hope this helps.
I'm new on this community, and I'll be very grateful if someone can help me with this.
Well, I'm trying to get the information from any article, by ID or by [object].
The idea is, "do some actions" for all of these data, extracting the "title", "content", etc. then manipulate this information.
Some code that I think it could be usefull:
$model = JModelLegacy::getInstance('Articles', 'ContentModel');
$articles = $model->getItems();
"$articles" saves an array, right? which contains all my articles indexed from 0->N.
Now when I use it in a foreach for example, like:
foreach($articles as $newArt){
echo $newArt->get("alias");
}
"echo" returns nothing.
Actually I tested [echo $articles[0 or 1 ...]; ] same ... nothing.
Any ideas for what is the problem?
EDIT:
I was wrong calling attributes using get("alias").
$newArt->alias;
Simple :').
How can I get an item code from invoices? I'm working on php and I'm trying this
$Code = $invoice->LineItems->ItemCode;
and
$Code = $invoice->LineItems->LineItem->ItemCode;
But that doesn't work. How can I get it then?
Thanks!
LineItems will be an array of LineItem elements, so you need to access the Array element(s) you want the code from:
Loop over all line items:
foreach($invoice->LineItems as $LineItem){
$code = $LineItem->ItemCode;
/* do something with the $code */
}
Or to access the first Line Item:
$code = $invoice->LineItems[0]->ItemCode;
Note, I do all my Xero API work in Python, so I haven't tested this in PHP, apologies if I've made a syntax error, the approach should be sound.
A paged request was necessary to get the Item Code field.
I'm looking for a solution to display posts based on the viewers
location.
For Example: I'm writing an article and set an option so it is only
visible for users from within USA.
If a user in Germany is visiting the Wordpress page, the post does
not appear.
I don't want to change content within a post, but more hide and show
them with an option for the author.
If there is some plugin, that can do this, I would highly appreciate.
Thanks.
I had similar problem. Didn't found a plugin and wrote this to query post for country of visitor: (remember to get your key at http://ipinfodb.com/)
$ip=$_SERVER['REMOTE_ADDR'];
$request = 'http://api.ipinfodb.com/v3/ip-country/?key= get your own key &ip='.$ip.'&format=json';
$response = file_get_contents($request);
$jsonobj = json_decode($response);
if ($jsonobj->countryCode == "SE"){
// args for query post for selected country $args = array ()
}
elseif ($jsonobj->countryCode == "PL"){
// args for query post for selected country $args = array ()
}
query_posts( $args );
Have a look at this discussion over here: https://wordpress.stackexchange.com/questions/26832/display-only-certain-posts-based-on-visitors-country. It might not give you a ready-to-use solution but maybe it helps as a pointer in the right direction.
I know there are functions to like is_single() that will return data about the page, but I'm looking for a way to get the following information outside of the loop:
The category of the single post.
AND
The title of the single post.
All I would really need is the post ID in question and I could get all the other information. I've looked through the functions reference in the codex, but I haven't found anything. Is this impossible because the script doesn't even get that information 'til the Loop runs?
(I would need this information in both the header and footer, so before and after the PHP script for the loop, if that is a problem.)
Hopefully someone can offer some insight.
EDIT: To clarify: I want the information to be from the post that is loaded in the loop on the "single" page. (AKA the post they are viewing.) So how would I get this ID in the first place? Basically, when viewing a post, I want to get its category or title, but not while the loop is going.
This is actually quite simple.
// Gets an array of post objects.
// If this is the single post page (single.php template), this should be an
// array of length 1.
$posts = get_posts();
// The post object contains most/all of the data that is accessible through the
// normal functions like `the_ID()`, `the_author()`, etc
var_dump($posts[0]->ID);
This can be performed outside of and it doesn't affect the normal loop. For example, my single.php template looks like this (line numbers included):
1. <?php
2. get_header();
3. $posts = get_posts();
4. var_dump($posts[0]->ID);
5. ?>
6. <div id="post">
You could execute your own query and return a $post object and then access it's attributes through something like
echo $post->title;
Read about $wpdb and Custom Queries on the Codex. Basically you could run a SQL query using the ID as a where filter through its $wpdb object.
Another option, more WP-like, would be to use a Custom Query Post, where you could define another loop, returning only your post using its id:
query_posts('p=2010'); // 2010 being the post's ID
Then you could run the alternative loop and use the similar $post object to display some information.
This is what I use inside the loop to run an new query; I just tried it outside the loop and it works, on a single.php template page. This will give the title of the latest post in mycategory. I don't know how to pull the category ID, though.
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while($my_query->have_posts()):
$my_query->the_post();
the_title();
endwhile;
After the query has run, try running:
global $wp_query;
var_dump( $wp_query );
The information you are looking for is probably in $wp_query->query_vars or $wp_query->post
You can use the $wp_query global object, and address all returned posts like this
$wp_query->posts ; // contains an array of posts.
Then, if for instance you need to know if the first post is of a certain post_type, you can do this:
$first_post_type = $wp_query->posts[0]->post_type;
Couldn't you store the $post information in a separate variable on the page, then just echo the data you need out later when you need it (ie, outside the loop)?
This is predicated on needing the data after the initial loop has run.
The usual thing is to create "second loop" which would extract needed post, hence your needed data.