Logic and Checkout logic help

0

I am doing an accreditation system with checkin and checkout . Everything is working, but I just can not do the following: When I do the checkin , it registers; When I do it again, it registers again. I need him to sweep this register saying that he is already "inside the fair"; only when you go through the checkout it can re-enter.

<?php
require_once('../controller/checkinsController.php');
salvarCheckin();
?>

<?php
require_once ('../config.php');
require_once ("../inc/visibilidade-header.php");
?>
<script>
    jQuery(function($){
        $("#campoCPF").mask("999.999.999-99");
    });
</script>
<?php if ($_REQUEST['posicao']=='checkin'):
        $posicao = '1';         
?>
<h2>Check-in</h2>
<?php endif; ?>
<?php if ($_REQUEST['posicao']=='checkout'):
        $posicao = '0';         
?>
<h2>Check-out</h2>
<?php endif; ?>

<form id="formCheckin" action="add.php?posicao_atual=<?php echo $posicao; ?>" method="post">
    <!-- area de campos do form -->
    <hr />
    <div class="row">
        <div class="form-group col-md-2">
            <label for="name">CODIGO DE BARRA</label>
            <input type="text" name="codigo_barra" onfocus="if(this.value.length==10){submeter()}" maxlength="10" onchange="if(this.value.length==10){submeter()}"  onkeyup="if(this.value.length==10){submeter()}" onblur="if(this.value.length==10){submeter()}" id="codigo_barra" />

        </div>

    </div>
    <div id="actions" class="row">
        <div class="col-md-12">
            <button type="submit" class="btn btn-success" >Estou aqui!</button>
            <a href="../index.php" class="btn btn-default">Voltar</a>
        </div>
    </div>
</form>

<?php include(FOOTER_TEMPLATE); ?>

This is my Controller :

<?php
/**
 * Created by PhpStorm.
 * User: andre.luis.a.costa
 * Date: 03/12/2016
 * Time: 16:07
 */

require_once('../config.php');
require_once(DBAPI);

$checkin = null;

/**
 *
 */
function salvarCheckin() {


    $db = open_database();

    if (!empty($_POST['codigo_barra'])) {
        $posicao =  $_REQUEST['posicao_atual'];

        $today = date_create('now', new DateTimeZone('America/Recife'));

        $checkin["codigo_barra"] = $_POST['codigo_barra'];
        $checkin["posicao"] = $posicao;
        $checkin['created'] = $today->format("Y-m-d H:i:s");
        $checkin['modified'] = $checkin['created'];

         $retorno = ultimaPosicaoVisitante($checkin['codigo_barra'], $db, $posicao);

        $retorno = validarCadastroCodigoVisitante($checkin['codigo_barra'], $db);

        if($retorno){
            save('checkins', $checkin);
            echo "<script>alert(".json_encode($_SESSION['message']).")</script>";
        } else{
            $_SESSION['message'] = "Visitante não cadastrado! Favor realizar o cadastro!";
            echo "<script>alert(".json_encode($_SESSION['message']).")</script>";

        }

    } 
}

function validarCadastroCodigoVisitante($codigo_barra, $db) {
    $existeVisitante = false;

    $sql = "SELECT * FROM visitantes WHERE codigo_barra = '$codigo_barra' ";

    $search = mysqli_query($db, $sql);
        //mysqli_result['num_rows']
        // var_dump($search);
    if (isset($search)) {
        $row = mysqli_fetch_array($search,MYSQLI_ASSOC);
    }

    if(isset($row) > 0){
        $existeVisitante = true;
    }

    return $existeVisitante;

}
function ultimaPosicaoVisitante($codigo_barra, $db, $posicao) {
    $posicaoVisitante = false;

    $sql = "SELECT * FROM checkins WHERE codigo_barra = '$codigo_barra' LIMIT 1 ORDER BY id DESC  ";

    $search = mysqli_query($db, $sql);
        //mysqli_result['num_rows']
        // var_dump($search);
    if (isset($search)) {
        $row = mysqli_fetch_array($search,MYSQLI_ASSOC);
    }

    if(isset($row['posicao']) == "$posicao"){
        $posicaoVisitante = true;
    }

    return $posicaoVisitante;

}

?>
    
asked by anonymous 22.05.2017 / 16:42

1 answer

0

Trying to help you there, as you requested in the comments, but I'm honestly trying to figure out what the difficulty is.

function salvarCheckin() {


    $db = open_database();

    if (!empty($_POST['codigo_barra'])) {
        $posicao =  $_REQUEST['posicao_atual'];

        $today = date_create('now', new DateTimeZone('America/Recife'));

        $checkin["codigo_barra"] = $_POST['codigo_barra'];
        $checkin["posicao"] = $posicao;
        $checkin['created'] = $today->format("Y-m-d H:i:s");
        $checkin['modified'] = $checkin['created'];

         $retorno = ultimaPosicaoVisitante($checkin['codigo_barra'], $db, $posicao);

        $retorno = validarCadastroCodigoVisitante($checkin['codigo_barra'], $db);

        if($retorno){
            if (verificarSeCheckinJaExiste($checkin['codigo_barra'], $db)) {
               $_SESSION['message'] = "Visitante já fez checkin!";
               echo "<script>alert(".json_encode($_SESSION['message']).")</script>";
               return false;
            }
            save('checkins', $checkin);
            echo "<script>alert(".json_encode($_SESSION['message']).")</script>";
        } else{
            $_SESSION['message'] = "Visitante não cadastrado! Favor realizar o cadastro!";
            echo "<script>alert(".json_encode($_SESSION['message']).")</script>";

        }

    } 
}

function verificarSeCheckinJaExiste($codigo_barra, $db) {

    $sql = "SELECT * FROM checkins WHERE codigo_barra = '$codigo_barra'";
    $sql = mysqli_real_escape_string($db, $sql);
    $search = mysqli_query($db, $sql);
    $rows = mysqli_num_rows($search)
    if ($rows > 0) {
        return true;
    }
    return false;
}

As you can see, I had to use mysqli_num_rows to count how many rows came in the result, follows the documentation: link

    
22.05.2017 / 21:34