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);
}
function pageController($dbc)
{
    var_dump($_POST);
    $error = '';
    $errors = NULL;
    if (!empty($_POST)) {
        if (checkValues()) {
            $errors = insertPark($dbc);
        }
    }
    return array('errors' => $errors);
}
Esempio n. 3
0
function pageVariables($connection)
{
    $in_name = new PInput('in_name');
    $in_location = new PInput('in_location');
    $in_date_est = new PInput('in_date_est', true);
    $in_area = new PInput('in_area', false, true);
    $in_description = new PInput('in_description');
    $exMessage = ['name' => $in_name->exMessage, 'location' => $in_location->exMessage, 'date_established' => $in_date_est->exMessage, 'area' => $in_area->exMessage, 'description' => $in_description->exMessage];
    $edit_parkdb = new PInput('edit_parkdb');
    $pass = ['in_name' => $in_name->value, 'in_location' => $in_location->value, 'in_date_est' => $in_date_est->value, 'in_area' => $in_area->value, 'in_description' => $in_description->value, 'exMessage' => $exMessage, 'edit_parkdb' => $edit_parkdb->isSet];
    // var_dump($in_date_est);
    var_dump($pass);
    if ($in_name->isSet && $in_location->isSet && $in_date_est->isSet && $in_area->isSet && $in_description->isSet) {
        insertPark($pass, $connection);
        $pass['in_name'] = '';
        $pass['in_location'] = '';
        $pass['in_date_est'] = '';
        $pass['in_area'] = '';
        $pass['in_description'] = '';
    }
    return $pass;
}
Esempio n. 4
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);
}
    try {
        $delete_park = Input::getNumber('delete_park');
    } 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');
        echo $error;
        print_r($errors);
    } else {
        $insert = "INSERT INTO national_parks (name, location, date_established, area_in_acres, description)\n\tVALUES (:name, :location, :date_established, :area_in_acres, :description)";
        $stmt = $dbc->prepare($insert);
        $stmt->bindValue(':name', $name, PDO::PARAM_STR);
        $stmt->bindValue(':location', $location, PDO::PARAM_STR);
        $stmt->bindValue(':date_established', $d, PDO::PARAM_STR);
        $stmt->bindValue(':area_in_acres', $area_in_acres, PDO::PARAM_STR);
        $stmt->bindValue(':description', $description, PDO::PARAM_STR);
        $stmt->execute();
    }
}
if (!empty($_POST)) {
    if (Input::noInput('name') && Input::noInput('location') && Input::noInput('date_established') && Input::noInput('area_in_acres') && Input::noInput('description')) {
        insertPark($dbc);
    } else {
        if (Input::noInput('deleted_name')) {
            $delete = "DELETE FROM national_parks WHERE name = :deleted_name";
            $stmt = $dbc->prepare($delete);
            $stmt->bindValue(':deleted_name', $_POST['deleted_name'], PDO::PARAM_STR);
            $stmt->execute();
        } else {
            echo "Please make a valid entry.";
        }
    }
}
// LIMIT AND OFFSET
$page = Input::has('page') ? Input::get('page') : 1;
$limit = 4;
$offset = $page * $limit - $limit;