function pageController($dbc)
{
    var_dump($_POST);
    $error = '';
    $errors = NULL;
    $limit = 2;
    $pageNumber = Input::has('pageNumber') ? Input::get('pageNumber') : 1;
    $pageNumber = $pageNumber > 0 ? $pageNumber : 1;
    $pageNumber = is_numeric($pageNumber) ? $pageNumber : 1;
    $offset = $limit * $pageNumber - $limit;
    if (!empty($_POST)) {
        if (checkValues()) {
            $errors = insertPark($dbc);
        }
    }
    deletePark($dbc);
    $stmt = $dbc->prepare("SELECT * FROM national_parks LIMIT :limit OFFSET :offset");
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
    $stmt->execute();
    $parks = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $count = $dbc->query('SELECT COUNT(*) FROM national_parks;')->fetchColumn();
    $maxPage = ceil($count / $limit);
    $next = $pageNumber + 1;
    $previous = $pageNumber - 1;
    if ($pageNumber > $maxPage) {
        $pageNumber = 1;
        header("Location: national_parks.php?pageNumber=1");
        die;
    }
    return array('pageNumber' => $pageNumber, 'previous' => $previous, 'next' => $next, 'parks' => $parks, 'maxPage' => $maxPage, 'error' => $error, 'errors' => $errors);
}
示例#2
0
function pageController($dbc)
{
    $errors = null;
    if (!empty($_POST)) {
        if (checkValues()) {
            $errors = insertPark($dbc);
        } else {
            $message = "Invalid format. Please try again.";
            $javascript = "<script type='text/javascript'>alert('{$message}');</script>";
            echo $javascript;
        }
    }
    deletePark($dbc);
    // Count
    $countAll = 'SELECT count(*) FROM national_parks';
    $count_stmt = $dbc->query($countAll);
    $count = $count_stmt->fetchColumn();
    $limit = 2;
    $max_page = ceil($count / $limit);
    // Sanitizing
    $page = Input::has('page') ? Input::get('page') : 1;
    // grabs url value if exists, if not set to 1
    $page = is_numeric($page) ? $page : 1;
    // is value numeric, if not set to 1
    $page = $page > 0 ? $page : 1;
    // is value greater than zero, if not set to 1
    $page = $page <= $max_page ? $page : $max_page;
    // is value less than or equal maximum amount of pages, if not set to max page
    // Offset
    $offset = $page * $limit - $limit;
    $selectAll = 'SELECT * FROM national_parks LIMIT :limit OFFSET :offset';
    $stmt = $dbc->prepare($selectAll);
    $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    $parks = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return array('page' => $page, 'parks' => $parks, 'errors' => $errors, 'max_page' => $max_page);
}
function pageController($dbc)
{
    $errors = null;
    if (!empty($_POST)) {
        if (checkValues()) {
            $errors = insertPark($dbc);
        } else {
            $message = "Invalid format. Do over!";
            $javascript = "<script type='text/javascript'>alert('{$message}');<?script>";
            echo $javascript;
        }
    }
    deletePark($dbc);
    // Count
    $countAll = 'SELECT count(*) FROM national_parks';
    $count_stmt = $dbc->query($countAll);
    $count = $count_stmt->fetchColumn();
    $limit = 2;
    $max_page = ceil($count / $limit);
    // Sanitizing
    $page = Input::has('page') ? Input::get('page') : 1;
    $page = $page > 1 ? $page : 1;
    // Is value greater than 0 if not set to 1.
    $page = is_numeric($page) ? $page : 1;
    // If input not numeric then return 1.
    $page = $page <= $max_page ? $page : $max_page;
    // Offset
    $offset = $page * $limit - $limit;
    $selectAll = "SELECT * FROM national_parks LIMIT {$limit} OFFSET {$offset}";
    $stmt = $dbc->query($selectAll);
    $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    $parks = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return array('page' => $page, 'parks' => $parks, 'errors' => $errors, 'max_page' => $max_page, 'limit' => $limit);
}
    } catch (Exception $e) {
        $error = $e->getMessage();
        array_push($errorsArray, $error);
    }
    $query = "DELETE FROM national_parks WHERE id = :delete_park";
    $query = $dbc->prepare($query);
    $query->bindValue(':delete_park', $delete_park, PDO::PARAM_INT);
    $query->execute();
    return $errorsArray;
}
var_dump($_POST);
if (Input::notEmpty('name') && Input::notEmpty('location') && Input::notEmpty('date_established') && Input::notEmpty('area') && Input::notEmpty('visitors') && Input::notEmpty('description')) {
    var_dump($errorsArray);
    $errorsArray = insertPark($dbc, $parks);
} elseif (Input::notEmpty('delete_park')) {
    $errorsArray = deletePark($dbc);
}
var_dump($errorsArray);
var_dump($parks);
?>

