使用CodeIgniter,php5和MySQL在数据库上进行实时搜索 [英] Making live search on a database with CodeIgniter, php5 and MySQL
问题描述
我有一个正在进行的小项目,它的主要思想是创建一个美观的用户界面,并为用户提供在给定数据库上进行搜索的选项。
I've got a little project I'm currently working on and its main idea is to create a good-looking user interface and grant the user the option to make searches on a given database.
我有一个可运行的CodeIgniter框架,该框架可在php5上运行,并与MySQL服务器集成,目前仅存储用户名和密码。
而且,我有一个登录界面,该界面可以在成功登录后授予主页(我知道...不多,显然也没有什么值得骄傲的。)。
I've got a working CodeIgniter framework that runs on php5 and integrates with MySQL server that as for now only stores users and passwords. Moreover, I've got a login interface that grants a home page after a successful login (I know...not much, and clearly not something to be proud of).
在用户主页中,我想创建一个外观美观的实时搜索界面,该界面将允许用户执行基于以下条件的自定义搜索查询:位置,关键字,类别和时间。
In the user homepage, I want to create a good-looking live search interface that will allow a user to execute a custom search query that bases on the following criteria: Location, Keywords, Categories and Times.
从以上信息可以得出我是新手的结论。他是正确的。
我对php的了解很少,我认为这个项目是学习它的绝好机会。
From the above information, one can conclude that I am a newbie. And he is correct. I have a very little knowledge in php and I see this project as a great opportunity of learning it.
我不需要完整的代码。我只要求提供一些示例,解释,灵感,想法和学习的地方。
I don't request the full code. I ask only for some examples, explanations, inspirations, ideas, and places to learn from.
就这些!
非常感谢!
-------------------------- - - - - - - - - - - - - - - - - - -编辑 - - - - - - - ------------------------------------------------
-------------------------------------------------------------Edit--------------------------------------------------------------
好的。所以...我遵循了本指南:
http://www.technicalkeeda.com/jquery/live-search-using-jquery-ajax-php-codeigniter-and-mysql
OK. so...I followed this guide: http://www.technicalkeeda.com/jquery/live-search-using-jquery-ajax-php-codeigniter-and-mysql
没有任何效果。我更新了几行代码,使它们成为旧的CodeIgniter语法,但仍然无效。
and nothing worked. I updated a few lines that my eye caught as an old CodeIgniter syntax, and it still did not work.
这里是我的代码:
控制器-Person.php
Controller - Person.php
<?php
class Person extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Person_model');
}
public function index(){
$search = $this->input->post('search');
$query = $this->Person_model->getPerson($search);
echo json_encode ($query);
}
}
?>
模型-Person_model.php
Model - Person_model.php
<?php
class Person_model extends CI_Model {
public function getPerson($search){
$this->load->database();
$query = $this->db->query("SELECT * FROM People where last_name like '%$search%' ");
return $query->result();
}
?>
查看-home.php
View - home.php
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<style>
#search {
background-color: lightyellow;
outline: medium none;
padding: 8px;
width: 300px;
border-radius: 2px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
border: 2px solid orange;
}
ul {
width: 300px;
margin: 0px;
padding-left: 0px;
}
ul li {
list-style: none;
background-color: lightgray;
margin: 1px;
padding: 1px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
}
</style>
<script type="text/javascript" language="javascript" src="http://www.technicalkeeda.com/js/javascripts/plugin/jquery.js"></script>
<script type="text/javascript" src="http://www.technicalkeeda.com/js/javascripts/plugin/json2.js"></script>
<script>
$(document).ready(function(){
$("#search").keyup(function(){
if($("#search").val().length>3){
$.ajax({
type: "post",
url: "http://localhost/index.php/Person",
cache: false,
data:'search='+$("#search").val(),
success: function(response){
$('#finalResult').html("");
var obj = JSON.parse(response);
if(obj.length>0){
try{
var items=[];
$.each(obj, function(i,val){
items.push($('<li/>').text(val.LAST_NAME + " " + val.ID));
});
$('#finalResult').append.apply($('#finalResult'), items);
}catch(e) {
alert('Exception while request..');
}
}else{
$('#finalResult').html($('<li/>').text("No Data Found"));
}
},
error: function(){
alert('Error while request..');
}
});
}
return false;
});
});
</script>
</head>
<body>
<h1>Welcome <?= $this->session->userdata('username') ?></h1>
<a href="<?= site_url('home/logout') ?>">Logout</a>
<div id="container">
<p>Note:- Search by last name!</p>
<input type="text" name="search" id="search" />
<ul id="finalResult"></ul>
</div>
</body>
</html>
我看到一个警告框,上面写着:请求时出错。
I am being presented with an alert box that says: 'Error while request..'
我该怎么办?
随意对我发牢骚,问我一些我可能不知道答案的问题。
Feel free to capslock at me and ask me questions that I might not know the answer for.
将感谢您的帮助!
推荐答案
好的问题已经解决了!
对于那些要解决过去两天我遇到的挫折感的人,请别担心!
您要做的是将以下代码放入您的控制器(我的是Person.php):
Okay problem solved! For those who are going to tackle the frustration that I had in the past 2 days, fear no more! What you got to do is to put the following code in your controller (mine is Person.php):
$this->output->set_header("Access-Control-Allow-Origin: *");
$this->output->set_header("Access-Control-Expose-Headers: Access-Control Allow-Origin");
$this->output->set_status_header(200);
$this->output->set_content_type('application/json; charset=utf-8');
$this->output->_display();
粗略地说,这允许从不同站点发送和接收数据。
您可以阅读有关它的更多信息 这里 。
之后,您必须在发送查询的页面上添加 dataType:'json'
并返回已解析的json响应(我的视图是-> home .php)。
This allows to send and receive data from different sites, roughly speaking.
You can read more about it here.
Afterwards, you got to add dataType: 'json'
to the page that is sending the query and gets back a parsed json response (mine is view -> home.php).
希望这会有所帮助!
这篇关于使用CodeIgniter,php5和MySQL在数据库上进行实时搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!