EasyUI Forum
October 18, 2025, 06:47:34 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Combobox from PHP  (Read 15574 times)
AJLX
Newbie
*
Posts: 17


View Profile
« on: January 03, 2018, 05:43:57 AM »

Hi All,

I'm trying to load data from a database into a combo box. MY current Code is this:

Code:
 <input id="cc1" class="easyui-combobox" data-options="
        valueField: 'fixture_id',
        textField: 'custom_name',
        url: 'combobox_fixture.php',
        onSelect: function(rec){
            var url = 'combobox_mode.php?id='+rec.id;
            $('#cc2').combobox('reload', url);
        }">
        
<input id="cc2" class="easyui-combobox" data-options="valueField:'mode_id',textField:'custom_mode'">



My PHP file looks like this:

Code:

<?php
    
//open connection to mysql db
$conn = @mysqli_connect(XXXXXXXX');
mysqli_select_db($conn, '
XXXXXXX');


    //fetch table rows from mysql db
    $sql = "select fixture_id, custom_name from fixture_details";
    $result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));

    //create an array
    $data = array();

    while ( $row = $result->fetch_assoc() ){
    
    
     $id = $row['
fixture_id'];
     $name = $row['
custom_name'];
   
$data[] = ['
fixture_id' => json_encode($id), 'custom_name' => json_encode($name)];
    }
    
echo json_encode($data);
   
    

This is a sample output of the JSON encode:
Code:
[{"fixture_id":"\"19\"","custom_name":"\"MAC Viper Profile\""},{"fixture_id":"\"20\"","custom_name":"\"MAC Viper Wash\""},{"fixture_id":"\"21\"","custom_name":"\"Molefay Two Light\""},{"fixture_id":"\"22\"","custom_name":"\"Mythos\""},{"fixture_id":"\"23\"","custom_name":"\"Robin 100 LEDBeam\""},{"fixture_id":"\"24\"","custom_name":"\"Robin Pointe\""},{"fixture_id":"\"25\"","custom_name":"\"Source 4 - 25\\\/50 Zoom \""},{"fixture_id":"\"26\"","custom_name":"\"Arena Zoomspot Narrow\""},{"fixture_id":"\"27\"","custom_name":"\"Arena Theatre Fresnel\""},{"fixture_id":"\"28\"","custom_name":false},{"fixture_id":"\"29\"","custom_name":"\"StudioCOB FC\""},{"fixture_id":"\"30\"","custom_name":"\"ARRI Junior 1000\""},{"fixture_id":"\"31\"","custom_name":"\"MAC Quantum \""},{"fixture_id":"\"32\"","custom_name":"\"CE Source 4 - 25\\\/50 Zoom \""},{"fixture_id":"\"33\"","custom_name":"\"Mac Aura XB\""},{"fixture_id":"\"34\"","custom_name":"\"Sunstrip\""},{"fixture_id":"\"35\"","custom_name":"\"Lanta Fireball Par64\""},{"fixture_id":"\"36\"","custom_name":"\"P-5\""},{"fixture_id":"\"37\"","custom_name":"\"4 Cell Mole\""},{"fixture_id":"\"38\"","custom_name":"\"Atomic 3000\""},{"fixture_id":"\"39\"","custom_name":"\"Spikie\""},{"fixture_id":"\"40\"","custom_name":"\"Source 4\""},{"fixture_id":"\"41\"","custom_name":"\"Sceptron \\\/ LED Bars\""},{"fixture_id":"\"42\"","custom_name":"\"XLED RGBW\""},{"fixture_id":"\"43\"","custom_name":"\"Intella Storm 1000\""},{"fixture_id":"\"44\"","custom_name":"\"Pageant CM-600Z\""},{"fixture_id":"\"45\"","custom_name":"\"XP-1000WZ\""},{"fixture_id":"\"46\"","custom_name":"\"XP-20R BSW\""},{"fixture_id":"\"47\"","custom_name":"\"GL-6\""},{"fixture_id":"\"48\"","custom_name":"\"Stormy CC\""},{"fixture_id":"\"49\"","custom_name":"\"Rama 1.2kW Fresnel\""},{"fixture_id":"\"50\"","custom_name":"\"Mirror Ball\""},{"fixture_id":"\"51\"","custom_name":"\"R1 Wash\""},{"fixture_id":"\"52\"","custom_name":"\"Source 4\""},{"fixture_id":"\"53\"","custom_name":"\"Fresnel 1.2kW \""}]

This works, mostly, but all of the values that go into the combobox have "" around them.
1) How can I get rid of these?

2) Is there a way that I can do this with a text box instead of a combobox?
« Last Edit: January 03, 2018, 05:47:07 AM by AJLX » Logged
jarry
Administrator
Hero Member
*****
Posts: 2300


View Profile Email
« Reply #1 on: January 03, 2018, 05:09:24 PM »

Please define the 'loadFilter' function to convert the original data to the desired format.
Code:
<input id="cc1" class="easyui-combobox" data-options="
        valueField: 'fixture_id',
        textField: 'custom_name',
        url: 'combobox_fixture.php',
        loadFilter: function(data){
        return $.map(data, function(row){
        row.fixture_id = row.fixture_id.replace(/[^0-9]/g,'');
        return row;
        });
    },
        onSelect: function(rec){
            var url = 'combobox_mode.php?id='+rec.id;
            $('#cc2').combobox('reload', url);
        }">
Logged
battlezad
Newbie
*
Posts: 44


View Profile
« Reply #2 on: January 04, 2018, 12:23:30 AM »

You are returning values with quotation marks around them.

{"fixture_id":"\"19\"","custom_name":"\"MAC Viper Profile\""}
-->
{"fixture_id":"19","custom_name":"MAC Viper Profile"}

Are your values in database with quotation marks?
Logged
AJLX
Newbie
*
Posts: 17


View Profile
« Reply #3 on: January 05, 2018, 12:22:04 PM »

Hi all,

I found out that the issue was due to how the data was encoded. I managed to bodge around it by using this working code:

Code:

<?php
    
//open connection to mysql db
$conn = @mysqli_connect('XXX','XXX','XXXX');
mysqli_select_db($conn'XXXX');


    
//fetch table rows from mysql db
    
$sql "select fixture_id, custom_name from fixture_details";
    
$result mysqli_query($conn$sql) or die("Error in Selecting " mysqli_error($conn));

    
$rows = array();
    while(
$r mysqli_fetch_assoc($result)) {
    
$rows[] = $r;
    }
    
    
    function 
utf8ize($d) {
    
if (is_array($d)) {
    
foreach ($d as $k => $v) {
    
$d[$k] = utf8ize($v);
    
}
    
} else if (is_string ($d)) {
    
return utf8_encode($d);
    
}
    
return $d;
    }
    
    echo 
json_encode(utf8ize($rows))


?>



« Last Edit: January 05, 2018, 01:22:54 PM by AJLX » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!