· 7 years ago · Mar 04, 2019, 09:22 AM
1CREATE DATABASE IF NOT EXISTS php_tests;
2USE php_tests;
3CREATE TABLE IF NOT EXISTS tasks(
4 ID INT(11) AUTO_INCREMENT NOT NULL,
5 Title VARCHAR(100) NOT NULL,
6 Description VARCHAR(255) NOT NULL,
7 CONSTRAINT pk_tasks PRIMARY KEY(ID)
8);
9
10<?php
11
12$connection = mysqli_connect(
13 'localhost',
14 'root',
15 'admin',
16 'php_tests'
17);
18
19<?php
20
21require_once 'connection.php';
22
23$query = "SELECT * FROM tasks";
24$result = mysqli_query($connection, $query);
25
26if (!$result) {
27 die('Query Failed' . mysqli_error($connection));
28}
29
30$json = array();
31
32while ($row = mysqli_fetch_array($result)) {
33 /* $json = array($row['ID'], $row['Title'], $row['Description']); */
34
35 $json = array(
36 'ID'=>$row['ID'],
37 'Title'=>$row['Title'],
38 'Description'=>$row['Description']
39 );
40
41 /* var_dump($json); */
42
43 $jsonstring = json_encode($json);
44 echo $jsonstring;
45}
46
47$.ajax({
48 url: 'task_list.php',
49 type: 'GET',
50 success: (response) => {
51 let tasks = JSON.parse(response);
52 let template = '';
53
54 tasks.forEach(task => {
55 template +=
56 `
57 <tr>
58 <td>${task.ID}</td>
59 <td>${task.Title}</td>
60 <td>${task.Description}</td>
61 </tr>
62 `;
63 });
64 $('#Tasks').html(template);
65 }
66});