setQuery($dbQuery);//echo($dbQuery );// load the data$speciesRecords=$db->loadAssocList();//print_r ($speciesRecords);?>"As I say - that fills the tag fine. I had it in mind to use the data passed in that array to manually set the selected value (if PassedValue == $aSpeciesRecord['id'] then add 'selected'. But I can't work out how to get at that value PassedValue. The odd thing is that, if I refresh the window after the first load, then the correct selected value is 'marked'. I am thus wondering if there is something that I should do after filling that tag to force the 'select' value.Any help greatly appreciated.Nick"> Fill <select> from db - set 'selected' option - Forums

Forums

Fill &lt;select&gt; from db - set 'selected' option

NickOg 21 Mar, 2012
Hi,

I am now running 4.0 RC3.21.

I have a form that uses a <select><option> tag set. I need to fill it from the database. That works fine. My problem is that I can't get the '
<option selected ...
option to work. So, debugger shows me this

 [option] => com_chronoforms
    [chronoform] => edtheAcquiredStock
    [acqToken] => 13
    [Itemid] => 
    [Acquired_Stock_ID] => 13
    [idSpecies] => 12
    [Source] => Noosa Landcare
    [Date_Acquired] => 2011-01-26
    [Qty_Acquired] => 3
    [Remaining_Qty] => 0
    [Comment] => 


And the selected value I need to trap is passed by that[idSpecies] field.

This is the code I am using to fill the ,select. tag.

function fillSpeciesSelectBox(){
//fb ('fillSpeciesSelectBox',FirePHP::INFO);
// build a query to list species
$db =& JFactory::getDBO();
$dbQuery ="SELECT id,Scientific_Name, Common_Name FROM  #__RV_Species ORDER BY Scientific_Name, Common_Name";
// set the query
$db->setQuery($dbQuery);
//echo($dbQuery );
// load the data
$speciesRecords=$db->loadAssocList();
//print_r ($speciesRecords);
?>
<select size="1" style="width:400px;" label_over="0" hide_label="0" id="ididSpecies"  name="idSpecies">
<?php
foreach ($speciesRecords as $aSpeciesRecord) {
?>
<option value="<?php echo $aSpeciesRecord['id'];?>"
</option><?php echo $aSpeciesRecord['Scientific_Name'].' "'.$aSpeciesRecord['Common_Name'].'"';
}
?>
</select>
<?php
return;
}

