关于如何转换此Laravel-5.1代码的任何想法。我有这个代码在纯PHP中运行,因为我正在使用Laravel进行开发,所以我想写在Laravel中。将我指向已经完成此操作或提供代码段的教程将受到高度赞赏。以Laravel-5.1格式重写纯PHP代码
<?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>
请阅读:https://laravel.com/docs/5.1/requests –
由于您已经在使用Laravel进行开发,因此应该很简单:创建新的控制器来处理请求,创建geonames_names模型以从中获取数据数据库,返回JSON响应。你正在努力与哪些? –
http://stackoverflow.com/questions/34679066/using-bootstrap-typeahead-in-laravel-5-1 @Alex Blex我试过实现,但没有运行。请,我已经发布了我上面的执行链接。尽管json已经返回,但我认为这是不可行的。 – kehinde