Skip to content

Instantly share code, notes, and snippets.

@maksbd19
Last active March 31, 2017 09:24
Show Gist options
  • Save maksbd19/cdb466fc7103efee1b8a7648ea153064 to your computer and use it in GitHub Desktop.
Save maksbd19/cdb466fc7103efee1b8a7648ea153064 to your computer and use it in GitHub Desktop.
-- MySQL dump 10.13 Distrib 5.6.35, for osx10.9 (x86_64)
--
-- Host: localhost Database: student_test
-- ------------------------------------------------------
-- Server version 5.6.35
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `classes`
--
DROP TABLE IF EXISTS `classes`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `classes` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`class_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `classes`
--
LOCK TABLES `classes` WRITE;
/*!40000 ALTER TABLE `classes` DISABLE KEYS */;
INSERT INTO `classes` VALUES (1,'Class 1'),(2,'Class 2'),(3,'Class 3'),(4,'Class 4'),(5,'Class 5'),(6,'Class 6'),(7,'Class 7'),(8,'Class 8'),(9,'Class 9'),(10,'Class 10');
/*!40000 ALTER TABLE `classes` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `students`
--
DROP TABLE IF EXISTS `students`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `students` (
`student_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) DEFAULT NULL,
`middle_name` varchar(255) DEFAULT NULL,
`surname` varchar(255) DEFAULT NULL,
`current_class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`student_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `students`
--
LOCK TABLES `students` WRITE;
/*!40000 ALTER TABLE `students` DISABLE KEYS */;
INSERT INTO `students` VALUES (1,'John','James','Doe',10),(2,'Jane','Amos','Doe',9),(3,'Jane','James','Doe',10),(4,'John','Amos','Doe',10),(5,'John','Lews','Doe',9);
/*!40000 ALTER TABLE `students` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2017-03-25 13:24:06
<?php
defined("DB_HOST", "your_database_host_name");
defined("DB_USER", "your_database_user_name");
defined("DB_PWD", "your_database_password");
defined("DATABASE", "your_database_name");
$connection = mysql_connect(DB_HOST, DB_USER, DB_PWD) or die("unable to connect");
mysql_select_db(DATABASE) or die("unable to select");
$search_name = $_REQUEST['searchName'];
$search_class = $_REQUEST['current_class'];
$select__args = array();
$from__args = array();
$where__args = array("1=1");
$select__args[] = "*";
$from__args[] = "students";
$where__args[] = "(CONCAT(first_name,' ',middle_name,' ',surname) LIKE '%{$search_name}%') OR (CONCAT(first_name,' ',surname) LIKE '%{$search_name}%')";
if($search_class != ''){
$where__args[] = "current_class_id = '{$search_class}'";
}
$select_clause = implode(", ", $select__args);
$from_clause = implode(",", $from__args);
$where_clause = "(" . implode(") AND (", $where__args) . ")";
$query = "SELECT {$select_clause} FROM {$from_clause} WHERE {$where_clause}";
var_dump($query);
$students = array();
if ($result = mysql_query($query)){
if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_assoc($result)){
$students[] = $row;
}
}
}
echo json_encode($students);
die(0);
<?php
defined("DB_HOST", "your_database_host_name");
defined("DB_USER", "your_database_user_name");
defined("DB_PWD", "your_database_password");
defined("DATABASE", "your_database_name");
$connection = mysql_connect(DB_HOST, DB_USER, DB_PWD) or die("unable to connect");
mysql_select_db(DATABASE) or die("unable to select");
?>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Name</span>
<input class="form-control" type="text" name="searchName" id="searchName" placeholder="Search by name">
<span class="input-group-addon">Class</span>
<select name="current_class" id="current_class" class="form-control">
<option value="">Select Class</option>
<?php
$sql = "SELECT * from classes";
if ($result = mysql_query($sql)){
if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_array($result)){
echo "<option value=\"{$row['class_id']}\">{$row['class_name']}</option>";
}
}
}
?>
</select>
</div>
<div id="result"></div>
</div>
<script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#searchName').on( "keyup", function(){
var name = $('#searchName').val();
var current_class = $('#current_class').val();
if (name != '' || current_class != '') {
$.ajax({
url:"fetch.php",
method:"post",
data:{searchName:name, current_class:current_class},
dataType:"text",
success:function(data){
$("#result").html(data);
}
});
} else {
$("#result").html('');
}
});
$('#current_class').on("change", function(){
var name = $('#searchName').val();
var current_class = $('#current_class').val();
if (name != '' || current_class != '') {
$.ajax({
url:"fetch.php",
method:"post",
data:{searchName:name, current_class:current_class},
dataType:"text",
success:function(data){
$("#result").html(data);
}
});
} else {
$("#result").html('');
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment