Forums

Dynamic dropdown from DB via PHP

duracel3634 03 May, 2020
I would like to use a Dynamic Drop Down list for a form and I am currently using the code below:

$options = array();
$db = JFactory::getDbo();
//$options[] = "Select" => "Please Select";

$db->setQuery("SELECT * FROM newsite_chronoforms_data_companies ORDER BY c_name");
$results = $db->loadObjectList();

foreach ($results as $result) {
$value = $result->aid;
$c_name = $result->c_name;
$c_address1 = $result->c_address1;
$c_town = $result->c_town;
$c_postcode = $result->c_postcode;


$options[]= $value.', '.$c_name.', '.$c_address1.', '.$c_town.', '.$c_postcode;


}

return $options;

The dropdown works however I am struggling to use the => between the $value and $c_name strings so the $value string is saved to the DB, is there any clever people in this Forum that can help me out?
Max_admin 03 May, 2020
why not use the official way with the "Read data" action ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
duracel3634 03 May, 2020
Thanks Max for your prompt response.

I had given that a consideration but I have been unable to under stand that function fully at this time.

I have tried the option using Return an array of key/value pairs, two fields must be provided, which works fine and stores the value I want but only lists 1 field, so $value is stored and $c_name is shown in the list. What I would like to do is show additional fields in the list, as per my example in the previous post, as there will duplicate's in each field so additional fields will help with the selection.

If I could easily find definitive examples to suit my needs I would certainly be steered towards.

I had been previously testing purchased versions of Breezingforms and RSForms, both have good points and bad points, but I noticed that the Forums and guidance was lacking in regular activity so I thought I would give Chronoforms a bash. Whilst Chronoforms appears to be more complex to that of your competition, I was more impressed with the activity within your Forum.

I am currently on the 10 day trial and if all works out I think I will be purchasing your product.

Once this little dropdown issue of mine is sorted, there are a few other things for me to try and if all goes well, I'm sold.

Additional functions I will be trying are:

A second Dynamic Select List within the same form which will be populated from DB but will depend on selected item from first Dynamic DB select list.

Auto-populate some Textfields based on the selection from Dropdown list 1 and auto-populate additional Textfields dependent on selection from 2nd select list in the same form.

If this all works I will be purchasing at the end of trial period.

Thank you once again Max for the prompt response

Ross
Max_admin 03 May, 2020
Hi Ross,

In order to be able to build a custom option you will need to loop over the data returned by PHP, something which is supposed to be simplified in the new v7 but its still in Beta stage (you can get it from the blog)

Regarding the other features, they are doable with the Reload feature.

if you have the time for testing now then please send me a message using the contact form and I can send you the latest v7 dev with some instructions on how to get the options setup you need working, but this will be mainly for experimntal purposes as you should not put a production form before v7 stable!

Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
healyhatman 03 May, 2020
In your fields to retrieve do it like this

id_field_name_here
CONCAT('field_name_1' , ' ' , 'field_name_2' , ' ' , 'field_name_3'):an_alias_here

Take not of the spaces around the commas.
duracel3634 04 May, 2020
Thanks healyhatman for your input.

I must admit my coding is limited and CONCAT is new to me but after some research I got it working and, unless I am doing something wrong, all it does is tidy up the code I originally posted.

I notice my original code was missing some key bits which had been removed when pasting in my post so I am uploading a txt file which is the revised version and commented out what is happening. The absolute key problem is inserting the symbol => to separate the value I want to store and the value I want to display[file=12983]PHP_demo.txt[/file]

An alternate Forms Software uses | to separate the value I wish to save from the displayed item using PHP.

Have you any further suggestions
duracel3634 04 May, 2020
Thanks for your response Max.

I am quite interested in looking at V7 and will be checking out the Blog later, how near to completed product are you?
Will V7 run separately from V6 or update V6 to V7?

I ask this as I have already started putting together something I need and I am keen to have something functional by the time I am actually allowed to return to work.

Ross
healyhatman 04 May, 2020
Using db read as shown you shouldn't be using anything to separate anything. Just put {var:readdataactionname} in the dropdown, and the first field retrieved becomes the value and the second thing retrieved becomes the display.

When you want things in there manually, it's value=display
duracel3634 05 May, 2020
Thanks for input healyhatman however I cannot get the php script to work manually using the format you suggested using just =.

Looking back at previous posts in the Forum it appears just using = has worked in the past but now the format seems to be =>. If I was to use the following:

$options openthenclosedbrackets = "fixedvalue" =>$variable;
return $options;

This would work correctly however it is a pointless to do this as I would have to manually insert a value but it would suggest I am going down the right path. My problem with my script appears to be having a variable before the =>

I think I am going to approach this from a different angle and use the built in software eg READ DATA etc.

So pursuing this angle I have the following:

Under Action I have the following Model

Read Data Model Name = js_read_comp
Database Table = newsite_chronoforms_data_companies
Select Type = Return an array of key/value pairs, two fields must be provided.
Paging for multiple results = Enabled
Page Limit = 1000
Offset =
Where Conditions =
Order Fields = c_name Ascending
Fields to retrieve=
js_read_comp.aid
js_read_comp.c_name
js_read_comp.c_address1
js_read_comp.c_town
js_read_comp.c_postcode


Under View I have the following Field named js_c_select

General Tab
Label = Select Company
Multi select? = No
Name = js_c_select
Options = {var:js_read_comp}
Selected values =


The above work as it should saving the value for the field aid and displaying the field c_name in alphabetical order.

So to expand on this, which may be beneficial for others, how can I combine the fields c_name, c_address1, c_town, c_postcode to be displayed in the drop list and retain the field aid as the value?

If I wanted to put Select or Select Address at the top of the drop list how can I achieve this?

Sorry to be a pain about this, I know I am being a bit thick about this but I hope this thread might give some positive guidance to others.
healyhatman 05 May, 2020
Why are you using the PHP at all, is what I'm asking.
healyhatman 05 May, 2020
You've chosen "return an array of key/value pairs, TWO FIELDS MUST BE PROVIDED"

I highlighted, capitalised, and underlined the bit you got wrong

In fields to retrieve, do what I said to do and don't bother with your PHP code.
duracel3634 05 May, 2020
Thank you for your very prompt resonse

So in the Fields to Retrieve I have put the following:

js_read_comp.aid
CONCAT('c_name' , ' ' , 'c_address1' , ' ' , 'c_postcode'):address


I get ther response as follows:
1054 Unknown column 'js_read_comp.'c_name'' in 'field list'

The column c_name does exist as proven when I put the following in the Fields to Retrieve
js_read_comp.aid
js_read_comp.c_name
healyhatman 05 May, 2020
Sorry remove the quotes around the field names
duracel3634 05 May, 2020
Excelent it is now working almost perfectly

As previously stated, I have an order clause as follows:
c_name Ascending

If I leave this clause in place I get the following error

1054 Unknown column 'js_read_comp.c_name' in 'order clause'

when I put the following in Fields to Retrieve

js_read_comp.aid
CONCAT(c_name , ' ' , c_address1 , ' ' , c_postcode):address

If I remove the Order Clause it works correctly however not in the order I require

If I sort by another field in the same table, that is not shown Fields to Retrieve box, I get the same error
healyhatman 05 May, 2020
Order it by your new address alias, since it starts with c_name anyway
duracel3634 05 May, 2020
Absolutely spot on, thank you so so much.

I have yet to check that it saves ok but can't think of any reason why it should not.

Thanks once again

Ps - If you are currently in Australia, don't you ever sleep!
This topic is locked and no more replies can be posted.