PHP Field Option Multiple Fields

PHP Field Option Multiple Fields

franks59franks59 Posts: 16Questions: 2Answers: 1

In PHP, when using Field->options, Is there a way to specify multiple columns be returned to the corresponding Select in the Editor?

If not, is there anyway else to populate the Select with data from more than one column?

Thanks,

Frank

Replies

  • mRendermRender Posts: 151Questions: 26Answers: 13
    if ( !isset($_POST['action']) ) {
       
       $res = ($data['projectList'] = $db->query( 'select' )
      ->distinct( true )
      ->table( 'projects' )
      ->get( 'userprojects.projectid' )
      ->get( 'projects.projectname' )
      ->get( 'clients.clientname' )
      ->join( 'userprojects', 'userprojects.projectid = projects.projectid' )
      ->join( 'clients', 'clients.clientid = projects.clientid' )
      ->where( 'userprojects.userid', $userid )
      ->order( 'clients.clientname ASC' )
      ->exec());
      
        $data['projectList'] = array();
        while ( $row = $res->fetch() ) {
            $data['projectList'][] = array(
                "value" => $row['userprojects.projectid'],
                "label" => $row['clients.clientname'].' - '.$row['projects.projectname']
            );
        }
    

    This is something that I have used personally. Basically you have to create a custom select query and then populate the array that you get back with the corresponding value or label that you want to appear.

  • franks59franks59 Posts: 16Questions: 2Answers: 1
    edited February 2015

    Thanks, I'll give that a try.

    Frank

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Is there a way to specify multiple columns be returned to the corresponding Select in the Editor?

    As in you want the label to show information from multiple columns?

    Best way is probably to use options() with a closure function and a database query like what @Modgility has above. You can also execute SQL directly using the sql() method if you wanted to have the db do the string concatenation.

    There is an example using a closure for options here and API documentation here.

    Allan

  • franks59franks59 Posts: 16Questions: 2Answers: 1

    Thanks Alan,

    I can see how that would work, but I'm not doing a self-join. The data in options is coming from another table - one not currently associate with the $db variable.

    I assume I need to instantiate a second $db to be used in the options, but it's not clear to me how to go about that.

    I tried instantiating a 2nd Editor but the script just dies at that point.

    Frank

  • mRendermRender Posts: 151Questions: 26Answers: 13

    Can you show us what you have and where the problem is? That way we can help you out better.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I can see how that would work, but I'm not doing a self-join. The data in options is coming from another table - one not currently associate with the $db variable.

    Sure - that doesn't matter. That example was really just showing you how you can use a closure function for options() - I wasn't really expecting you to use the query that it uses.

    The $db instance will be able to query the order tables just fine, as long as the database user for the connection has access to those tables?

    Allan

  • franks59franks59 Posts: 16Questions: 2Answers: 1

    Actually that worked just fine. I had a typo in my code.

    Thanks,

    Frank

This discussion has been closed.