1

I'm trying to make a receipt printing website. I want to be able to increment the receipt number each type a new receipt gets printed, for example, first time ever printing, the number will be 1, second time 2, etc. The numbers should not repeat automatically, because doing this manually wouldn't be good since we tend to forget the last number we were on. I have been looking on the Internet for solutions but I have not found any. Please may I have some help? here is what my receipt looks like: enter image description here in the place that says "recu n 2839" I want there to be an automatically generated number by order. here is my form code:

<!DOCTYPE html>
<html>

<head>

    <title>Reçu</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <link rel="stylesheet" href="PdfStyles.css">
    <link rel="icon" type="image/x-icon" href="img/schoolicon.png">

</head>

<body>
    <style>
        body {
            background-image: url('img/backo.jpg');
        }
    </style>
    <br>
    <br>


    <div class="hero">
        <div class="mt-5">



            <form action="pdfmaker.php" method="post" class=" offset-md-3 col-md-6" style="text-align:center;">

                <h1 style="color:white">Création de reçu</h1>

                <div class="row mb-2  ">
                    <div class="col-md-6 mb-2"><input type="text" name="nrecu" placeholder="Numéro Reçu" class="form-control" required></div>
                    <div class="col-md-6"><input type="text" name="fname" placeholder="Élève" class="form-control" required></div>
                    <div class="col-md-6 mb-2"><input type="text" name="nClass" placeholder="Classe" class="form-control" required></div>

                    <div class="col-md-6 mb-2"><input type="text" name="Moi" placeholder="Mois" class="form-control" required></div>
                    <div class="col-md-6 mb-2"><input type="text" name="Mont" placeholder="Montant" class="form-control"></div>
                    <div class="col-md-6"><input type="text" name="anne" placeholder="Année scolaire" class="form-control" required></div>
                    <div class="col-md-6 mb-2"><input type="text" name="total" placeholder="Total" class="form-control" required></div>
                    <div class="col-md-6 mb-2"><form action="/pdfmaker.php">
                        <label  style="font-family:fantasy;font-size:15px"for="cars">Mode de paimenet:</label>
                        <select name="mode" id="mod">
                            <option value="Espèce - نقدا">Espèce - نقدا</option>
                            <option value="Chèque - صك بنكي">Chèque - صك بنكي </option>
                            <option value="Carte Bancaire - بطاقة بنكية">Carte Bancaire - بطاقة بنكية</option>
                        </select>
                        <br><br>
                    </form></div>


                    <button style="background-color: #8064A2 !important; font-family:fantasy;font-size:30px" type="submit" class="btn btn-success btn-lg btn-block">Imprimer</button>







            </form>

        </div>

    </div>




</body>

</html>

the "nrecu" div is the one that takes care of the number, this line:

<div class="col-md-6 mb-2"><input type="text" name="nrecu" placeholder="Numéro Reçu" class="form-control" required></div>

and I believe I'd be able to achieve what I wanted just from this code, but if not, i'll include the pdf maker code, that generates the pdf.

this is the part of the pdf generation code that handles that number: first:

$nrecu=$_POST['nrecu'];

second:

<p style="text-align:center;font-family:monospace;">Reçu N° '.$nrecu.' ايصال رقم </p>

any help will be apperciated.

Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
john
  • 106
  • 1
  • 7
  • 1
    Don't you have a database for your website? Save last number there and update it every time you make new receipt. Or save all related receipt data in the DB and it can automatically increase the number each time. If you don't have a database and you just want to save the number, you can use php ```file_put_contents``` and ```file_get_contents``` functions. Save your last number to the file, get it when you need, update it and save again – Lothric Jul 01 '22 at 00:53

1 Answers1

1

you could try to do that PHP create a file if not exists and on every print update the value in it. This should look like:

    $file = fopen("my_file.txt", "a"); // try to open the file, if not exist create it

    if(filesize("my_file.txt") == 0){ // if the file is empty, write it with the value 1
        fwrite($file, "1");
    }else{ // if the file is correct execute the code
        $value = file_get_contents("my_file.txt", "r"); // get the value of the first line
        ftruncate($file, 0); // remove the first line
        fwrite($file, $value + 1); // write the new value on the first line
        $nrecue = file_get_contents("my_file.txt", "r");
}

and now you just need to set the value in you html

Hope this helped.

Tenteur
  • 49
  • 1
  • 5