Help with Checkin and Checkout logic

Asked

Viewed 164 times

0

I’m doing a system of accreditation with checkin and checkout. It’s all working, but I just can’t do the following: When I do the checkin, he registers; when I do it again, he registers again. I need him to file this record saying he’s already "inside the fair"; only when he goes through checkout he may enter again.

<?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;

}

?>
  • That function save($checkin) is included in a database checkin, correct? The bid would be to check, before saving, if the codigo_barra already exists in this table, no? Or it can do checkin and checkout several times?

  • Exactly friend @Daniel is coming from a DB, the barcode is coming from the DB too, I’m not getting exactly this, that the same codigo_barra Ex: 05552225 gave 'Checkin' --- if I go to pass this again appears the error message, only releasing if you pass the 'Checkout'

  • That line: $retorno = validarCadastroCodigoVisitante($checkin['codigo_barra'], $db);&#xA; is returning true or false, right? You need to do something similar, with what you did in validarCadastroCodigoVisitante before saving (or inside the save function). Make a SELECT and see if there’s anyone with that barcode in the checkin register.

  • like exactly, that’s where the problem is, I’m not finding the way to do it, it could be clearer, I’m beginner with it :P @Daniel

  • Ué, you did it dude: you checked if the barcode already exists in the visitors table. Just do the same thing on the checkin table. It would be something like SELECT * FROM checkins WHERE codigo_barra = '$codigo_barra' and checks if the return came empty: count($checkins).

  • Nothing, I created Select in Salvarchekin and it made no difference! I think that I fell all over myself... because he’s constantly creating, he’s not stopping at what he’s already created! Can’t you help me by showing me the code above?? help me friend! Please! Let me be clearer!!! ! a number EX: 12345678 presses the Checkin it records there it gets like 1, if again the 12345678 press on the Checkin it’s going to creating again with the 0 instead of locking his passage!!! !

  • Does it look like 1? Where? Dude, put the code of this function save() if it doesn’t make it hard to help.

  • that comment was before you sent, I’m testing aqi ja 1 Mint qe ja te te te me funfou! : D

Show 3 more comments

1 answer

0


Trying to help you there, as you asked in the comments, but honestly I’m 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 lines came in the result, follow documentation: http://php.net/results.php?q=mysqli_num_rows&l=pt_BR&p=all

  • I’m going to test that friend...!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.