test-PDO-json_autocomplete.php 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?php
	require_once("initPDO.php");
	require_once("createUserTable.php");

	class User
	{
		protected $props;

		public function __get($prop) {
			return $this->props[$prop];
		}

		public function __set($prop, $val) {
			$this->props[$prop] = $val;
		}

		public static function executeRequest($req) {
			$pdo = $GLOBALS['pdo'];
			$request = $pdo->prepare($req);
			if (!$request) {
				myDump(debug_backtrace());
				die('Error while doing request ' . $sqlRequest);
			}

			$request->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
			$request->execute();

			return $request->fetchAll();
		}

		public static function getAllUsers() {
			return static::executeRequest('select * from `users`');
		}

		// instance-side method to render a User object to HTML
		public function toHtml() {
			echo "<tr>"
				. "<td>". $this->id . "</td>"
				. "<td>". $this->name . "</td>"
				. "<td>" . $this->email . "</td></tr>";
		}

		// class-side method to render an array of users as an HTML table
		public static function showUsersAsTable($users) {
			echo '<table><thead>
					<tr><th>Id</th><th>Nom</th><th>Email</th></tr></thead><tbody>';
			foreach($users as $u)
				$u->toHtml();
			echo '</tbody></table>';
		}
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Get Results
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	$allUsers = User::getAllUsers();
	$allUsersEmails = [];
	foreach($allUsers as $u)
		 $allUsersEmails[] = $u->email;

	$allUsersEmailsAsJSON = json_encode($allUsersEmails);

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Close the database connection
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   $pdo = null;
?>

<!doctype html>
70
 <html lang="fr">
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
	  var availableTags = <?php echo $allUsersEmailsAsJSON; ?>;

	  $( "#tags" ).autocomplete({
		      source: function(request, response) {
		  			var filteredArray = $.map(availableTags, function(item) {
		           if(item.indexOf(request.term) == 0)
		            	 return item;
		           else
		               return null;
				  	 });
		          response(filteredArray);
		       },
				minLength: 1
    	});

  });
  </script>
	<style>
	table {
		border-top: 1px solid black;
		border-bottom: 1px solid black;
	}

	td {
		text-align: center;
		padding-left: 2em;
		padding-right: 2em;
	}
	</style>

</head>
<body>

<h1>Users</h1>
<?php
	User::showUsersAsTable($allUsers);
?>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>