· 8 years ago · Apr 14, 2018, 07:28 PM
1//MYSQL
2-- Table structure for table `tools`
3CREATE TABLE IF NOT EXISTS `tools` (
4 `id` int(11) NOT NULL AUTO_INCREMENT,
5 `name` varchar(256) NOT NULL,
6 `description` varchar(256) NOT NULL,
7 `image` varchar(256) NOT NULL,
8 `manual` varchar(256) NOT NULL,
9 PRIMARY KEY (`id`)
10) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
11
12-- Dumping data for table `tools`
13INSERT INTO `tools` (`id`, `name`, `description`, `image`, `manual`) VALUES
14(1, 'Bosch - GSB 18 Li2', 'Denne kompakte og funktionelle slagboremaskine har et ergonomisk "\n" greb med vedhængt bælteclips for ekstra komfort under arbejdet.', 'img1.png','pdf2.pdf'),
15(2, 'Dewalt - PSD 18 Li2', 'En Skruemaskine fra Dewalt', 'img2.png', 'pdf2.pdf');
16
17//PHP
18<?php
19// required headers
20header("Access-Control-Allow-Origin: *");
21header("Content-Type: application/json; charset=UTF-8");
22
23// include database and object files
24include_once '../config/database.php';
25include_once '../objects/tool.php';
26
27// instantiate database and product object
28$database = new Database();
29$db = $database->getConnection();
30
31// initialize object
32$tool = new Tool($db);
33
34// query products
35$stmt = $tool->read();
36$num = $stmt->rowCount();
37
38// check if more than 0 record found
39if($num>0){
40
41 // products array
42 $tools_arr=array();
43 $tools_arr["tools"]=array();
44
45 // retrieve our table contents
46 // fetch() is faster than fetchAll()
47 // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
48 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
49 // extract row
50 // this will make $row['name'] to
51 // just $name only
52 extract($row);
53
54 $tool_item=array(
55 "id" => $id,
56 "name" => $name,
57 "description" => html_entity_decode($description),
58 "image" => $image,
59 "manual" => $manual
60 );
61
62 array_push($tools_arr["tools"], $tool_item);
63 }
64
65 echo json_encode($tools_arr);
66}
67
68else{
69 echo json_encode(
70 array("message" => "No products found.")
71 );
72}
73?>