Advertisement
Guest User

Untitled

a guest
Feb 19th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /* Connection vars here for example only. Consider a more secure method. */
  2. $dbhost = 'xxx.db.xxx.xxx.com';
  3. $dbuser = 'user';
  4. $dbpass = 'password';
  5. $dbname = 'name';
  6.  
  7. try {
  8. $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
  9. }
  10. catch(PDOException $e) {
  11. echo $e->getMessage();
  12. }
  13.  
  14. $return_arr = array();
  15.  
  16.  
  17. // Do Prepared Query
  18. $query = $conn->prepare("SELECT booth,exhibitor FROM exhibitors WHERE exhibitor LIKE :search LIMIT 40");
  19.  
  20. // Add a wildcard search to the search variable
  21. $query->execute(array(':search'=>"%".$search."%"));
  22.  
  23. // Do a quick fetchall on the results
  24. $list = $query->fetchall(PDO::FETCH_ASSOC);
  25.  
  26. // Make sure we have a result
  27. if(count($list) > 0){
  28. foreach ($list as $key => $value) {
  29. $data[] = array('id' => $value['booth'], 'text' => $value['exhibitor']);
  30. }
  31. } else {
  32. $data[] = array('id' => '0', 'text' => 'No Products Found');
  33. }
  34.  
  35. // return the result in json
  36. echo json_encode($data);
  37.  
  38. $( ".webform-component--exhibitor-information--exhibitor-select" ).select2({
  39. placeholder: "Select Exhibitor",
  40. allowClear: true,
  41. ajax: {
  42. url: "https://example.com/dbase.php",
  43. dataType: 'json',
  44. delay: 250,
  45. data: function (params) {
  46. return {
  47. q: params.term // search term
  48. };
  49. },
  50. processResults: function (data) {
  51. // parse the results into the format expected by Select2.
  52. // since we are using custom formatting functions we do not need to
  53. // alter the remote JSON data
  54. return {
  55. results: data
  56. };
  57. },
  58. cache: true
  59. },
  60.  
  61. minimumInputLength: 2
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement