92 Posts
Seb
10 years ago
6
Topic
Hello,
What plugin allows to complete a field with dynamique value with sql query?
Thank you for your reply
Seb
Get a Book for SEBLOD
92 Posts
Seb
10 years ago
0
Level 1
Hi,
I have not found the answer unfortunately.
I would like to complete a field (before form render) with a value from a custom sql query (all database not only Seblod tables)
Tanks for your help
Seb
10 years ago
0
Level 1
Hello Seb,

what you are searching for is called "Autocomplete" and uses Ajax to retrieve values from the DB on the fly.

I found a video in Spanish that explains the process with Seblod.

Some time ago I implemented autocomplete in a Seblod project. I used a position override with the required jQuery code.

Google "jQuery autocomplete" and you will find some plugins/code that you can use.

10 years ago
0
Level 1
Here is some code from that project.

First my code for the position override of the search form.
<?php
$jyaml = JYAML::getDocument();
$jyaml->addScript($jyaml->getUrl('script', 'jquery.autocomplete.min.js'));
//require_once 'templates/seb_one/positions/lehrerliste/search/search.php';
?>
<p><?php echo JText::_('COM_CCK_SORTIERT_NACH_PLZ'); ?></p>
<?php echo $cck->renderField('gbr_lehrer_suchtext'); ?>
<div id="gbrsearch">
<?php echo $cck->renderField('cck'); ?>
<?php echo $cck->renderField('gbr_user_list'); ?>
<span class="no1"><?php echo $cck->renderField('user_name'); ?>
<?php echo $cck->renderField('gbr_user_plz'); ?></span><br />
<span class="no2"><?php echo $cck->renderField('gbr_user_cert_dd'); ?></span>
<div style="clear:both;"></div>
<?php echo $cck->renderField('button_submit'); ?>
</div>
<script type="text/javascript">
function selectItem(li) {
    return false;
}
function formatItem(row) {
    return row[0] + "<br><i>" + row[1] + "</i>";
}
var baseuri = '<? echo JURI::base(); ?>';
$j().ready(function() {
    $j("#gbr_user_plz").autocomplete(baseuri + '/components/com_gbrajaxcall/platform/autocompplz.php', { 
        width: 200,
        selectFirst: false
    });
    $j("#user_name").autocomplete(baseuri + 'components/com_gbrajaxcall/platform/autocompname.php', { 
        width: 200,
        selectFirst: false
    });
});
</script>
I am using jQuery autocomplete script.

You can see a call to /components/com_gbrajaxcall/platform/autocompplz.php.
This file must be placed within the Joomla component folder, otherwise Joomla doesn't allow Ajax Calls. I created a folder /components/com_gbrajaxcall. In that folder is a folder "platform".
There I placed a file /components/com_gbrajaxcall/platform/joomla_platform.php:
<?php
    /* If not already done, initialize Joomla framework */
    if (!defined('_JEXEC')) {
    //init Joomla Framework
    define( '_JEXEC', 1 );
    }
    define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' )); // print this out or observe errors to see which directory you should be in
    define( 'DS', DIRECTORY_SEPARATOR );
    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
//    require_once( JPATH_CONFIGURATION   .DS.'configuration.php' );
//    require_once ( JPATH_LIBRARIES .DS.'joomla'.DS.'database'.DS.'database.php' );
    require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
    require_once ( JPATH_LIBRARIES .DS.'import.php' );
    JFactory::getApplication('site')->initialise();
?>
And a file autocompplz.php for fetching the data from the database:
<?php
define('_JEXEC', 1);
// Fix magic quotes.
@ini_set('magic_quotes_runtime', 0);
// Maximise error reporting.
@ini_set('zend.ze1_compatibility_mode', '0');
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'joomla_platform.php';
$q = strtolower($_GET["q"]);
if (!$q) return;
$db = JFactory::getDBO();
$query = "SELECT gbr_user_plz,gbr_user_ort FROM j25_cck_store_form_user WHERE gbr_user_plz LIKE '".$q."%' ";
$db->setQuery( $query );
$row = $db->loadAssocList();
foreach($row as $id => $assoc) {
        echo "$assoc[gbr_user_plz] $assoc[gbr_user_ort]|$assoc[gbr_user_plz]\n";
}
?>

This all together gives you a autocomplete field. You can see it in action here. The fields Name and Postleitzahl in the "Lehrersuche" box.

Hope this helps
Gerhard

92 Posts
Seb
10 years ago
0
Level 1
Tank you very much Gebeer for your reply,
I not want autocomplete field exactly.
For example I want to display the number of people who've chosen an option = "8 peoples have chosen this option" with sql "SELECT COUNT" in database.
Seb
10 years ago
0
Level 1
Hi Seb,
I see, something like the facebook Like button that gets updated on the fly.

The principle will still be the same: an Ajax call in the background with your SQL query. Then inject the result into the page.

Best way to implement this is custom template or position override.
92 Posts
Seb
10 years ago
0
Level 1
Hi Gebeer,
Ok I'll try
Thank you very much!
Seb
Get a Book for SEBLOD