SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Drupal] PHP array is printing array instead of value

    Hi all

    I have a small snippet of PHP below which for some reason is printing array instead of the value.
    Can anybody see what I'm doing wrong?

    The first snippet prints ok, its the second one(field_event_location) thats printing array.

    PHP Code:
    $build['node_'.$node->nid]['title'] = array(
          
    '#type'=> 'markup',
          
    '#markup'=> l($node->title,drupal_get_path_alias('node/'.$node->nid)),
          
    '#weight'=> 0,
        );

    $build['node_'.$node->nid]['field_event_location'] = array(
          
    '#type'=> 'markup',
          
    '#markup'=> $node->field_event_location,
          
    '#prefix'=> '<p>',
          
    '#suffix'=> '</p>',
          
    '#weight'=> 1,
        ); 
    Thanks, cb
    The more you learn.... the more you learn there is more to learn.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi What do you use to echo the value?

    when u use [] after a variable name it will automatically create an array.

    Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, and good question.
    This is taken from another post I've been working on for Drupal CMS.
    Tell you the truth I'm not to sure myself

    The code above is wrapped inside a function and then added to a block which displays the results.
    PHP Code:
    $block['content'] = eventlist_block_eventlist(); 
    PHP Code:
    function eventlist_block_eventlist() {

    .....

    $build['node_'.$node->nid]['field_event_location'] = array(
          
    '#type'=> 'markup',
          
    '#markup'=> $node->field_event_location,
          
    '#prefix'=> '<p>',
          
    '#suffix'=> '</p>',
          
    '#weight'=> 1,
        );

    You can see the full script here if that helps http://www.sitepoint.com/forums/show...ery-and-Module

    Cheers peanuts
    The more you learn.... the more you learn there is more to learn.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    If it's printing "Array", you're not going deep enough into the multi-dimensional array

    For example, the value of
    $build['node_123']['field_event_location']['#type']
    is 3 deep.

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mittineague

    If I change from
    PHP Code:
    '#markup'=> $node->field_event_location
    to
    PHP Code:
    '#markup'=> $node->nid,
    //or
    '#markup'=> $node->title
    It works. Array is then removed and replaced with the id number/title of that node.
    Though I still have the below code above it.
    PHP Code:
    $build['node_'.$node->nid]['field_event_location'] = array( 
    It must be to do with the field name.
    I'll have to investigate further unless you can see the problem.

    Barry
    The more you learn.... the more you learn there is more to learn.

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This also works

    PHP Code:
    $build['node_'.$node->nid][] = array(
          
    '#type'=> 'markup',
          
    '#markup'=> $node->title,
          
    '#prefix'=> '<p>',
          
    '#suffix'=> '</p>',
          
    '#weight'=> 1,
        ); 
    The more you learn.... the more you learn there is more to learn.

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anybody help with this?

    I'm also running the Devel module which funny enough shows the correct value of field_event_location.

    Just can't figure out what I'm doing wrong or how to dig deeper into this as Mittineague mentioned?

    PHP Code:
    $build['node_'.$node->nid]['field_event_location'] = array(
          
    '#type'=> 'markup',
          
    '#markup'=> $node->field_event_location,
          
    '#prefix'=> '<p>',
          
    '#suffix'=> '</p>',
          
    '#weight'=> 1,
        ); 
    Cheers, cb
    The more you learn.... the more you learn there is more to learn.

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    You're not getting syntax errors from the extra comma at the end of the array? i.e.

    $array = ( '1' => '1', '2' => '2', );
    should give some kind of "expected" message no?

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mittineague

    I was just updating the post as you posted, if this helps which was taken from the devel Module output.

    Code:
    node_487
    	field_event_location
    		#type
    		#markup
    			und(Array, 1 element)
    				0(Array, 3 elements)
    					value(String, 9 characters) Location1
    					format(NULL)
    					safe_value(String, 9 characters) Location1
    …
    node_480
    	field_event_location
    		#type
    		#markup
    			und(Array, 1 element)
    				0(Array, 3 elements)
    					value(String, 9 characters) Location2
    					format(NULL)
    					safe_value(String, 9 characters) Location2
    Cheers, cb

    update:
    No errors, it just prints Array still?

    If I remove the comma it says:
    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in...
    The more you learn.... the more you learn there is more to learn.

  10. #10
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this helps?

    The exact path to the value inside field_event_location as above:
    $...['elements']['node_480']['field_event_location']['#markup'][0]['value']
    The more you learn.... the more you learn there is more to learn.

  11. #11
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    That's because it is yet another array. "und" looks a bit odd, "and"?
    And if that doesn't make it deep enough it's an array containing a single array (Cheesh!)

    So you need something like
    PHP Code:
    $build['node_'.$node->nid]['field_event_location'] = array(
          
    '#type'=> 'markup',
          
    '#markup'=> $node->field_event_location['#markup'][0]['safe_value'],
          
    '#prefix'=> '<p>',
          
    '#suffix'=> '</p>',
          
    '#weight'=> 1,
        ); 
    (or just value depending)

  12. #12
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "und" looks a bit odd
    I know, still trying to figure out where this fits in, something Drupal adds.

    ERROR
    Notice: Undefined index: #markup in eventlist_block_eventlist() (line 82 of...

    I've tried both which give the same error
    Code:
    '#markup'=> $node->field_event_location['#markup'][0]['value'],
    '#markup'=> $node->field_event_location['#markup'][0]['safe_value'],
    Devel is now showing:
    Code:
    #markup (NULL)
    $...['elements']['node_487']['field_event_location']['#markup']
    ?
    The more you learn.... the more you learn there is more to learn.

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    I just searched for "devel Module und" and guess what, another level!
    Try
    PHP Code:
    '#markup'=> $node->field_event_location['#markup']['und'][0]['safe_value'], 
    and
    PHP Code:
    '#markup'=> $node->field_event_location['#markup']->und[0]['safe_value'], 

  14. #14
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just searched for "devel Module und" and guess what, another level!
    that made me laugh

    I've been at this for well over a week

    Nothing has changed. Everything still NULL as before.
    I did try the second option but gives an error referring to non-object though I think the first way is the prefered way after much reading.

    I'm now wondering if the first line has anything to do with it?
    As I said in an early post, I can remove the reference to ['field_event_location'] altogether and it was still working.
    PHP Code:
    $build['node_'.$node->nid]['field_event_location'] = array( 
    Start a fresh tomorrow unless your still online, and thanks for your time Mittineague.

    Barry
    The more you learn.... the more you learn there is more to learn.

  15. #15
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just searched for "devel Module und"
    It got me thinking and I done a search myself earlier

    PHP Code:
    '#markup'=> $node->field_event_location['und'][0]['value'], 
    We just needed to remove ['#markup']
    I can't believe how long I've been working on this, though got there in the end Mittineague, feels great

    But already, a few other problems have become exposed. If you get chance, might start another thread if needed.

    ---------------

    Ok, I've cloned the location snippet above and will be using the date of the event to do further querying.
    As you can see below, the date condition is set between two dates.

    How do I replace the first date 2014-03-12 with NOW() so I don't have to keep editing the file?
    PHP Code:
    ->fieldCondition('field_event_date_and_time''value', array('2014-03-12''2014-03-31'), 'BETWEEN'
    I thought something like this though gives errors.
    PHP Code:
    ->fieldCondition('field_event_date_and_time''value', array(NOW(), '2014-03-31'), 'BETWEEN'
    Thanks again, Barry
    The more you learn.... the more you learn there is more to learn.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •