-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
106 lines (100 loc) · 3.67 KB
/
index.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
62
63
64
65
66
67
68
69
70
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>phploppuharkka</title>
<meta charset="UTF-8">
<style>
.row-table {padding-left: 10px; padding-right: 10px;}
tr:hover {background-color: #42f4df;}
th {text-align: center; padding: 7px;vertical-align: middle;}
input,select {border: 3px solid #dddddd;border-radius: 7px;}
</style>
</head>
<body style="position: absolute; left: 20%; top: 20% transform: translate(-50%, -50%);">
<?php
//vaihtoehdot
$parametrit = array(
array('value'=>'', 'text'=>'---'),
array('value'=>'kategoria', 'text'=>'kategoria'),
array('value'=>'id', 'text'=>'id'),
array('value'=>'hinta', 'text'=>'hinta')
);
?>
<div>
</div><h1 style=" color: #111; font-family: 'Open Sans Condensed', sans-serif; font-size: 64px; font-weight: 700; line-height: 64px; margin: 0 0 0; padding: 20px 30px 20px 0px; text-align: center; text-transform: uppercase; ">SAIPPUAKAUPPIAS</h1></div>
<!-- formi gettimetodilla -->
<form action="index.php" method="get">
<!-- hakuparametrin valinta -->
Valitse hakutapa:<br>
<select name="parametri">
<?php foreach ($parametrit as $key => $value){?>
<option value="<?php echo $value['value'] ?>" <?php echo (!empty($_GET['parametri']) && $_GET['parametri']==$value['value']?'valittu':'') ?>><?php echo $value['text'];?></option>
<?php } ?>
</select>
<br><br>
<!-- kirjoitus fieldi -->
<br>
ID: 1-10<br>
Kategorioita: saippua, shampoo ja hammastahna<br>
<input type="text" name="strval" value="<?php echo (!empty($_GET['parametri']) && !empty($_GET['strval'])?$_GET['strval']:'') ?>"><br><br>
<input type="submit" value="Hae"><br><br>
</form>
<?php if(!$_GET){ //jos ei syöteparametria -> palauta tyhjä array
$result = array();
}else{
//hae data API:sta
//$url = "http://localhost/PHP_kurssi/phploppuharkka/tuote.php";
$url = "https://loppuharkkaphp.000webhostapp.com/tuote.php";
//tarkistetaan ettei parametri ole tyhjä
if($_GET['parametri']!=''){
$parametrit = '?' . $_GET['parametri'] . '=' . $_GET['strval'];
}else{//hae ilman syötettä
$parametrit = '';
}
$url = $url . $parametrit;
//näytä tiedot
//echo "API --> GET: " . $url . "<br/><br/>";
//taulukon ylätunnisteet
$tunnisteet = array(
'Content-Type: application/json',
'Accept: application/json'
);//juttelee jsonia
//curl API kutsu
$curl = curl_init($url);
//API paluuarvoon
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//taulukon ylätunnisteet
curl_setopt($curl, CURLOPT_HTTPHEADER, $tunnisteet);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPGET, true);
if(!($curl_response = curl_exec($curl))){
die('Virhe: "' . curl_error($curl) . '" - Virhekoodi: ' . curl_errno($curl));
}
//sulje yhteys
curl_close($curl);
//paluutulos
$result = json_decode($curl_response, true);
}?>
<table border="2" style="border: 3px solid #dddddd; border-radius: 7px; border-collapse: collapse; ">
<tr>
<th>Id</th>
<th>Kuva</th>
<th>Nimi</th>
<th>kategoria</th>
<th>hinta</th>
<th>paino</th>
</tr>
<!-- looppaa tulokset tauluun -->
<?php foreach ($result as $key => $value){?>
<tr>
<td class="row-table"><?php echo $value['id']; ?></td>
<td class="row-table"><img src="<?php echo $value['picurli']; ?>" alt="<?php echo $value['nimi']; ?>" width="50" height="50" /></td>
<td class="row-table"><?php echo $value['nimi']; ?></td>
<td class="row-table"><?php echo $value['kategoria']; ?></td>
<td class="row-table"><?php echo $value['hinta']; ?></td>
<td class="row-table"><?php echo $value['paino']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>