This topic is locked
[SOLVED]

explode a concatenated string to use for lookup values

8/9/2022 4:07:08 PM
PHPRunner General questions
R
rmac author

I have a lookup table with a field of concatenated values that I need to be able to use as separate values in a select dropdown. i.e when the floor location chosen is A1, the hgts options are one of 0, 4, 8 or 12.
Could I use PHP explode somewhere to accomplish this, or would it need to be done in a field event using javascript? Any suggestions and examples (especially if a field event) would be appreciated. Thanks.

img alt

Sergey Kornilov admin 8/10/2022
R
rmac author 8/11/2022

Thank you! Very inspirational, and all seems to work up until the very last part. I modified the code as below:

**JS**
var ctrlFloor = Runner.getControl(pageid, 'location_floor');
var ctrlHgt = Runner.getControl(pageid, 'location_hgt');

var func = function(e){
$.post("hgts_lookup.php", {
'value_location_floor_26' : this.getValue() },

function(data) {
var sel = $("#value_location_hgt_26");
sel.empty();
for (var i=0; i<data.length; i++) {
sel.append('<option value="' + data[i] + '">' + '</option>');
}
}, "json");
};

ctrlFloor.on('keyup', func);
ctrlFloor.on('change', func);
<?php
include("include/dbcommon.php");

$result = array();
$floor = db_addslashes(trim(postvalue('value_location_floor_26')));
$sql = "SELECT hgts FROM locations_lu WHERE floor = '$floor'";
$rs = CustomQuery($sql);

while ($data = db_fetch_array($rs)) {
$result[] = array(
$data['hgts'],
);
}
$exp = explode(",", $result[0][0]);
echo json_encode($exp);

?>

If I fix the input on the PHP script (ie $Floor = ('S5');) I get the expected result back in the browser. ["16","15","14","13","12","11","8","0"].

But in the application, although it clears the original data in the hgts lookup and inserts the correct number of rows in the dropdown, the rows are always blank. I'm sure I'm missing something related to data formatting, but nothing I've tried has given me any hints. I'm sure someone wise in the ways of jQuery can give me a clue. Once it's working, I'll add comments to the Tips and Tricks article to point out issues not so obvious to a JS/ jQuery newb. TIA.

Sergey Kornilov admin 8/11/2022

I'm glad you are making progress!

Unfortunately, it is not easy to tell what exactly is wrong by just looking at the code. You need to step though your Javascript code, step by step, inspect variable values to see what exactly is not working.

Here is a video that can help you with this task.

R
rmac author 8/11/2022

Got it. I goofed up the <option value syntax. I changed the line to
sel.append('<option value="' + data[i] + '">' + data[i] + '</option>'); and all seems to work now.