• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: SQL Query through form. Is it possible?

SQL Query through form. Is it possible? 17 years 10 months ago #931

  • dino00
  • dino00's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 11
Hi to everyone.

First of all my congrats to alex for his great extension.

What I want to do is populate a select box with text values in my database (thus performing an sql query). Is this possible and if yes how am I suppose to do it?

Thnaks to everyone
Bye!
The administrator has disabled public write access.

Re:SQL Query through form. Is it possible? 17 years 10 months ago #1000

  • alex
  • alex's Avatar
  • OFFLINE
  • RSJoomla! Official Staff
  • Posts: 443
  • Thank you received: 3
Hello Dino,

you can do this with RSform! 1.0.4. You have to follow this small tutorial:

Edit your form, go to Scripts, and add this script in the Script called on form display:
 
$database->setQuery(\"SELECT value,text FROM #__mytable\"«»); // here we extract our data from the table.
$results = $database->loadObjectList();
 
foreach($fields as $i=>$field){
	if($field->name=='selectbox'){ //change select box with your field name
		if(!empty($results)){
			foreach($results as $result){
				$fields[$i]->default_value[]=$result->value.'|'.$result->text; // here we are creating an array which contains all the selectbox options 
			}
		}else{
			$fields[$i]->default_value[]='|No option'; //if no options found, display something. (array empty)
		}
	    $fields[$i]->default_value = implode(',',$fields[$i]->default_value); // here we create the default selectbox syntax value1|text1,value2|text2 etc.
	}
}

Let me know if that worked
The administrator has disabled public write access.

Re:SQL Query through form. Is it possible? 17 years 9 months ago #1138

  • tjmoyer
  • tjmoyer's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 1
I'd like to do something similar. I want to populate parts of my event registration form with data from registered user table, if user is logged in.

I started to rewrite your script, Alex, to work for this, but perhaps I'm just too tired. Can't seem to finish it properly. Any help would be appreciated.

Here's my form: event registration form

And here's my incomplete rewritten script:
$database->setQuery(\"SELECT name, email FROM jos_users\"«»); // here we extract our data from the table.
$results = $database->loadObjectList();
 
foreach($fields as $i=>$field){
    if($field->name=='lname'){ //change select box with your field name
        if(!empty($results)){
            foreach($results as $result){
				if ($result->name) {
					list ($fname, $lname) = split (' ', $result->name);
				}
                $fields[$i]->default_value[]=$result->value; // here we are creating an array which contains the field value
            }
        }else{
            $fields[$i]->default_value[]=''; //if no options found, display something. (array empty)
        }
    }
}

Thanks,
tj
The administrator has disabled public write access.

Re:SQL Query through form. Is it possible? 16 years 8 months ago #4074

  • tobypsl
  • tobypsl's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 62
  • Thank you received: 1
does the above code work with RSFormPro because I am getting a blank page :huh: my forms without scripts are fine though.
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!