• 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: Populating A form checkbox with mysql data

Populating A form checkbox with mysql data 3 months 2 weeks ago #44050

  • lesmar
  • lesmar's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 5
Hello everone , got a bit of a problem here
I have a form with a checkbox group on it and it works fine if I hard wire the options as items.

However I have database with all this information on and was able to create the table in mysql and load it with the data that i have as items like these

A|A - Western South Pacific
B|B - Greenland, Iceland, and Kosovo (European Alternate)
C|C - Canada

because i have a flag in my table to say if that record is in use i.e. inuse set to Ý then i want to dynamically populate the checkbox from my table

some criteria for what i need

My database is not the same as the main JOOMLA database as i want to keep my data separate from JOOMLA.
I have already crafted a method to connect to my database as follows -:

<?php
use Joomla\CMS\Factory;
use Joomla\Database\DatabaseDriver;

defined('_JEXEC') or die;

global $externalDb;

// ─────────────────────────────────────────────────────────────
// Connect to external database using INI config
// ─────────────────────────────────────────────────────────────
$configPath = JPATH_ROOT . '/phpvmsgen/PhpvmsgenConfig/Phpvmsgen.ini';
if (!file_exists($configPath)) {
echo 'Config file not found at: ' . $configPath;
return;
}

$config = parse_ini_file($configPath, false, INI_SCANNER_TYPED);
$options = [
'driver' => 'mysqli',
'host' => $config,
'user' => $config,
'password' => $config,
'database' => $config,
'prefix' => ''
];

try {
$externalDb = DatabaseDriver::getInstance($options);
$externalDb->setQuery("SET NAMES 'utf8mb4'");
$externalDb->execute();
} catch (Exception $e) {
echo 'Failed to connect to external database: ' . htmlspecialchars($e->getMessage());
return;
}
the table name is Phpvmsgen_02_01_ReferenceFile
the value part of the checkbox will be field Phpvmsgen_02_01_ReferenceFile_region
the lable part should be a concat of Phpvmsgen_02_01_ReferenceFile_region then a - and then field Phpvmsgen_02_01_ReferenceFile_name
the selection should only use records where Phpvmsgen_02_01_ReferenceFile_inuse is set to Y

The site template is RSJOOMLA LAZIO
The administrator has disabled public write access.

Populating A form checkbox with mysql data 3 months 2 weeks ago #44060

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 272
  • Thank you received: 70
So are you after how to get the data and populate the dropdown? I think if you add your code and this below into the 'items' property it might work.
// ───────────────────────────────────────────────
// Fetch records where inuse = 'Y'
// ───────────────────────────────────────────────
$query = $externalDb->getQuery(true)
    ->select([
        $externalDb->quoteName('Phpvmsgen_02_01_ReferenceFile_region', 'region'),
        $externalDb->quoteName('Phpvmsgen_02_01_ReferenceFile_name', 'name')
    ])
    ->from($externalDb->quoteName('Phpvmsgen_02_01_ReferenceFile'))
    ->where($externalDb->quoteName('Phpvmsgen_02_01_ReferenceFile_inuse') . ' = ' . $externalDb->quote('Y'))
    ->order($externalDb->quoteName('Phpvmsgen_02_01_ReferenceFile_region'));
 
$externalDb->setQuery($query);
$rows = $externalDb->loadAssocList();
 
// ───────────────────────────────────────────────
// Build checkbox items
// ───────────────────────────────────────────────
$items = [];
foreach ($rows as $row) {
    $value = $row['region'];
    $label = $row['region'] . ' - ' . $row['name'];
    $items[] = $value . '|' . $label;
}
 
return $items;
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.
The following user(s) said Thank You: lesmar

Populating A form checkbox with mysql data 3 months 2 weeks ago #44061

  • lesmar
  • lesmar's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 5
Top Notch works a treat mow !!!!
The administrator has disabled public write access.

Populating A form checkbox with mysql data 3 months 2 weeks ago #44062

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 272
  • Thank you received: 70
You're welcome
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
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!