4

It's pretty stright forward, im trying to display the results of my form, Im having trouble finding something that explains how to display the results outside of the drupal_set_message() code, I think I am close with my current code.However I am getting this error Fatal error: Cannot use string offset as an array

/** 
 * Block info 
 */ 

function zillow_block_info(){
    $blocks['zillow_block'] = array(
        'info' => t('Zillow Block'),
    );
    return $blocks;
}

function zillow_block_view($delta = ''){
    $block = array();
        switch ($delta){
    case 'zillow_block';
        $block['subject'] = t('Zillow Block');
        $block['content'] = drupal_get_form('zillow_form');
    break;
    }
    return $block;
}

/**
 * Api info 
*/

function zillow_api_info($form, &$zinfo){
    set_time_limit(3600);
    $state = $zinfo['values']['state'];
    $zillow_id = 'XXXXXXXXXXX';
    $xml = file_get_contents('http://www.zillow.com/webservice/GetRateSummary.htm?zws-id='.$zillow_id.'&state='.$state.'');
    $zinfo = simplexml_load_string($xml);

    foreach($zinfo->response as $rates){
        $zinfo .= $rates->today->rate[0];
        $zinfo .= $rates->today->rate[1];
        $zinfo .= $rates->today->rate[2];
        $zinfo .= $rates->lastWeek->rate[0];
        $zinfo .= $rates->lastWeek->rate[1];
        $zinfo .= $rates->lastWeek->rate[2];
    }

    return $zinfo;
}

/**
 * Form
 */

function zillow_form($form, &$zinfo){

    $form['description'] = array(
        '#type' => 'item',
        '#title' => t('Get your mortgage rates'),
    );
    $form['rates'] = array(
        '#type' => 'fieldset',
        '#title' => t('Mortgage Rates'),
        '#collapsiable' => TRUE,
        '#collapsed' => FALSE,
    );

    $form['rates']['state'] = array(
        '#type' => ('select'),
        '#title' => t('Choose One'),
        '#options' => array( 
            'AK' => t('Alaska'),  
            'AZ' => t('Arizona'),
        ),
        '#required' => TRUE
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    if(isset($zinfo['values'])){
        $form['page_output'] = array(
        'markup' => 'Submitted value:'. $zinfo['values']['state']); 
    }
    return $form;
}

/**
 * Submission Output    
*/

function zillow_form_submit($form, &$zinfo){
  drupal_set_message(t('this is the info the form has submitted. states=@states, todaysrates=@todaysrates', array('@states' => $zinfo['values']['state'], '@todaysrates' => zillow_api_info($form, $zinfo))));
  $zinfo['processed']['states'] =  serialize($zinfo['values']['state']);
  $zinfo['processed']['todaysrates'] =  serialize(zillow_api_info($form, $zinfo));
}

Edit** My code has been modified I am now only getting this error

  • Your code is a mess, if you spent some time cleaning it up it would be easier to see what's going on and you might fix the problem yourself. – googletorp Mar 20 '12 at 21:07
  • I didnt realize it was really a mess, I have comments above the functions that are being used... Anyhow its a little cleaner, but it is necessary to use my whole code – Jeremy Love Mar 20 '12 at 23:06
  • You should use Drupal naming conventions, it seems like $zinfo should be $form_state, you api function takes $form but doesn't use it. The error you get is due to an form array containing values that aren't allowed, but nothing looks wrong. (All non #attributes should be arrays). – googletorp Mar 21 '12 at 9:12
  • If that's the case and there's nothing wrong with my code then it should work correctly, however it doesn't... Even if i do take the api function off it still does not output the results... – Jeremy Love Mar 21 '12 at 18:39
6
+50

The problem is here:

if(isset($zinfo['values'])){
    $form['page_output'] = array(
    'markup' => 'Submitted value:'. $zinfo['values']['state']); 
}

Any element of a render-array that doesn't start with a # symbol is itself considered a child render-array by the drupal_render() function.

As drupal_render() expects an array when your array key is markup (without the #), it tries to treat the variable as an array. Since it's a string, PHP complains that it can't use the provided variable as an array...because it isn't one :)

This should work:

if(isset($zinfo['values'])){
    $form['page_output'] = array(
    '#markup' => 'Submitted value:'. $zinfo['values']['state']); 
}

Have a look at Render Arrays in Drupal 7 and the FAPI #markup property for more information.

EDIT

There's a slightly bigger problem: in zillow_api_info() you're accepting the $zinfo parameter by reference, and then overwriting it with this call: $zinfo = simplexml_load_string($xml);.

That code overwrites the $zinfo variable in memory, so when you use it again in your submission function it will cause errors. It's also what's causing the last two lines not to work, and $zinfo['values'] to not exist when your form is rebuilt.

The easiest solution is to change zillow_api_info() to look like this:

function zillow_api_info($form, &$form_state){
  set_time_limit(3600);
  $state = $form_state['values']['state'];
  $zillow_id = 'XXXXX';
  $xml = file_get_contents('http://www.zillow.com/webservice/GetRateSummary.htm?zws-id='.$zillow_id.'&state='.$state.'');
  $zinfo2 = simplexml_load_string($xml);

  foreach($zinfo2->response as $rates){
    $zinfo2 .= $rates->today->rate[0];
    $zinfo2 .= $rates->today->rate[1];
    $zinfo2 .= $rates->today->rate[2];
    $zinfo2 .= $rates->lastWeek->rate[0];
    $zinfo2 .= $rates->lastWeek->rate[1];
    $zinfo2 .= $rates->lastWeek->rate[2];
  }

  return $zinfo2;
}

As an extra note, you do need $zinfo['rebuild'] = TRUE in your submit function too.

I'd really advise using the $form_state variable name throughout your code when it refers to the form state array, it'll make problems like this vanish instantly :)

| improve this answer | |
  • Thank you for replying, I tried what you said and although that may be a problem is did not fix the error I am continuously getting. The error is on the last to lines of the code at the bottom. And even if I take those out, It still does not output the value that was select, for example if I select a state lets say california for example and I click submit, it should output california. It does that fine in drupal_set_message but doesnt do it on the actual page. – Jeremy Love Mar 21 '12 at 19:26
  • Try adding $form_state['rebuild'] = TRUE; into the zillow_form_submit() function...that will probably help. If it doesn't work let me know and I'll make a quick module with your code in it to find the problem – Clive Mar 21 '12 at 19:34
  • Yeah i had that in before I posted a edit of my code, it still gave off the same error, your welcome to try to make the module, I really appreciate it. – Jeremy Love Mar 21 '12 at 19:39
  • No probs I'll have a go now there's nothing good on the telly anyway :) Are you able to share a valid $zillow_id so I can get the proper data from your webservice? Don't worry if not, I'll just fudge some data – Clive Mar 21 '12 at 19:41
  • @JeremyLove I've got it working in the test module, I've edited the answer and it should help :) – Clive Mar 21 '12 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.