Accessing arrays and or objects - php

*I solved it myself... like 10 minutes later. *
$varbgimg = $row->_field_data['nid']['entity']->field_slideimage['und'][0]['uri'];
Is what I used...
Hope someone can help me. How can I access 'uri' => 'public://veglo8.jpg'?
This is from Drupal and the Views module. If someone could maybe even help me with my ultimate goal, I would appreciate that...
I have a field in Views called slideimage. I want to add a style="background-image:url(image field URL);" to my div. Tried to rewrite the output but it strips the style...
Thanks in advance.
stdClass::__set_state(array(
'nid' => '20',
'node_title' => 'Test 1',
'field_data_field_slideimage_node_entity_type' => 'node',
'field_data_body_node_entity_type' => 'node',
'_field_data' =>
array (
'nid' =>
array (
'entity_type' => 'node',
'entity' =>
stdClass::__set_state(array(
'vid' => '20',
'uid' => '1',
'title' => 'Test 1',
'log' => '',
'status' => '1',
'comment' => '1',
'promote' => '0',
'sticky' => '0',
'nid' => '20',
'type' => 'test',
'language' => 'und',
'created' => '1358336066',
'changed' => '1358337923',
'tnid' => '0',
'translate' => '0',
'revision_timestamp' => '1358337923',
'revision_uid' => '1',
'body' =>
array (
'und' =>
array (
0 =>
array (
'value' => 'Body text here',
'summary' => '',
'format' => 'filtered_html',
'safe_value' => '
Body text here',
'safe_summary' => '',
),
),
),
'field_slideimage' =>
array (
'und' =>
array (
0 =>
array (
'fid' => '8',
'alt' => '',
'title' => '',
'width' => '624',
'height' => '390',
'uid' => '1',
'filename' => 'veglo8.jpg',
'uri' => 'public://veglo8.jpg',
'filemime' => 'image/jpeg',
'filesize' => '27393',
'status' => '1',
'timestamp' => '1358336725',
'rdf_mapping' =>

You can use file_create_url to convert public://... to real world URLs.
$real_url = file_create_url($img_src);

I've used the pathinfo() function for this in the past.
http://php.net/manual/en/function.pathinfo.php
You can apply styles programmatically with this by replacing user-picture[0]['uri'] with your image url. I think you can even place the whole public:// url in there and it will work just fine as well.
THUMBNAIL_STYLE = 'thumbnail';
// now get the full image url from the uri and the style
$default_thumbnail = image_style_url($THUMBNAIL_STYLE, $user->picture[0]['uri']);
http://drupal.org/node/1425836

Related

How to output value of an array

I am a complete beginner in PHP. However I know how to output the value of custom field. I am having a bit of problems with arrays. The post meta key is fw_options. The value has multiple arrays and looks like this:
array (
0 =>
array (
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array (
'time' => 'fixed',
'hourly' =>
array (
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array (
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>
array (
),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array (
0 => '717',
),
),
)
I have this code which I have tried with no success:
$array = get_post_meta( get_the_ID(), 'fw_options', true );
echo $array[0]['reading_type']['fixed']['reading_times'];
How can I output the value 500 from the post meta key reading_times?
Simple Print array according to key
First hold the value into a variable than print according to array key
$data = array(
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array(
'time' => 'fixed',
'hourly' =>
array(
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array(
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>array(),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array(
0 => '717',
),
);
echo $data['reading_type']['fixed']['reading_times'];
Output #500

cakephp: structure database data returns

a possible return of a DB query looks like this:
array(
(int) 0 => array(
'Question' => array(
'id' => '737',
'question' => 'what is 1x7?',
),
'Answer' => array(
(int) 0 => array(
'id' => '2373',
'question_id' => '737',
'correct' => true,
'answer' => 'possible answer1',
'created' => '2014-05-08 13:46:43',
'modified' => '2014-05-08 13:46:43'
),
(int) 1 => array(
'id' => '2374',
'question_id' => '737',
'correct' => false,
'answer' => 'possible answer2',
'created' => '2014-05-08 13:46:43',
'modified' => '2014-05-08 13:46:43'
)
),
'Linkquestioncategory' => array(
(int) 0 => array(
'id' => '608',
'question_id' => '737',
'category_id' => '5',
'created' => '2014-05-08 13:46:47',
'modified' => '2014-05-08 13:46:47',
'Category' => array(
'id' => '5',
'name' => 'Simple Math',
'active' => true,
'linkquestioncategory_count' => '64',
'created' => '2014-02-03 09:20:54',
'modified' => '2014-03-04 14:47:05'
)
)
)
),
In order to clean it up to avoid sending to much unwanted data => my questions is => how can I get rid of those fields like
Answer.created
Linkquestioncategory.created
Linkquestioncategory.Category.created
I know that I can use conditions 'fields' to select the selected fields, but as far as I know, this works only for 'Question', but how can I manipulate those deeper array data?
Can I do this also with the 'fields' condition? If yes, how?
Thanks!!
http://nuts-and-bolts-of-cakephp.com/2008/09/05/example-of-cakephps-containable-for-deep-model-bindings/
This helped me a lot!!
Thanks!!

Custom sorting an array in cakePHP

not sure if this is the best way to do what I need but this is my issue. In my edit function view I recreate a forms fields based on what's in $this->data->expense but I need the array to be in a certain order or the fields get generated in the wrong order. This is the array I have:
'Expense' => array(
(int) 0 => array(
'id' => '98',
'date' => '2012-08-23',
'sitename' => '123',
'detail' => 'Breakfast',
'amount' => '100.00',
'miles' => null,
'total' => '100.00',
'expense_claim_id' => '63',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 09:08:52',
'ExpenseClaim' => array(
'id' => '63',
'user_id' => '3',
'claim_status_id' => '1',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 10:14:10',
'approved' => false,
'approved_by' => '0',
'date_submitted' => '2012-08-23 09:08:52'
),
'ExpenseCode' => array(
(int) 0 => array(
'id' => '1',
'name' => 'Plane fare',
'code' => '1',
'created' => '2012-07-31 09:52:02',
'modified' => '2012-07-31 09:53:57'
)
)
),
This is how I need it to be ordered (ExpenseCode) appears higher up:
'Expense' => array(
(int) 0 => array(
'id' => '98',
'date' => '2012-08-23',
'sitename' => '123',
'detail' => 'Breakfast',
'ExpenseCode' => array(
(int) 0 => array(
'id' => '1',
'name' => 'Plane fare',
'code' => '1',
'created' => '2012-07-31 09:52:02',
'modified' => '2012-07-31 09:53:57'
)
),
'amount' => '100.00',
'miles' => null,
'total' => '100.00',
'expense_claim_id' => '63',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 09:08:52',
'ExpenseClaim' => array(
'id' => '63',
'user_id' => '3',
'claim_status_id' => '1',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 10:14:10',
'approved' => false,
'approved_by' => '0',
'date_submitted' => '2012-08-23 09:08:52'
)
),
How can I achieve this and will changing the structure of it affect cake when I post?
As far as I know, there is no built-in function on CakePHP to custom sort an array. I guess you will have to do this with pure PHP. https://stackoverflow.com/search?q=custom+sort+array+php
Unless you must have the fields created dynamically, I suggest you to add each field yourself.
will changing the structure of it affect cake when I post?
No, if you simply put the fields in a different order this won't affect CakePHP. The data sent via POST will be in array $this->data['Expense']. When saving the data with $this->model->save(), the order does not matter.
This makes no sense. If the order of the data in an associative array is causing your HTML fields/form inputs..whatever to be in the wrong order, you're doing it wrong.
Try using the keys instead of just looping through in order. Or read more about associative arrays - there are a lot of fun / easy things to do to use & manipulate their data... but again to reiterate, there is NO need to return the data in a specific order.
Edit:
You could always create an array of keys and repeat through those. Might be a cleaner way of managing the order.

Add a page browser to front-end extension

Is it possible to add a page browser (browse_links) like this to a front end extension?
Sure. Add the 'suggest' wizzard in the TCA for that field:
'yourlink' => array (
'label' => 'LLL:EXT:yourextension/locallang_general.xml:yourlink',
'config' => array (
'type' => 'group',
'internal_type' => 'db',
'allowed' => 'pages',
'size' => '1',
'maxitems' => '1',
'minitems' => '0',
'show_thumbs' => '1',
'wizards' => array(
'suggest' => array(
'type' => 'suggest',
),
),
),
),

PHP Multi-Dimensional Arrays Issue

Ok I have a mult-dimensional array which has the following structure...
0 =>
array (
'membership' =>
array (
'member' =>
array (
'name' => '',
'landline' => '',
'libcard' => '',
'mobile' => '',
'email' => '',
),
'updated_at' => '',
'member_id' => 12345,
'starts_at' => '',
'id' => 14,
'group_id' => 280,
'optional_field_values' =>
array (
0 =>
array (
'optional_field' =>
array (
'name' => '',
'updated_at' => '',
'id' => 1,
'group_id' => 280,
'description' => '',
'created_at' => '',
),
'updated_at' => '',
'optional_field_id' => 1,
'membership_id' => 14,
'id' => 4,
'value' => '12539267',
'created_at' => '',
),
),
'ends_at' => '',
'joining_fee' => 0,
'created_at' => '',
),
),
Now I can access everything inside Membership and inside Member using code like...
$member[0]['membership']['member']['name']
or
$member[0]['membership']['joining_fee']
But when ever I try to access stuff inside optional_field_values I get nothing returned...
Any ideas why this is not working?
Edit:
Trying to access the field using code like...
$member[0]['membership']['optional_field_values']['value']
$member[0]['membership']['optional_field_values'][0]['value']
^ Should work...
(Edited to match OP's edit)
How about :
$member[0]['membership']['optional_field_values'][0]['value']
You can iterate over all optional field values like this :
foreach ($member[0]['membership']['optional_field_values'] as $field)
echo $field['value'];

Categories