Multiple Dynamic Select Lists from DB

  • kenryanjr
  • Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 134
  • Thank you received: 0

kenryanjr created the topic: Multiple Dynamic Select Lists from DB

I am building a vehicle classifieds application with Breezing Forms Pro 1.9.0 Stable (931) and Content Builder 0.9.91 (build 252) on Joomla 3.9.11.

I am building the Vehicle Make select list out of the database with no issues using this code in the Form Pieces/Before Form/Custom section.
$this->execPieceByName('ff_InitLib');
 
$rows = ff_select("select id, make from #__car_makes order by make asc"); $listdata = ";Select Vehicle Make;\n"; for ($i = 0; $i < count($rows); $i++) {
                $listdata .= "0;{$rows[$i]->make};{$rows[$i]->make}\n";
}
ff_setSelectList('vehicleMake', $listdata);
 
function ff_setSelectList($name, $value) {
    global $ff_processor;
    for ($r = 0; $r < $ff_processor->rowcount; $r++) {
        $row =& $ff_processor->rows[$r];
        if ($row->name==$name)
            $row->data2 = $value;
        unset($row);
    } // for
} // ff_setSelectList

Where I am having trouble is building the Vehicle Model select list based on the Vehicle Make that the user selects.

car_make table has an id field and make field
car_model table has and id field, carmodel field, and a make_id field.

I have seen several different variations of this in the forum but can't seem to find what I need to make this work.

I tried this one and built a php page that would post back the data but kept getting a 500 error. crosstec.org/en/forums/30-tips-tricks/99...ng-on-selection.html

crosstec.org/en/forums/1-forums/125334-m...om-list.html?start=0

This doesn't seem to be really hard, but I am not a fantastic coder as I am out of practice and haven't really done a lot of complicated coding.
#237609
  • Posts: 2553
  • Karma: 87
  • Thank you received: 343

mihaela replied the topic: Multiple Dynamic Select Lists from DB

Hello,

If I have understood correctly, you populate the first select list vehicleMake with the options from the database.

Would you then like to fill in the next select list BASED on what was selected in the " vehicleMake" select list, or you just want this select list to be filled in from database regardless of what was selected in the "vehicleMake "?

Regards,

Mihaela
#237620
  • Posts: 2553
  • Karma: 87
  • Thank you received: 343

mihaela replied the topic: Multiple Dynamic Select Lists from DB

Hi again,

I've seen your post on Discord:
"Where I am having trouble is building the Vehicle Model select list based on the Vehicle Make that the user selects."
That is basically the answer to make questions so I will give you instructions on how to do so.

In this topic that you have linked here the solution to your question is provided crosstec.org/en/forums/1-forums/125334-m...m-list.html?start=24 .
You should just read it carefully.

I can give you an example.
So you have to go to the Advanced properties of vehicleMake element. In the Actionscript section set Type to Custom and Action to Change.
Then in the code area below put the code similar to this one:
function ff_vehicleMake_action(element, action)
{
    switch (action) {
        case 'change':
            jQuery.ajax({
                type: "POST",
                url: "http://localhost/value.php",
                data: { vmake : element.value },    
                success: function(data) {
                var obj = JSON.parse(data);
                        jQuery('#ff_elem7509').empty();
                       var arr = obj;
                       jQuery('#ff_elem7509').append(jQuery('<option>', {
                                value: '',
                                text: 'Choose option'
                            }));
                       for (var i = 0; i < arr.length; i++) { 
                           console.log(arr[i]);
                           jQuery('#ff_elem7509').append(jQuery('<option>', {
                                value: arr[i],
                                text: arr[i]
                            })); 
                        }
                }      
            });
            break;
        default:;
    } // switch
} // ff_vehicleMake_action
NOTE: In the code above you have to replace http://localhost/value.php with the correct path to the value.php file that you will create in Joomla root folder.

Also, replace ff_elem7509 with the correct ID of the second Vehicle Model select list that you can get by inspecting that element in your form.

Then go to the Joomla root folder and create there file named value.php with the following content:
<?php

define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
	include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
	define('JPATH_BASE', __DIR__);
	require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';

$vehMake = JRequest::getVar('vmake'); 

$db = JFactory::getDBO();
$db->setQuery('SELECT `carmodel` FROM `TABLENAME` WHERE make_id ='.$db->quote($vehMake)); 
$result = $db->loadColumn();
 if(!$result){
 	echo "error";
} else {
	echo json_encode($result);
}
NOTE: In this code, you have to replace the current database query which works with the query in this case. This would work if the value of first select list is the make_id.

Let me know whether you have managed to adjust this for your case.

Regards,

Mihaela
#237635
  • kenryanjr
  • Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 134
  • Thank you received: 0

kenryanjr replied the topic: Multiple Dynamic Select Lists from DB

I am sure I am going to have more questions as I move forward, but that worked perfectly. Thank you!
#237642
  • Posts: 2553
  • Karma: 87
  • Thank you received: 343

mihaela replied the topic: Multiple Dynamic Select Lists from DB

Hello,

Great! I'm glad I've helped and that it is working for you.

Regards,

Mihaela
#237646
  • kenryanjr
  • Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 134
  • Thank you received: 0

kenryanjr replied the topic: Multiple Dynamic Select Lists from DB

How can I order by the carmodel in this code? I have tried every combination I know and nothing works.
<?php

define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
	include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
	define('JPATH_BASE', __DIR__);
	require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';

$vehMake = JRequest::getVar('vmake'); 

$db = JFactory::getDBO();
$db->setQuery('SELECT `carmodel` FROM `TABLENAME` WHERE make_id ='.$db->quote($vehMake)); 
$result = $db->loadColumn();
 if(!$result){
 	echo "error";
} else {
	echo json_encode($result);
}
#237754
Moderators: ForumSupporttomeperica
Time to create page: 0.066 seconds

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans, templates and icon packs!

Get Your Subscription Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99 for just 49! (Summer Sale)

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here