Accessing field value in Drupal 7 on load_node in template - php

In a template for a content type I am loading a node from a node reference.
It loads and if I do a print_r I get this:
stdClass Object (
[vid] => 40
[uid] => 14
[title] => Cover
[log] =>
[status] => 1
[comment] => 0
[promote] => 1
[sticky] => 0
[nid] => 40
[type] => portfolio_image_main
[language] => en
[created] => 1309382711
[changed] => 1309382711
[tnid] => 0
[translate] => 0
[revision_timestamp] => 1309382711
[revision_uid] => 14
[field_portolio_image] => Array (
[en] => Array (
[0] => Array (
[fid] => 5626
[alt] =>
[title] =>
[uid] => 14
[filename] => Cover.jpg
[uri] => public://Cover.jpg
[filemime] => image/jpeg
[filesize] => 147898
[status] => 1
[timestamp] => 1309382711
)
)
)
[name] => jojo
[picture] => 0
[data] => a:1:{s:7:"contact";i:1;}
)
and Im trying to access the single variable here:
$newImagePath1 = $newImage1->field_portfolio_image['en '][0]['filename'];
but so far nothing. Any thoughts?

please try to use below code
$keys = array_keys($arr[field_portolio_image][en]);
$arr[field_portolio_image][en][$keys][filename];

There is a helper function to access field items for the user's correct language (otherwise, you'd have to hardcode the ['en'] part).
field_get_items()
So your code would end up being something like this:
$field_instances = field_get_info('node', $newImage1, 'field_portfolio_image');
// $field_instances should now be an array.
foreach ($field_instances as $field_instance) {
print $field_instance['filepath'];
}

Related

PHP check array object key value without using loop/foreach

Just to learn more techniques in PHP , I'm so used in loop foreach and I want to find some other ways on how to check an array object. Let's say if we have a result something like this:
Array
(
[0] =; stdClass Object
(
[gallery_id] => 38
[artist_id] => 58
[title] => Appearances
[description] => Appearances
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 03:27:23
[update_timestamp] => 2014-08-07 15:26:24
[url] => talents/58/photos/38/Appearances
[position] =>
)
[1] => stdClass Object
(
[gallery_id] => 36
[artist_id] => 58
[title] => Endorsements
[description] => Endorsements
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 03:17:28
[update_timestamp] => 2015-01-13 15:50:09
[url] => talents/58/photos/36/Endorsements
[position] => 1
)
[2] => stdClass Object
(
[gallery_id] => 34
[artist_id] => 58
[title] => Magazine Covers
[description] => Magazine Covers
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 02:54:27
[update_timestamp] => 2015-01-13 15:50:10
[url] => talents/58/photos/34/Magazine-Covers
[position] => 2
)
)
I know we can do something like this to check a value in the object:
$myFlag = 2;
foreach($objArr as $obj){
if($obj->position == $myFlag){
return true;
}
}
But is there a way to check it without using a loop? A more efficient way, coz I feel whenever I use this(most of the time) , It's not that efficient and somehow takes time to process.
How about using array_filter with closure.
$myFlag = 2;
$target = array_filter($objArr, function($elem) use($myFlag){
return $elem->position === $myFlag;
});
print_r($target);
result is ...
Array
(
[2] => stdClass Object
(
[gallery_id] => 34
[artist_id] => 58
[title] => Magazine Covers
[description] => Magazine Covers
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 02:54:27
[update_timestamp] => 2015-01-13 15:50:10
[url] => talents/58/photos/34/magazine-covers
[position] => 2
)
)

Unsetting an array on a variable property

