-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_papers.php
61 lines (48 loc) · 1.25 KB
/
get_papers.php
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
<?php
require_once("configuration.php");
header('Content-Type: application/json');
$paperAmount = 8;
if (!isset($_SESSION["LOGIN_USER"]))
{
header('HTTP/1.1 401 Unauthorized');
echo(json_encode(array('error' => 'authentication-needed')));
exit();
}
if (isset($_GET["amount"]))
{
if (!preg_match('/^\d+$/',$_GET["amount"]))
{
header('HTTP/1.0 400 Bad Request');
echo(json_encode(array('error' => 'bad-request')));
exit();
}
else
{
if (intval($_GET["amount"]) > 100)
{
$paperAmount = 100;
}
else
{
$paperAmount = intval($_GET["amount"]);
}
}
}
$randomPaperArray = array();
/*class RandomPaper
{
private static $paperId;
private static $paperBrand;
private static $paperTitle;
}*/
$dataquery = "SELECT paperBrand, paperID, paperTitle FROM papers GROUP BY paperBrand ORDER BY rand() LIMIT $paperAmount";
$dataresult = $dataconnection->query($dataquery);
if ($dataresult->num_rows > 0)
{
while($randomPapers = $dataresult->fetch_assoc())
{
array_push($randomPaperArray, array("paperBrand" => $randomPapers["paperBrand"], "paperId" => $randomPapers["paperID"], "paperTitle" => $randomPapers["paperTitle"]));
}
}
echo json_encode($randomPaperArray, JSON_PRETTY_PRINT);
?>