We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34174
    • 67 Posts
    Hi folks, I'm trying to grab some TV values to display them in the front end (in a form) but I get a php error when I try to use variable variables. Can anyone say what is wrong with my syntax?

    Thanks!

      $tv_array = array(
          '7'  => 'service_description',
          '2'  => 'address_line1',
          '37' => 'address_line2',
          '12' => 'category',
          '39' => 'city',
          '13' => 'country',
          '6'  => 'email',
          '5'  => 'fax',
          '40' => 'mobile',
          '3'  => 'phone1',
          '4'  => 'phone2',
          '42' => 'intro_video',
          '8'  => 'twitter',
          '9'  => 'facebook',
          '38' => 'linkedin',
          '41' => 'skype',
          '10' => 'website',
        );
    
      foreach ($tv_array as $id=>$var)
      {
        $temp = '';
        ${'tv_'.$var} = $modx->getObject('modTemplateVarResource', array(
            'tmplvarid' => $id,
            'contentid' => $companyPageId
          ));
    
        // ===============================================================
        // Fatal error: Call to a member function get() on a non-object on line below 
        ${'tv_'.$var.'_value'} = ${'tv_'.$var}->get('value');
    
        $modx->setPlaceholder('profile.'.$var,${'tv_'.$var.'_value'});
    
      }
    
      // ===============================================================
      // these 4 lines below work but I'd need to do them manually. 
      $tv_service_description_value = $tv_service_description->get('value');
      $modx->setPlaceholder('profile.service_description',$tv_service_description_value);
    
      $tv_address_line1_value = $tv_address_line1->get('value');
      $modx->setPlaceholder('profile.address_line1',$tv_address_line1_value);
    
    
    
      • 3749
      • 24,544 Posts
      I think I'm following you, and it seems to me that the only thing you need the dynamic name for is the placeholder.


      foreach ($tv_array as $id=>$var)
      {
        $tvr = $modx->getObject('modTemplateVarResource', array(
            'tmplvarid' => $id,
            'contentid' => $companyPageId
          ));
        if ($tvr) { /* added sanity check in case of bad $tv_array values */
            $modx->SetPlaceholder('profile' . $var, $tvr->get('value);
        }
      }


      Using dynamic variable names is possible in PHP, but it's a nightmare to maintain. I don't think I've never seen an example in good production PHP code. I've done it once or twice, but in retrospect, it's always been a mistake. If the code above doesn't work for you, I'd recommend creating a new array in the loop and using that.

      Something else to think about: If there's a way to get the data in one query to the DB rather than 17 separate ones, it would speed things up a lot.

      This would get all the Tvrs in one query:

      $modx->getObject('modTemplateVarResource', array(
            'contentid' => $companyPageId,
      ));
      


      If that gets too many, you could restrict it by doing something like this:

      $query->where(array(
            'contentid' => $companyPageId,
            'tmplvarid:IN' . array_keys($tv_array),   
      ));


      Then, it might be possible to walk through the resulting array of Tvrs and set the placeholders.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 34174
        • 67 Posts
        Thanks a lot Bob. Will try this out.