<script type="text/javascript">
$(document).ready(function() {
	"use strict";

	$(".deletePark").click(function(a) {
		var parkName = $(this).data('name');
		var parkId = $(this).data('id');

		if(confirm("Are you sure you want to delete "+parkName+"?")){
			$("#delete-id").val(parkId);
示例#5
0
        $add_basket = $_POST["basket_number"];
    }
    if ($add_name_error == "" && $add_basket_error == "") {
        $mysqli = new mysqli($GLOBALS["servername"], $GLOBALS["server_username"], $GLOBALS["server_password"], $GLOBALS["database"]);
        $stmt = $mysqli->prepare("INSERT INTO parks_php (park_name, nr_of_baskets) VALUES (?, ?)");
        $stmt->bind_param("si", $add_name, $add_basket);
        $stmt->execute();
        echo "Pargi lisamine õnnestus!";
        $stmt->close();
        $mysqli->close();
    }
}
$park_list = getParkData();
//pargi kustutamiseks
if (isset($_GET["delete"])) {
    deletePark($_GET["delete"]);
}
//PARide sisestamiseks
if (isset($_POST["pars"])) {
    $park_id = $_GET["id"];
    $nr_of_baskets = $_GET["nr"];
    insertPars($park_id, $nr_of_baskets);
    header("Location: insert_pars.php");
}
?>



<h2>Please, insert new disc golf park</h2>
<table class="center" border= 1>
	<tr>
function pageController()
{
    // Login information for db_connect.php.
    require '../park_login.php';
    // How we call to connect to the db via an outside file.
    require '../db_connect.php';
    // Calling file of functions for Input aka $_GET.
    require '../Input.php';
    // "&& is_numeric" prevents letter inputs into browser query string.
    // "round(abs())" prevents decimals and negative numbers as input to browser query string.
    $page = Input::has('page') && is_numeric(Input::get('page')) ? round(abs(Input::get('page'))) : 1;
    // Prevents page numbers less than 1. 0 causes problems by making a negative $offset.
    if ($page < 1) {
        $page = 1;
    }
    // Prevents changes to browser query string for limit value. 'newlimit' is tied to the Change Rows button.
    $limit = Input::has('newlimit') && is_numeric(Input::get('newlimit')) ? abs(intval(Input::get('newlimit'))) : 3;
    $offset = $page * $limit - $limit;
    // Gets the total number of rows of data.
    function getNumRows($dbc)
    {
        $stmt = $dbc->prepare('SELECT * FROM national_parks');
        $stmt->execute();
        $rowTotal = $stmt->rowCount();
        return $rowTotal;
    }
    $rowTotal = getNumRows($dbc);
    // Dividing $rowTotal by $limit gives us the number of pages to hold the data.
    $numOfPages = ceil($rowTotal / $limit);
    // Prevents page numbers more than the total number of pages.
    if ($page > $numOfPages) {
        $page = $numOfPages;
        $offset = $page * $limit - $limit;
    }
    // Uses variables of $limit and $offset to run the SELECT query in a scalable way.
    function getAllParks($dbc, $limit, $offset)
    {
        $stmt = $dbc->prepare('SELECT * FROM national_parks ORDER BY name LIMIT :limit OFFSET :offset');
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
        $stmt->execute();
        $parksArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $parksArray;
    }
    function getAllAllParks($dbc)
    {
        $stmt = $dbc->prepare('SELECT * FROM national_parks ORDER BY name');
        $stmt->execute();
        $parksAllArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $parksAllArray;
    }
    $parksArray = getAllParks($dbc, $limit, $offset);
    $parksAllArray = getAllAllParks($dbc);
    // Uses the 'Submit A National Park' form to insert new values to the table and database.
    function insertParks($dbc)
    {
        // Now calls on the Input class's getString and getDate methods with try catches.
        // Try catch create an array of errors for passing to the user in the HTML.
        $errorArray = [];
        try {
            $name = Input::getString('name', 0, 50);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errName'] = $error;
        }
        try {
            $location = Input::getString('location', 0, 50);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errLoc'] = $error;
        }
        try {
            $date_established = Input::getDate('date_established', '1776-07-04', 'next month');
            $date_established = $date_established->format('Y-m-d');
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errDate'] = $error;
        }
        try {
            $area_in_acres = Input::getNumber('area_in_acres', 0, 375000000);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errArea'] = $error;
        }
        try {
            $description = Input::getString('description', 0, 500);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errDes'] = $error;
        }
        // If the $errorArray is not empty, this will return out of the method before binding values and executing below. The $errorArray returns with an array of strings.
        if (!empty($errorArray)) {
            return $errorArray;
        }
        $stmt = $dbc->prepare('INSERT INTO national_parks (name, location, date_established, area_in_acres, description) VALUES (:name, :location, :date_established, :area_in_acres, :description)');
        $stmt->bindValue(':name', $name, PDO::PARAM_STR);
        $stmt->bindValue(':location', $location, PDO::PARAM_STR);
        $stmt->bindValue(':date_established', $date_established, PDO::PARAM_STR);
        $stmt->bindValue(':area_in_acres', $area_in_acres, PDO::PARAM_STR);
        $stmt->bindValue(':description', $description, PDO::PARAM_STR);
        $stmt->execute();
    }
    // Uses the 'Delete A Park' form to delete a row of data from the table and database.
    function deletePark($dbc)
    {
        $park_to_delete = Input::get('park_to_delete');
        $stmt = $dbc->prepare('DELETE FROM national_parks WHERE id = :park_to_delete');
        $stmt->bindValue(':park_to_delete', $park_to_delete, PDO::PARAM_INT);
        $stmt->execute();
    }
    // Logic that checks for $_POST values and empty string before running the functions to insert or delete.
    // Additionally, saves a different $noteToUser variable and $errorArray to show the user in the HTML.
    $noteToUser = '';
    $errorArray = [''];
    $formName = '';
    $formLoc = '';
    $formDate = '';
    $formArea = '';
    $formDes = '';
    if (!empty($_POST)) {
        if (Input::notEmpty('name') && Input::notEmpty('location') && Input::notEmpty('date_established') && Input::notEmpty('area_in_acres') && Input::notEmpty('description')) {
            // If insertsParks() throws exceptions, it returns an array of strings. If no exceptions thrown, null.
            $errorArray = insertParks($dbc);
            $parksArray = getAllParks($dbc, $limit, $offset);
            $parksAllArray = getAllAllParks($dbc);
            $rowTotal = getNumRows($dbc);
            // This if checks the $errorArray, if empty insertParks() did not throw exception and it worked.
            if ($errorArray == []) {
                $noteToUser = '******';
                $errorArray = [''];
            } else {
                $formName = Input::get('name');
                $formLoc = Input::get('location');
                $formDate = Input::get('date_established');
                $formArea = Input::get('area_in_acres');
                $formDes = Input::get('description');
            }
        } elseif (Input::notEmpty('park_to_delete')) {
            deletePark($dbc);
            $parksArray = getAllParks($dbc, $limit, $offset);
            $parksAllArray = getAllAllParks($dbc);
            $rowTotal = getNumRows($dbc);
            $noteToUser = '******';
        } else {
            $noteToUser = '******';
        }
    }
    return array('parksArray' => $parksArray, 'parksAllArray' => $parksAllArray, 'page' => $page, 'limit' => $limit, 'offset' => $offset, 'rowTotal' => $rowTotal, 'numOfPages' => $numOfPages, 'noteToUser' => $noteToUser, 'errorArray' => $errorArray, 'formName' => $formName, 'formLoc' => $formLoc, 'formDate' => $formDate, 'formArea' => $formArea, 'formDes' => $formDes);
}