I am trying to filter a view of Biblio nodes via Author ID. On the staff profiles, I have a field (field_author_id_ccsi) which contains the ID (an integer). How can I reference that via PHP in the contextual filter for views (under Advanced). Here's what I have so far but it's not working:
$node = node_load(arg(1));
if($node->field_author_id_ccsi[und][0]->value)
return $node->field_author_id_ccsi[und][0]->value;
}
else {
return;
}
You will need to open up you array object $node->field_author_id_ccsi to see where you ID is located and then drill down to it.
For example:
Let $node->field_author_id_ccsi['und'] be your array of interest.
You will need to investigate the contents of said array to see what is actually in that array. If you ID is the first element in the array then:
echo $node->field_author_id_ccsi['und'][0];
will show the integer on the screen in your view.
However a var_dump($node->field_author_id_ccsi['und']); will show you exactly where you ID is located - if it is there, as will a print_r();. You may need to use a foreach() to traverse the array if it is a multi-dimentional array.
Here's the code I used which works:
$node = node_load(arg(1));
if($node && isset($node->field_author_id_ccsi)) {
return $node->field_author_id_ccsi['und'][0]['value'];
}
return;
Related
I'm having a problem with an array in PHP. The thing is that want to display some users info in a table on my page, these users have several properties. I just want to display the username followed by 4 properties, this properties in my database are boolean type (true/false). In order to represent these values on the list I'm using checkboxes, so each checkbox displays checked or not checked according to the value on database. I've already got those values through a query. I'm just having issues on the displaying on my view (page).
In order to check if the property is true or false I'm using two foreach loop. I use the first one to pass for each user and the second one to evaluate the 4 properties of each user. I also preset an array that is responsible for storing if that property is true or false. So if the property is true the index "selected" on my array is going to be true.
The array structure is:
$choiceProperty['text'] // This already contains the four possible properties.
$choiceProperty['selected'] // This is going to store true if the property is true.
$choiceProperty['user_id'] // This is going to store the id of the user.
This is my code:
foreach ($user as $key => $userIndiv) {
foreach ($properties as &$choiceProperty) {
unset($choiceProperty['selected']);
if ($choiceProperty['text'] == 'A' && $userIndiv->CategoryA) {
$choiceProperty['selected'] = true;
}
if ($choiceProperty['text'] == 'B' && $userIndiv->CategoryB) {
$choiceProperty['selected'] = true;
}
if ($choiceProperty['text'] == 'C' && $userIndiv->CategoryC) {
$choiceProperty['selected'] = true;
}
if ($choiceProperty['text'] == 'D' && $userIndiv->CategoryD) {
$choiceProperty['selected'] = true;
}
$choiceProperty['user_id'] = $userIndiv->id;
}
array_push($finalChoices, $properties);
}
As you can see each $userIndiv->CategoryA, CategoryB, CategoryC... already contains the value obtained from the database.
At the end of each user I add the current $properties array to another array $finalChoices in order to check again for the next user and so on.
The problems comes when I printed the $finalChoices array to see its content and I see that it contains an array for each user with its properties but the last index of each array of each user contains the exact same value for all the arrays.
See what I mean:
First position of array:
Second position of array:
Third and last position of array:
As you can see all elements of arrays in their last position have the same values of the last position array. So they're not showing their real value. For example, based on my database the first and second arrays should have "selected" on its last position since they have 'true' in CategoryD.
I'm quite new in PHP and I'm not sure what I'm doing wrong. Of course it could be something missing on my logic that could be making me have this error. If you could help me I'd be grateful!
I also want to apologize for my English, I'm not native speaker and it's still improving. Thank you.
To finish I'd like to add a picture of how that looks on my view:
View
I'm developping a website, where if a user changes some data, it should be stored on the background, to see who did last change and what etc... . I have 1 object called Event, but the data onscreen is devided into 2 tabs (Client and Event). After the submit, I get all the fields and put the data in the object. I have this self made function to compare the values in the new boject with the values of the old object:
function createArrayReturnDiff($obj1, $obj2) {
$helpArray1 = (array) $obj1; //convert object to array
$helpArray2 = (array) $obj2; //convert object to array
$help = array_diff_assoc($helpArray2, $helpArray1); //Computes the difference of arrays with additional index check
return $help;
}
Now this works all fine, I get an array returned with names of the field and the new value.
But here comes the tricky part. After the return of this array, I loop trough it I want to check which tab the value was on in order to give beter user feedback later. So if the value is on Cleint or Event tab. Now I made 2 arrays where I describe all the fields in each tab.
$tabKlant = array('Evenementfirmanaam', 'Evenementaanspreking', 'Evenementcontactpersoon', 'Evenementcontactpersoonstraat', 'Evenementcontactpersoongemeente', 'Evenementcontactpersoonland', 'Evenementcontactpersoonmail', 'Evenementcontactpersoontel', 'Evenementgeldigheidsdatum', 'Evenementfacturatiegegevens', 'Evenementfactuur_mededeling', 'Evenementbestelbon', 'Evenementreferentie');
$tabEvenement = array('Evenementstartdatum', 'Evenementeinddatum', 'Evenementnaam', 'Evenementfeestlocatie', 'Evenementcontactfeestlocatie', 'Evenementaantal', 'Evenementact_speeches_opm', 'Evenementdj', 'Evenementinleiding');
Now my code to check:
foreach ($help as $key => $value) {
if (in_array($key, $tabEvent)) {
$tab = "Event";
} else if (in_array($key, $tabClient)) {
$tab = "Client";
} else {
$tab = "";
}
}
Now what I tried to change was Evenementfirmanaam, so the $help array contains values with key = Evenementfirmanaam and value = 'xxxx'. Everything looks like it is supposed to work. But for some reason, it can't find the value in the in_array of my foreach.
After I tried to write away data to the database. I used a mysqli_real_escape_string on the $key of my help array (firmanaam in this case) and I found out it is creating the string like: '\0Evenement\0firmanaam' . I have no idea why the \0 are added, but I have a feeling this is the reason why the in_array function won't compare my values properly. Does anyone have an idea what the problem might be?
The problem is that the firmanaam property of your Evenement class (which $obj1 and $obj2 look like to be instances of) is private, which results in the cast to array creating special keys:
If an object is converted to an array, the result is an array whose
elements are the object's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are
unaccessible; private variables have the class name prepended to the
variable name; protected variables have a '*' prepended to the
variable name. These prepended values have null bytes on either side.
This can result in some unexpected behaviour.
In essence, you are being punished for violating the logical design of your class: if $firmanaam is private the outside world should not have any access to its value. The cast to array does allow you to get the value but you really should not do this.
Since you are using Evenement to encapsulate and hide data members, do it all the way. If you want access to those members, provide for and use a getter. If you want to compare two instances with specific semantics, add a comparison method to the class.
We need to extract specific fields, no matter if they have data or not.
If I f.ex. want an putput with only the field with external id : 'logo' i am trying this:
$limit = 10;
$app_id = xxxxxxx;
$items = PodioItem::filter($app_id,array('Limit' => $limit,'external_id' => 'logo'));
Within podio not all the fields are filled in, and the result is that we do not get a return value for field 'logo'.
When we don't get a return from field logo - the array output is not structured in a way so we can grab the data we need.
How do we achieve this ?
Added text
Rough Example on print output:
items[0] = Companyname,Logo,Address,Phone,Country
(has data in Logo)
items[1] = Companyname,Address,Phone,Country,ContactPerson
(no data in Logo)
items[2] = Companyname,Address,Phone,Country
PROBLEMS ARISING
items[2][4] is not existing (but i won't know this)
items[0][2] is Address Field (but i won't know this)
items[1][2] is Phone Field (but i won't know this)
Looking for Company Contact Person [x][4] accross items will end in
[0] = Wrong (country)
[1] = Right data (ContactPerson)
[2] = PHP error
Like in SQL, i would take the coloumn_name_1,coloumn_name_2, etc.. and grab data from only these fields.
PodioItem objects have a collection of item fields. As you've noted only fields that have values are included (since including empty fields is redundant). As you've noted you can't reliably access the fields by their array offset.
What you should do it access it by field_id or external_id. These are the unique identifiers. You can use the field method on the PodioItem object for this purpose:
$items = PodioItem::filter(...);
foreach ($items['items'] as $item) {
// Get field with external_id "logo". You can also pass in field_id
$field = $item->field('logo');
if ($field) {
print "Found a logo field!";
}
}
In PHP I have a multidimensional object created from looping through a list of ids
$summary = array();
foreach ( $request->id as $id ) {
...
$summary[] = $summary_data;
}
it's then passed to my javascript.
return json_encode(array('summary' => $summary));
Not sure how to navigate the returned object correctly. Do I have to use the original list of id's, and use that as an index against this object? Or is there a better way to keep track of this?
End result, I want a select box such that when a new item is selected, its data is displayed.
A general JSON object would look like this (trying to put all possible cases):
{
"key1":"value1",
"subObject":{
"subKey1":"subValue1",
"subKey2":"subValue2"
},
"arrayOfSubObjects":[
{"subKey3":"subValue3"},
{"subKey4":"subValue4"}
]
}
You can reference any element of a JSON object with jsonObject.key, but remember those part between [] are arrays so you'll need to index them as if they were in an array so:
// to point subKey1:
jsonObject.subObject.subKey1;
// to point subKey3
jsonObject.arrayOfSubObjects[0].subKey3;
OR
// to point subKey1:
jsonObject["subObject"]["subKey1"];
// to point subKey3
jsonObject["arrayOfSubObjects"][0]["subKey3"];
note the 0 has no quotes because it's an index.
This is using WordPress lingo - but is more of a core PHP based question. I have an option in my site backend that I am storing a specific Location (a WordPress custom post type) as the "headquarters". I am storing the value as such:
function options_headquarters() {
foreach(get_field('option_headquarters','options') as $post_object) {
$options_headquarters = $post_object->ID;
return $options_headquarters;
}
}
What I am fuzzy on is - since my option is only allowed to grab one value (option is configured to just be a single dropdown) -- is there an alternative to using a foreach statement (to grab just a specific array value) and still applying it to the post_object?
Thanks!
function options_headquarters() {
$options = get_field('option_headquarters','options');
$options_hq = isset($options[0]) ? $options[0]->ID : NULL;
return $options_hq;
}
Just get the array and access the first element:
$post_object = array_pop(get_field('option_headquarters','options'));
Reference: array_pop