【发布时间】:2016-04-13 00:46:06
【问题描述】:
关于如何转换此 Laravel-5.1 代码的任何想法。我有这段代码在纯 PHP 中运行,我想用 Laravel 编写它,因为我使用 Laravel 进行开发。向我指出已经完成此操作的教程或提供代码 sn-p 将不胜感激。
<?php
if(isset($_POST['query'])){
mysql_connect('localhost', 'root', '');
mysql_select_db('tradersmart');
$query = $_POST['query'];
$sql = mysql_query("SELECT name, timezone_id FROM geonames_names WHERE name LIKE '%{$query}%'");
$arrayName = array();
$arrayTimezone = array();
while($row = mysql_fetch_assoc($sql)){
$arrayName[] = $row['timezone_id'];
$arrayTimezone[] = $row['name'];
}
echo json_encode($arrayName +$arrayTimezone);
}
?>
这是 HTML 文件:它使用 JSon 和 typeahead 来加快建议速度。
<body>
<div class="well">
<input type="text" class="css-input" id="typeahead" data-provider="typeahead">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script>
$(function(){
$('#typeahead').typeahead({
source: function(query, process){
$.ajax({
url: 'http://localhost:2222/bootstrap/source.php',
type: 'POST',
data: 'query=' +query,
dataType: 'JSON',
async: true,
success: function(data){
process(data);
}
});
}
});
});
</script>
</body>
【问题讨论】:
-
既然你已经在使用 Laravel 进行开发,它应该很简单:创建新的控制器来处理请求,创建 geonames_names 模型来从数据库中获取数据,返回 JSON 响应。您正在为哪些问题苦苦挣扎?
-
stackoverflow.com/questions/34679066/… @Alex Blex 我已经尝试了实现,但它没有运行。拜托,我已经在上面发布了我的实现链接。虽然返回了 json,但我认为它的 typeahead 不起作用。
标签: php arrays json html bootstrap-typeahead