How to display the form number in php

Asked

Viewed 409 times

-1

I am creating a small cafeteria command system , and I would like to know how to make each new form to display the number of the current where the name commands , so I do not have to enter the number and yes already comes automatic IN PHP I APPRECIATE. The source code

<html>
<head>
    <meta charset="utf-8"/>
    <title>Peixaria</title>
    <style>
        .add {
            text - decoration: none;
        }
    </style>
</head>
<body>
<style>
.add,add2{ text-decoration:none;}
#selecionados input,#selecionados2 input{ margin:10px;}
</style>
    <h1>Peixaria</h1>
    <ul class="menu cf">
        <li><a href="secao.php">Início</a></li>
        <li><a href="pedidos.php">Pedidos</a></li>
        <li><a href="reserva.php">Reserva</a></li>
        <li><a href="relatorio.php">Relatório</a></li>
    </ul>
    <main>
        <form>
            <header>
                <h2>Fazer Pedido</h2>
            </header>
            <fieldset>
                <label>
                    <span>Comanda:</span>
                    <input type="text" value="">
                </label>
                <label>
                    <span>Mesa:</span>
                    <input type="text" value="">
                </label>
                <span>Refeições/Bebidas/Sobremesas:</span>
                <div class="pedidos">

                    <select class="selecionar">
                        <option selected disabled>Selecione</option>
                        <option >Costela de Tambaqui sem Espinha</option> 
                        <option  >Lombo de Tambaqui Frito sem Espinha</option>
                        <option >Caldeirada de Tambaqui sem Espinha</option>
                        <option >Caldeirada de Tucunaré</option> 
                        <option >Peixe no Tucupi com Camarão</option>
                        <option >Escabeche de Pirarucu</option>
                        <option >Escabeche de Tambaqui</option>
                        <option >Escabeche de Tucunaré</option>
                        <option >Tucunaré Frito</option> 
                        <option >Sardinha Frita</option>
                        <option >Jaraqui Frito</option>
                        <option >Pacu Frito</option> 
                        <option >Filé de Pirarucu Frito</option>
                        <option >Filé de Pirarucu a Milanesa</option>
                        <option >Guisado de Pirarucu</option>
                    </select>
                    <a class="add" href="#">+</a>
                    <hr>
                    Selecionados
                    <hr>
                    <div class="selecionados">

                    </div>
                </div>
               <br>
                <div class="pedidos">

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <select class="selecionar">
                        <option selected disabled>Selecione</option> 
                        <option >Fanta Laranja 1l</option> 
                        <option >Fanta Laranja 2l</option> 
                        <option >Cola Cola 1l</option>
                        <option >Cola Cola 2l</option>
                        <option >Bare 2l</option> 
                        <option >Fanta Uva</option>
                        <option >Fanta Laranja</option>
                        <option >Sprit</option> 
                        <option >Cola Cola </option>
                        <option >Cola Cola zero </option>
                        <option >Guaraná Antarctica</option> 
                        <option >Guaraná Baré</option>
                        <option >Suco Goiaba</option> 
                        <option >Suco Manga</option>
                        <option >Suco Pessego</option>
                        <option >Suco Uva</option> 
                        <option >Suco Maracujá</option>
                        <option >Suco Laranja</option>
                        <option >Suco Caju</option> 
                        <option >Agua Mineral </option>
                        <option >Agua com Gas </option>
                        <option >Cerveja em Lata</option> 
                        <option >Limonada Natural</option>
                    </select>
                    <a class="add" href="#">+</a>
                    <hr>
                    Selecionados
                    <hr>
                    <div class="selecionados">

                    </div>

                </div>

            </fieldset>
        </form>
    </main>

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

        $(".add").on('click',function(){ 
            var cont=0;
            var holder = $(this).closest('.pedidos');
            holder.find(".selecionados input").each(function(){
                if($(this).val()==holder.find(".selecionar option:selected").html()){
                    cont++;
                }
            });
            if(cont>0) { 
                alert("Este item ja esta adicionado, altere a quantidade se deseja mais..");
            } else{
                holder.find(".selecionados").append(
                    "<input disabled type='text' name='pedidos[]' value='" + 
                    holder.find(".selecionar option:selected").html() + 
                    "' ><input type='text' name='quantidade[]' placeholder='quantidade'><br>"
                );
            }
        });


    </script>

</body>
</html>

the table inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • If this is recorded in the database, you can use the id of the last +1 record to set the next command.

  • Each new request is sent to the database. And when I open a new request I wanted to display the form number where it is written commands , how could I do this in php . would have to put as reply @Juniornunes ?

  • This order numbering is daily or always incremental to infinity?

  • is incremental to infinity , type every time I open a request it opens together , as a control even , only it goes pro mysql. how could I display her ?

  • Where is the script that generates the command in the database when a new request is made? Which database is being used? What is the structure of your table? This information we cannot guess.

1 answer

1


You can use the field id from your command table to do the counter, like this:

-- Retorna o último id.
SELECT c.id 
FROM comanda as c
ORDER BY c.id DESC
LIMIT 1;

Or if the exposed ID is a problem for you (for me it would be), you can create a field contador in your table (preferably indexed):

-- Retorna o último contador.
SELECT c.contador
FROM comanda as c
ORDER BY c.contador DESC
LIMIT 1;

In PHP just add 1 to the result of one of the previous SQL you will have the next number.

PS: It is worth remembering that if two users open the screen at the same time the "counter technique" can repeat the number. This would require another mechanism to ensure that each counter is unique (example: add index UNIQUE in the field or use atomic counters of Redis).

EDIT:

An example of database connection using mysqli would be:

// Conexão fictícia
$db_config = [
    'host' => 'localhost',
    'user' => 'user',
    'password' => 'my_password',
    'database' => 'my_database',
    'port' => 3306,
];

// Conecta com o BD
$mysqli = new mysqli($db_config['host'], $db_config['user'], $db_config['password'], $db_config['database'], $db_config['port']);

// Em caso de erro encerra o script
if ($mysqli->connect_errno) {
    echo "Erro ao conectar com o banco de dados: " . $mysqli->connect_error;
    exit();
}

$query = <<<SQL
    SELECT c.id as ultimo_id
    FROM comanda as c
    ORDER BY c.id DESC
    LIMIT 1;
SQL;

// Executa a query
$result = $mysqli->query($query);


// Se $result !== FALSE, não ocorreu erro com a query
// Se $result->num_rows !== 0, retornou pelo menos 1 registro do BD
if ($result && $result->num_rows) {
    /*
        Transforma o resultado da query em um objeto. Nesse caso seria o equivalente a:
        $resultado_da_query = (object) ['ultimo_id' => 1234]
    */
    $ultimo_id = $res->fetch_object()->ultimo_id
} else {
    echo "Erro ao executar query.";
    exit();
}

echo "A próxima comanda é $ultimo_id";
  • Very good, an idea for the counter, would be to do the count again after the submission of the form to be able to save, and not use the data of the view, since it could be easily changed!

  • 1

    I use in a system a more complex counter, but in my model I have a function ultimo_codigo(). So I show you what the likely code that the model will have and before saving I do again so that the code is actually the last one. Remembering that this field is UNIQUE, so I guarantee the integrity of DB

  • How would it look in php to display an echo on the screen ? @fernandosavio

  • Are you using any framework? Just run the above query and add + 1 to the result. How is an integer echo would work normally.

  • @fernandosavio I’m not using any framework , as I could do ?

  • I changed the answer and added an example using mysqli.

Show 1 more comment

Browser other questions tagged

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