How to display form number in php [closed]

-1

I'm creating a small cafeteria management system, and I would like to know how I could do it so that each new form would display the number of the current one where the name is, so I do not have to enter the number, PHP FICO GRATO. 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><selectclass="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

    
asked by anonymous 17.01.2017 / 17:04

1 answer

1

You can use the id field of your command table to make the counter, type this:

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

Or if the ID is exposed is a problem for you (for me it would be), you can create a contador field 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. So another mechanism would be needed to ensure that each counter is unique (eg add UNIQUE to the field or use atomic counters from < a href="https://redis.io"> Redis ).

EDIT :

An example of connecting to the database 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";
    
17.01.2017 / 18:16