Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Juhanson - tervikveeb taimevaatluse põhjal #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions taimevaatlusmärkmik/class/Event.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
class Event
{

private $connection;

function __construct($mysqli)
{
$this->connection = $mysqli;
}


function saveEvent($observer, $plant_species, $obs_spot) {

$stmt = $this->connection->prepare("INSERT INTO plantlife (observer, plant_species, obs_spot) VALUES (?, ?, ?)");
echo $this->connection->error;

$stmt->bind_param("sss", $observer, $plant_species, $obs_spot);
header("Location: data.php");

if ($stmt->execute() ) {
echo "Saved!";
} else {
echo "ERROR " . $stmt->error;
}

}


function getAllObservations($q, $sort, $order) {

$allowedSort = ["observer", "plant_species", "obs_spot"];

// sort ei kuulu lubatud tulpade sisse
if(!in_array($sort, $allowedSort)){
$sort = "observer";
}

$orderBy = "ASC";

if($order == "DESC") {
$orderBy = "DESC";
}

echo "Sorting: ".$sort." ".$orderBy." ";


if ($q != "") {
//otsin
echo "Searching: ".$q;

$stmt = $this->connection->prepare("
SELECT id, observer, plant_species, obs_spot
FROM plantlife
WHERE (observer LIKE ? OR plant_species LIKE ? OR obs_spot LIKE ?)
ORDER BY $sort $orderBy
");

$searchWord = "%".$q."%";

$stmt->bind_param("sss", $searchWord, $searchWord, $searchWord);

} else {
//ei otsi
$stmt = $this->connection->prepare("
SELECT id, observer, plant_species, obs_spot
FROM plantlife
ORDER BY $sort $orderBy
");
}

$stmt->bind_result($id, $observer, $plant_species, $obs_spot);
$stmt->execute();

$results = array();

// ts�kli sisu tehakse nii mitu korda, mitu rida
// SQL lausega tuleb
while ($stmt->fetch()) {

$people = new StdClass();
$people->id = $id;
$people->observer= $observer;
$people->plant_species = $plant_species;
$people->obs_spot = $obs_spot;

//echo $color."<br>";
array_push($results, $people);

}

return $results;
}

function observerList() {

$stmt = $this->connection->prepare("
SELECT observer,
COUNT(*) AS plant_count FROM plantlife
GROUP BY observer
");

//SELECT observer, COUNT(*) AS plant_count FROM plantlife GROUP BY observer;

$stmt->bind_result($observer, $plant_count);
$stmt->execute();

$results = array();

// ts�kli sisu tehakse nii mitu korda, mitu rida
// SQL lausega tuleb
while ($stmt->fetch()) {

$obs = new StdClass();
$obs->observer= $observer;
$obs->plant_count = $plant_count;

//echo $color."<br>";
array_push($results, $obs);

}

return $results;
}
}
?>
3 changes: 3 additions & 0 deletions taimevaatlusmärkmik/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FOOTER
</body>
</html>
13 changes: 13 additions & 0 deletions taimevaatlusmärkmik/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require("/home/joosjuha/config.php");

// see fail peab olema siis seotud k�igiga kusb
// tahame sessiooni kasutada
// saab kasutada n��d $_SESSION muutujat
session_start();

$database = "if16_jsander";
$mysqli = new mysqli($serverHost, $serverUsername, $serverPassword, $database);

?>
20 changes: 20 additions & 0 deletions taimevaatlusmärkmik/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>



<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
HEADER
174 changes: 174 additions & 0 deletions taimevaatlusmärkmik/page/data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

//�hendan sessiooniga
require("../functions.php");

require("../class/Event.class.php");
$Event = new Event($mysqli);


/*{
echo "Saved.";
saveEvent(($_POST["observer"]),($_POST["plant_species"]), ($_POST["obs_spot"]));
}*/

//Save on error and incomplete form errors
$observer = "";
$plant_species = "";
$obs_spot = "";

$emptyObserver = "*";
$emptySpecies = "*";
$emptyObs = "*";


if ( isset($_POST["observer"]) &&
isset($_POST["plant_species"]) &&
isset($_POST["obs_spot"]) &&
!empty($_POST["observer"]) &&
!empty($_POST["plant_species"]) &&
!empty($_POST["obs_spot"])
) {
$Event->saveEvent(($_POST["observer"]),($_POST["plant_species"]), ($_POST["obs_spot"]));
}


// otsib
if (isset($_GET["q"])) {

$q = $_GET["q"];

} else {
//ei otsi
$q = "";
}

//vaikimisi, kui keegi mingit linki ei vajuta
$sort = "id";
$order = "ASC";

if (isset($_GET["sort"]) && isset($_GET["order"])) {
$sort = $_GET["sort"];
$order = $_GET["order"];
}

$people = $Event->getAllObservations($q, $sort, $order);



?>

<?php require("../header.php"); ?>


<h2>Save an observation</h2>
<form method="POST" >

<label>Observer</label><br>
<input name="observer" type="text"
<br>

<br><br>
<label>Plant species</label><br>
<input name="plant_species" type="text"
<br>

<br><br>
<label>Observed at</label><br>
<input name="obs_spot" type="text"

<br><br>

<input type="submit" value="Save">

</form>

<h3><a href="observers.php">Observer list</a></h3>

<h2>Observations</h2>

<form>
<input type="search" name="q" value="<?=$q;?>">
<input type="submit" value="Search">
</form>

<?php


$html = "<table class='table table-striped table-condensed'>";

$html .= "<tr>";
$html .= "<th>ID</th>";

$orderObserver = "ASC";
if (isset($_GET["order"]) &&
$_GET["order"] == "ASC" &&
$_GET["sort"] == "observer" ) {

$orderObserver = "DESC";
}

$html .= "<th>
<a href='?q=".$q."&sort=observer&order=".$orderObserver."'>
Observer
</a>
</th>";


$orderPlant = "ASC";
if (isset($_GET["order"]) &&
$_GET["order"] == "ASC" &&
$_GET["sort"] == "plant_species" ) {

$orderPlant = "DESC";
}

$html .= "<th>
<a href='?q=".$q."&sort=plant_species&order=".$orderPlant."'>
Plant species
</a>
</th>";


$orderObs = "ASC";
if (isset($_GET["order"]) &&
$_GET["order"] == "ASC" &&
$_GET["sort"] == "obs_spot" ) {

$orderObs = "DESC";
}

$html .= "<th>
<a href='?q=".$q."&sort=obs_spot&order=".$orderObs."'>
Observed at
</a>
</th>";


$html .= "</tr>";

//iga liikme kohta massiivis
foreach ($people as $p) {

$html .= "<tr>";
$html .= "<td>".$p->id."</td>";
$html .= "<td>".$p->observer."</td>";
$html .= "<td>".$p->plant_species."</td>";
$html .= "<td>".$p->obs_spot."</td>";
$html .= "<td>
<a class='btn' href='data.php?q=".$p->observer."'>
Select Observer
<span class='glyphicon glyphicon-user'></span>
</a>
</td>";
$html .= "</tr>";

} // <h3><a href="observers.php">Observer list</a></h3>

$html .= "</table>";

echo $html;

?>

<?php require("../footer.php"); ?>
44 changes: 44 additions & 0 deletions taimevaatlusmärkmik/page/observers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require("../functions.php");

require("../class/Event.class.php");
$Event = new Event($mysqli);

$obs = $Event->observerList();

?>

<?php require("../header.php"); ?>

<h5><a href="data.php"> Back</a></h5>

<h2>Observers</h2>

<?php


$html = "<table class='table table-striped table-condensed'>";

$html .= "<tr>";
$html .= "<th>Observer</th>";
$html .= "<th>Plant species observed</th>";
$html .= "</tr>";

//iga liikme kohta massiivis
foreach ($obs as $o) {

$html .= "<tr>";
$html .= "<td>".$o->observer."</td>";
$html .= "<td>".$o->plant_count."</td>";
$html .= "</tr>";

}

$html .= "</table>";

echo $html;

?>

<?php require("../footer.php"); ?>
3 changes: 3 additions & 0 deletions taimevaatlusmärkmik/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plantlife

id | observer | plant_species | obs_spot