我正在尝试使用jQuery autocomplete来创建搜索栏,并且将我的行回显到availableTags中。但是我只从数据库中获得第一行。我知道我应该使用while
循环,但不确定将其放置在何处。我已经尝试了多次,但我无法同时做到。
<?php
require_once 'config.php';
require_once 'session.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$query = "SELECT `user_id`, `username`, `leeftijd`, `email` FROM `users` ";
$resultaat = mysqli_query($mysqli, $query);
$row = mysqli_fetch_array($resultaat);
var_dump($row);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jqueryui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
<?php while($row = mysqli_fetch_array($resultaat)){ ?>
$(function() {
var availableTags = [
"<?php echo $row['user_id'];?>",
"<?php echo $row['username'];?>",
"<?php echo $row['leeftijd'];?>",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$("#tags").autocomplete({
source: availableTags
});
});
<?php } ?>
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
像这样添加while循环:
while($row =mysqli_fetch_array($resultaat))
{
var_dump($row);
}
在javascript中,您可以像这样使用:
var availableTags = [
<?php while($row =mysqli_fetch_array($resultaat)){?>
"<?php echo $row['user_id'];?>",
"<?php echo $row['username'];?>",
"<?php echo $row['leeftijd'];?>",
<?php } ?>
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
现在可以正常工作了,谢谢大家。
<?php
require_once 'config.php';
require_once 'session.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$query = "SELECT `user_id`, `username`, `leeftijd`, `email` FROM `users` ";
$resultaat = mysqli_query($mysqli, $query);
$row = mysqli_fetch_array($resultaat);
var_dump($row);
echo mysqli_num_rows($resultaat);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
var availableTags = [
<?php !mysqli_data_seek($resultaat, 0)?>
<?php while($row = mysqli_fetch_array($resultaat)){ ?>
"<?php echo $row['user_id'];?>",
"<?php echo $row['username'];?>",
"<?php echo $row['leeftijd'];?>",
"<?php echo $row['email'];?>",
<?php } ?>
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
} );
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>