Home > database >  Can SUBSTR() query change the output format of an array such that javascript field listeners can no
Can SUBSTR() query change the output format of an array such that javascript field listeners can no

Time:01-10

Really struggling to make a concise question without it being two phrases.

All my code worked until I started uses SUBSTR() in my mySQL query.

I have a list of folder numbers (F123456, F233999, etc).

I had a nice function that detected user input into a field, and the field would change color if their entered value was in the array.

After a while I realized the "F" wasn't needed, so I used SUBSTR (value, 2, 6) to drop the "F" in the query result and just return the remaining 6 digits. The query works both in the PMA window and if I print_r or var_dump the results.

But it seems like the array isn't being read fully, down in the javascript code.

This is the query where 'value' is the name of the column where the #'s reside. It sits across from 'cust_folder' which is in the variable column.

$getFileNumbers = $conn->query(" SELECT SUBSTR(value, 2, 6) 
                                FROM smfqg_themes 
                                WHERE variable = 'cust_folder' ");

while($rows = $getFileNumbers->fetch_object()) {
    $fileNumberArray[] = $rows; 
} 

This is the javascript:

<input type="text" size="10"   id="idk" name="file_number" onblur="checkNumber()" onkeyup="checkNumber()" >  

<script type="text/javascript">
const idk = document.getElementById('idk');


idk.addEventListener('input', checkNumber);
idk.addEventListener('focus', checkNumber);
idk.addEventListener('blur', checkNumber);

function checkNumber() {
    
     const theArray = <?php echo json_encode($fileNumberArray); ?>;
     
     if (theArray.includes(idk.value)) {
        idk.style.background = "green"   
     } else if (theArray.find(item => item.value === idk.value) !== undefined) {
        idk.style.background = "green";
     }else {
        idk.style.background = "red";
     }
}         
</script>

Is there a format step I'm missing--by using SUBSTR--that is needed to get the cont "theArray" looked at properly?

I have printed and dumped the array, both using an alias and without, and the results produce all the 6-digit values.

I have tried using SUBSTR with both (value, 2, 6) and (value, -6,6) but there are no changes.

CodePudding user response:

When your query was SELECT value ....

the column name in the resultset was called value When you changed it to SELECT SUBSTR(value, 2, 6)

the column name in the resultset was called SUBSTR(value, 2, 6)

So give that column an alias like this

SELECT SUBSTR(value, 2, 6) as value

Now the column will again have the correct name and there will be a property of value instead of a propery of SUBSTR(value, 2, 6)

Then javascript code that addresses idk.value will work again.

  • Related