Populating Var Colors with database data

Asked

Viewed 33 times

0

Now it is populating only with a license plate, must 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>
    <script src="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>

    <!-- Ignite UI Required Combined JavaScript Files -->
    <script src="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>

            <meta name="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>
  • I did not understand the question. I could not run here by the site, but if save and try to play on the host site it runs good. Wanted help for after making select play on the color there the plates.

  • You have to include the php code you are using, because your question is mostly about php and not javascript. It just so happens that the values php will generate will likely be within a javascript block

1 answer

1


Your problem is that you’re making a loop where you’re always declaring the same variable, which means you only have one value. Try it this way:

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’re still using, mysql_*, which is vulnerable to multiple attacks and in future versions of PHP will be removed.

I advise you to use PDO or MYSQLI and tries at most to separate php and javascript.

  • Thanks for the advice, I’m learning in practice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.