Populating Var colors with bank data

0

Now you're populating just with a license plate, it should be something with my while.

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <!-- Ignite UI Required Combined CSS Files -->
    <link href="http://cdn-na.infragistics.com/igniteui/2017.1/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet" />
    <link href="http://cdn-na.infragistics.com/igniteui/2017.1/latest/css/structure/infragistics.css" rel="stylesheet" />

    <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script><scriptsrc="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script><!--IgniteUIRequiredCombinedJavaScriptFiles--><scriptsrc="http://cdn-na.infragistics.com/igniteui/2017.1/latest/js/infragistics.core.js"></script>
    <script src="http://cdn-na.infragistics.com/igniteui/2017.1/latest/js/infragistics.lob.js"></script><metaname="author" content="Clube dos Geeks">
        <script type="text/javascript" src="jquery/jquery-3.2.1.min.js.js"></script>
 <script type="text/javascript">           

    </script>


</head>
<body>
    <style>        
        .combo-label {margin-bottom:.5em;}
       </style>
<h4 class="combo-label">Selecione as matrículas:</h4>
<div id="checkboxSelectCombo"></div>

   <?php
include 'conexao.php';
$sel= "select matricula FROM usuarios WHERE ativo = 1";
$query = mysql_query ($sel, $conexao) or die(mysql_error());

?>

<script>
    <?php
while ($result = mysql_fetch_array($query))
{ ?>
    var colors = [
        { Name: "<?php echo $result['matricula']; ?>" },
    ];
<?php } ?>

    $(function () {

        $("#checkboxSelectCombo").igCombo({
            width: 300,
            dataSource: colors,
                textKey: "Name",
                valueKey: "Name",
                multiSelection: {
                    enabled: true,
                    showCheckboxes: true
                },
                dropDownOrientation: "bottom"
            });

        });

    </script>

</body>
</html>
    
asked by anonymous 06.08.2017 / 15:42

1 answer

1

Your problem is that you are looping where you are always declaring the same variable, which means you only have one value. Try to do this:

PHP

$colors = [];
while ($result = mysql_fetch_array($query)) {
    $colors[] = $result['matricula'];
}

//converte ARRAY PHP para json
$colors = json_encode($colors);

JAVASCRIPT

<script>
    var colors = <?= $colors; ?>
</script>

But this is bad practice. I noticed in your code that you are still using, mysql_* , which is vulnerable to various attacks and future versions of PHP will be removed.

I'd advise you to use PDO or MYSQLI and try at most to separate php and javascript.

    
06.08.2017 / 17:01