As I say - that fills the tag fine.
I had it in mind to use the data passed in that array to manually set the selected value (
if PassedValue == $aSpeciesRecord['id']  then add 'selected'
. But I can't work out how to get at that value PassedValue.

The odd thing is that, if I refresh the window after the first load, then the correct selected value is 'marked'. I am thus wondering if there is something that I should do after filling that <select> tag to force the 'select' value.

Any help greatly appreciated.

Nick
GreyHead 21 Mar, 2012
Hi NickOg ,

A sideways answer but the easiest way to do this is to use the new Dynamic Data tab in the Select Box element. You can load the data with a DB Record Loader and then create the options with the Dynamic Data tab - and I think it will also set the selected value for you if there is a value set in the $form->data array.

Bob
NickOg 21 Mar, 2012
Hi Bob,

sounds interesting - I will have a go at that - should beat coding it in place on every occasion.

Thanks

Nick
NickOg 21 Mar, 2012
Hi Bob,

A very quicklook and I must have something wrong - see attached. I am looking through the forum to find out what I have done wrong and came across your Custom Select [GH] - for which I owe you a beer. As I understand it that won't answer this specific problem but will answer some others.

But a thought - would it be possible to automatically fill those options files with a PHP query to a database? Would that be a useful possible addition?

I suspect that my error is a syntax mistake. I must look at it more closely.

Regards

Nick
GreyHead 22 Mar, 2012
Hi NickOg,

That does look like a syntax error of some kind. The code showing doesn't look like anything you'd find in ChronoForms though.

Evidently my brain wasn't fully working yesterday . . . you don't need the Custom Select action to get optiosn from a DB query. Using the Dynamic settings of a select box with a DB Multi Record Loader action will do it for you.

In the Fields box of the DB Multi-Record Loader put the names of the column(s) you want to use e.g for articles id,title and set a Model ID e.g. articles.

On the Dynamic Data tab of the Select element put the Mode ID in the Data Path box and the column name(s) in the Value Key and Text Key boxes.

Bob
NickOg 22 Mar, 2012
Thanks Bob. I will try that. That sample code was directly from chronoforme. I'id I switch off the dynamic option it goes away.
NickOg 22 Mar, 2012
Morning Bob,

I have looked at this (and confirmed that the code in my earlier example was generated by chronoforms) and don't understand.

It may be I didn't explain the context of my problem. I am using the multi loader and single record edit examples from the tutorials. So the multi record loader runs fine and I can generate the report that I want. I can then load the single record screen via a link from that multi record form. It is the single record editor that I am having trouble with. That loads the data OK but I need to include code in that form to fill the select box with data from the database and to mark the current value. And in [u]that/u] form there is no multi record loader to do as you describe.

Have I missed something vital here? I thought your first suggestion matched my problem but I need to go look again and see where I have that wrong.

Nick

Hi NickOg,

That does look like a syntax error of some kind. The code showing doesn't look like anything you'd find in ChronoForms though.

Evidently my brain wasn't fully working yesterday . . . you don't need the Custom Select action to get optiosn from a DB query. Using the Dynamic settings of a select box with a DB Multi Record Loader action will do it for you.

In the Fields box of the DB Multi-Record Loader put the names of the column(s) you want to use e.g for articles id,title and set a Model ID e.g. articles.

On the Dynamic Data tab of the Select element put the Mode ID in the Data Path box and the column name(s) in the Value Key and Text Key boxes.

Bob

GreyHead 22 Mar, 2012
Hi Nick,

I was wrong, the code you have intruding there is from (I think) the DB Save action and my best guess is that there is something breaking the 'Dynamic model code that is built just before that line. I see that there is no closing ?> tag in the first code you posted here, please try adding one if it really is missing and not just lost in the copy and paste.

If that isn’t the problem then maybe seeing the page source would help pin it down more accurately.

As your options are coming from a database table and you are reading multiple records you 'could' use a DB Multi Record Loader even if at present you are writing the code by hand.

Bob
NickOg 22 Mar, 2012
Thanks (again) for the fast reply, Bob.

I will check that closing '?>' and if I can't find it, send you a copy of the page source.

As to that last para - are you suggesting that I could use a multi record loader instead of the single DB Record loader for the edit of a single record and use the parameters to narrow that multi record to a single record? Have I understood that right? I will try it and see later in the day.

Nick
GreyHead 23 Mar, 2012
Hi Nick,

You are using this query
$dbQuery ="SELECT id,Scientific_Name, Common_Name FROM  #__RV_Species ORDER BY Scientific_Name, Common_Name";
to get multiple records from a table to build the options for your drop-down. You could use the DB Multi-record loader to do that work for you.

Bob
NickOg 23 Mar, 2012
I just suspect that chronoforme is so powerful I am only scratching the service of what I can do with it and resort to 'manual' methods too quickly! I will give it a try!

Thanks Bob.
NickOg 25 Mar, 2012
G'day Bob,

I have made some progress - firstly with your suggested use of a multi-record - which works fine. I had never thought of adding more than one db event.

But my basic problem remains - I just get the code shown before - basically a stack of php source code. I have included that as attachment 1. I have attached a backup of the source form and as you can see, there is no code of mine in the form now. So unless I have filled the elements or events in wrong, I can't see that it can be a syntax error of mine. See attachment 2.

I am using an Apache server on my W7 box (Softaculous AMPPS) and using chronoforms 4.0 RC3.21. It struck me that maybe there was some problem there so I shifted the code to my remote Apache/Unix server to check it there. My original hand crafted code works fine there. It does not cause that crash shown in attachment 1. It loads the select box and picks the selected value correctly. Now, I have not yet applied the latest Chronoforms patch to that machine - it is still on 4.0 RC2.0. So the dynamic tab isn't there and select box does not get filled, of course, on the copy of the form that I transferred from RC 3.21.

I can upgrade to the latest Chronoforms but I anticipate that I will be back to the crash shown. Is it therefore reasonable to assume that this latest RC has a bug? I would rather not try that out for the moment as at least it seems that I can work round the problem if it is related to this latest RC.

Regards

Nick
GreyHead 25 Mar, 2012
Hi Nick,

Looking at the source code for the error you are seeing you'll find this (along with a lot of other error code)

Fatal error: Call to a member function get_array_value() on a non-object in W:\Ampps\www\banyanview\components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 4


The non-object message tyically shows up when a table doesn't exist, or it has a dash or other special character in the table name or a column name.

Bob
NickOg 28 Apr, 2012
Hi Bob,
I have at last managed to get back to this problem and I have made some advances in using a multi-record loader to load the data into the select boxes. So my events screen look like this.
[attachment=1]4Gh01.png[/attachment]
That clearly works because I can see the data in a dump under the model numbers I used for each multi record loader, so.
[attachment=0]4Gh02.png[/attachment]
So the data is there and thus the source tables do exist..
My problem seems to be in making the connection between that data and the corresponding Select Box This the way that I have set up that first select box
[attachment=2]4Gh03.png[/attachment]

But set up that way all I get is a screen with just an empty select box. If I switch off the dynamic setting for the first box, then the screen comes up and stops at the Locations select boxes. Neither are filled!

I am running Joomla 1.7.3 and the lastest chronoforms 4.0 RC3.3

So near yet so far! How do I get that data from $form into the select box?
GreyHead 30 Apr, 2012
Hi Nick,

I think that a bit more pre-processing is required on the data. You want to end up with an array that looks like this:
$some_array = array(
  0 => array(
    'id' => 1,
    'Scientific_name' => 'Agathis robusta'
  ),
  1 => array(
    'id' => 2,
    'Scientific_name' => 'Someother name'
  ), 
. . .
)

Running a foreach loop through the data you have should let you create this version.

Bob
NickOg 30 Apr, 2012
Ah hah! Thanks Bob. That way or use a view to just pick those two fields? Just trying that out now.
This topic is locked and no more replies can be posted.