Question about Select and Title.

Question about Select and Title.

JustereJustere Posts: 8Questions: 4Answers: 0
edited September 2014 in Editor

Hello,
I have following code to load data from table to select list:

if ( ! isset($_POST['action']) ) {
    $out['lecturers'] = $db
        ->selectDistinct( 'lecturers', 'id_lecturer as value, surname as label' )
        ->fetchAll();
}

Question is:

It's possible to have few columns names at one label?
For example:

id_lecturer as value, surname & name & middlename as label.

For usability only.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Hi,

    Yes indeed. The label string can be anything you want and concatenating the values from columns is certainly an option. To do that use the sql() method with the concat or concat_ws SQL functions. For example you might have:

    $out['lecturers'] = $db
        ->sql( 'SELECT DISTINCT id_lecturer as value, CONCAT_WS(' ', surname, name, middlename) as label ORDER BY label ASC' )
        ->fetchAll();
    

    Allan

  • JustereJustere Posts: 8Questions: 4Answers: 0
    edited September 2014

    Thank you very much , Allan.

    I'd like to add a little for users, who might read this thread.

    It is necessary to add "FROM" parameter, because without it there will be no query.

        $out['lecturers'] = $db
                ->sql( 'SELECT id_lecturer as value, CONCAT(surname," ",name,middle_name) as label FROM lecturers ORDER BY label ASC')
                ->fetchAll();
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Oops! Thanks for pointing that out!

    Allan

This discussion has been closed.