PHP Options example

PHP Options example

ptaylorptaylor Posts: 28Questions: 7Answers: 0

Hi, I have successfully used the Field Options method to get a full list of all the items in a table, but I need to limit the choices.

The comments in the source code, it says this about the Options method:

Get a list of values that can be used for the options list in radio,
select and checkbox inputs from the database for this field.

Note that this is for simple 'label / value' pairs only. For more complex
data, including pairs that require joins and where conditions, use a
closure to provide a query

Unfortunately, I've not been able to find an example of that. Does anyone have any sample code they could provide that will allow me to pull the values out of a database with a where restriction, or something along those lines?

Thanks,
Paul

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,179Questions: 1Answers: 10,410 Site admin
    Answer ✓

    Hi Paul,

    The self referencing join example has an example use of using Editor->options() as a closure function. Click the "Server script" tab below the table to see the code.

    Regards,
    Allan

  • ptaylorptaylor Posts: 28Questions: 7Answers: 0

    Thank you! I've defined my function, read in my data so that I could filter it, and I can loop through it to format it properly, just like the example. I want this select to have a "None" option, which would insert the record with a null value for that field, but get errors whenever I try to do so. To do that, I'm using this code above the loop that builds $out:

    $out[] = array('value' => NULL, 'label' => 'None');
    

    Whenever I try to insert a new record with None selected in the dropdown I get messages like:

    SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'null' for column 'MySelectColumn' at row 1
    

    It is odd that the error message mentions datetime format, as I don't have any datetime fields, but I think the real error is the Incorrect Integer value, as it appears to be trying to insert the record with 'null' as text.

    Any ideas?

  • allanallan Posts: 63,179Questions: 1Answers: 10,410 Site admin
    Answer ✓

    I would suggest giving it an empty string value. If you want it to actually be null in the database use the nullEmpty set formatter (since null can't be sent using a plain HTTP text parameter).

    Allan

  • ptaylorptaylor Posts: 28Questions: 7Answers: 0

    Thanks allan, this solved that issue!

This discussion has been closed.