So I have an object $entity, and a property $property.
$property has several arrays inside of it.
I want to be able to unset these arrays, as such:
unset($entity->$property['array']);
However, I'm getting the error:
Error: Cannot use string offset as an array
I tried setting $entity->$property to a single variable, but $entity was passed by reference into the function, and changing a new variable isn't going to modify the old object.
How do I ensure the old object gets modified and these arrays get unset?
Here is part of the entity, up until the property:
stdClass Object
(
[vid] => 33128
[uid] => 3
[title] => Title
[log] =>
[status] => 0
[comment] => 0
[promote] => 1
[sticky] => 0
[vuuid] => 3f922255-462b-459a-a8d6-9a2ac30899cf
[hash] => fa14fb00f1355761429977fe916e8f66
[nid] => 29652
[type] => resource
[language] => en-US
[created] => 1137158100
[changed] => 1371649725
[tnid] => 29652
[translate] => 0
[uuid] => 051eba0c-4b52-4269-b0d2-4d0ced9d6a6e
[revision_timestamp] => 1371649725
[revision_uid] => 0
[body] => Array
(
)
[field_file] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26750
[uid] => 1
[filename] => filename.pdf
[uri] => public://cms/cms/filepath/filename.pdf
[filemime] => application/pdf
[filesize] => 666960
[status] => 1
[timestamp] => 1370660770
[type] => default
[uuid] => ea7c2c1f-4ef2-44fb-b89d-51c0fa090793
[hash] => aafc3f362cda639dc0475fccbeeb9af2
[rdf_mapping] => Array
(
)
[display] => 1
[description] => filename.pdf
)
)
)
$property is field_file, and looks like this:
Array ( [und] => Array ( [0] => Array ( [fid] => 11285 [uid] => 1 [filename] => filename.pdf [uri] => public://cms/cms/resource_library/datasheets/filepath/filename.pdf [filemime] => application/pdf [filesize] => 666960 [status] => 1 [timestamp] => 1368086259 [type] => default [uuid] => ea7c2c1f-4ef2-44fb-b89d-51c0fa090793 [hash] => cdb2fc5203a9e7f9c066a89bf9f70502 [rdf_mapping] => Array ( ) [display] => 1 [description] => filename.pdf ) ) )
It should be written like this instead:
unset($entity->{$property}['some_array_name']);
Without the curly brackets, the array access operator is bound more tightly. Hence your original expression will actually be processed as...
unset($entity->($property['some_array_name']));
As $property itself is a string, it's obviously wrong to treat as an array - that's what PHP warns you about.

getting title field from the process array in the webform

I am using the webform module for Drupal.
I needed to customize one of the webform nodes.
So I tried printing [title] => title of this webform goes here!!! using <?php print drupal_render($form['process']['title']); ?>
But it returns nothing, what am I doing wrong here?
Thanks!
FYI, the form can be rendered with; drupal_render($form['submitted']);
Array ( [#attached] => Array ( [css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ) ) [#process] => Array ( [0] => webform_client_form_includes ) [#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...
You should try this,
echo $form[#node]->title;
var_dump($form[#node]);// to test it your node data
As from your array
Array (
[#attached] => Array ([css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ))
[#process] => Array ( [0] => webform_client_form_includes )
[#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...

PHP: Displaying single array data?

So the code is
$gallery = get_gallery('gallery_id');
print_r ($gallery);
And I get:
Array ( [0] => Array ( [id] => 13 [image] => 0 [user] => 13 [timestamp] => 1366237591 [ext] => png [caption] => Happy smile shi ma? [comment] => ) [1] => Array ( [id] => 14 [image] => 0 [user] => 13 [timestamp] => 1366237954 [ext] => jpg [caption] => Confused [comment] => ) [2] => Array ( [id] => 15 [image] => 0 [user] => 13 [timestamp] => 1366237979 [ext] => jpg [caption] => Facebookerg [comment] => ) [3] => Array ( [id] => 16 [image] => 0 [user] => 13 [timestamp] => 1366377510 [ext] => gif [caption] => lolwut? [comment] => ) [4] => Array ( [id] => 17 [image] => 0 [user] => 13 [timestamp] => 1366380899 [ext] => jpg [caption] => rorwut? [comment] => ) [5] => Array ( [id] => 18 [image] => 0 [user] => 13 [timestamp] => 1366651685 [ext] => jpg [caption] => Notes? [comment] => ) [6] => Array ( [id] => 19 [image] => 0 [user] => 13 [timestamp] => 1366711880 [ext] => jpg [caption] => asd [comment] => ) [7] => Array ( [id] => 20 [image] => 0 [user] => 14 [timestamp] => 1366940983 [ext] => jpg [caption] => Belzelga [comment] => ) )
Which is good, it finally worked. But how do you display a single data/table.
Because I am trying to get a single 'id' from these thingies.
I tried echoing $gallery['id']
but I got an error. :/
You need to access the correct index first:
$gallery[0]['id']
// ^^^
Your $gallery variable is now a multi dimentional array.
You have
$gallery[0]['id'];
$gallery[1]['id'];
.....
Now you can either use a foreach to process through the array or a for loop
foreach ($gallery as $anItem ) {
echo $anItem['id'];
}
OR
for ( $x=0; $x < count($gallery); $x++ ) {
echo $gallery[$x]['id'];
}
Try this looping code-segment:
foreach( $gallery as $temp ) {
echo $temp['id'];
}
Your $gallery is a multidimensional array. You need to iterate through it.
If you want to only display a particular gallery, you should probably use something like www.yoursite.com/gallery.php?id=1. then in your code to display it
if(isset($_GET['id']))
{
foreach( $gallery as $g )
{
if($g['id'] == $_GET['id'])
echo $g['id'];
}
}
You can get the data so. Without being bound to a key.
current($gallery)['id']

how do i access these array keys as a variable in CI?

Array
(
[abc] => Array
(
[0] => Array
(
[id] => 1
[title] => hello 12
[meta_keyword] =>
[meta_description] =>
[tags] => sdfgdfg
[status] => draft
[body] => dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 5
[subcatid] => 7
[featured] =>
[pubdate] => 2011-06-17 03:39:55
[user_id] => 0
)
[1] => Array
(
[id] => 2
[title] => hello xyz
[meta_keyword] =>
[meta_description] =>
[tags] => xcfasdfcasd
[status] => draft
[body] => dfdsafsdf dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 1
[subcatid] => 2
[featured] =>
[pubdate] => 2011-06-17 03:43:12
[user_id] => 0
)
for example if i want to echo out title I would do echo $abc['title'] but it's not working pls help,
the above output is a result of print_r($count['abc]);
it shows nothing when i do print_r($count['abc']['title'])
You would need to use the numeric key as well: $abc[0]['title'].
In other words, you've got an array with array members of an array type which use numeric keys, in which each of those members are arrays which use associative keys to access values. So you need to access each array in $abc to get to the array which contains your title values.
EDIT
If you're trying to loop through these values, you would need to loop through each array. Such as:
$c_abc = count($abc);
for ($i = 0; $i < $c_abc; $i++) {
echo "{$abc[$i]['title']}<br/>";
}
Read about php associative arrays....you will have you goal achieved
try this:
foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}
To access you array variables, the right way is like this
$count['abc'][0]['title']
However, in your title, you are asking about Array keys as variables?
Actually this does not need to be related with CI.
A simple example
$array = array ( "hi" => "bye");
extract( $array);
//it will make "hi" a variable :: $hi = "bye"
echo $hi; // will output bye
Heres structured solution
$data = Array(
[abc] => Array
(
[0] => Array
(
[id] => 1
[title] => hello 12
[meta_keyword] =>
[meta_description] =>
[tags] => sdfgdfg
[status] => draft
[body] => dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 5
[subcatid] => 7
[featured] =>
[pubdate] => 2011-06-17 03:39:55
[user_id] => 0
)
[1] => Array
(
[id] => 2
[title] => hello xyz
[meta_keyword] =>
[meta_description] =>
[tags] => xcfasdfcasd
[status] => draft
[body] => dfdsafsdf dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 1
[subcatid] => 2
[featured] =>
[pubdate] => 2011-06-17 03:43:12
[user_id] => 0
)
)
);
extract($data);
foreach($abc as $value){
echo $value['title']."<br>";
}

Categories