-1

I am building a POS with electronic invoices, because the invoice number will start with 1 and it is ugly to show invoice number : 1 I want to build something to add 00000 as prefix like this

$lastinvoice = mysqli_query($db, "SELECT * FROM `sales` 
    order by `id` DESC LIMIT 1");
$last_invoice = mysqli_fetch_assoc($lastinvoice);
$invoice = $last_invoice['id']+1;

building the prefix

$prefix=if (($invoice)<10){echo '0000000'};
elseif (($invoice)<100){echo '000000'};
elseif (($invoice)<1000){echo '00000'};
elseif (($invoice)<10000){echo '0000'};
elseif (($invoice)<100000){echo '000'};
elseif (($invoice)<1000000){echo '00'};
elseif (($invoice)<10000000){echo '0'};

Now in the HTML form I submit a hidden input like this

<input type="hidden" name="set_invoice"
    value="<?php echo "$prefix$invoice"?>">

I am getting error with the builded prefix, it is not well made but because I am new to PHP I just do not know how to do that...

Any ideas ?

The error is

PARSE ERROR: SYNTAX ERROR, UNEXPECTED 'IF' (T_IF) IN /HOME/LOCAL/PUBLIC_HTML/*******/SALES.PHP ON LINE 6

and Dreamweaver marks all the builded $prefix as bad coded

NOT A DUPLICATED

It is not a duplicated question. What I need is not only leading zeros but to check the last invoice and accoding to this apply the ammount of zeros and the links to the "duplicated" posted by someone does not solve the problem because is not the same kind of question ...

FirstOne
  • 6,033
  • 7
  • 26
  • 45
Liberator
  • 67
  • 6
  • Some tests: [https://eval.in/614556](https://eval.in/614556) – FirstOne Jul 31 '16 at 02:01
  • First its not a duplicated, second I edited the question with the error I am getting. What I need is not only leading zeros but to check the last invoice and accoding to this apply the ammount of zeros – Liberator Jul 31 '16 at 02:03
  • The answers posted in the "duplicated" does not solve my problem and is not even the same kind of question or problem – Liberator Jul 31 '16 at 02:05

2 Answers2

2

If you want your invoice number in 8, you can do like this:

 $invoice = sprintf("%08d", $invoice);

If $invoice is 12, it'll be 00000012

And for the next auto_increment:

 <?php
 $req = $bdd->query("SHOW TABLE STATUS FROM table_name LIKE 'table_name' ");
 $donnees = $req->fetch();
 echo $donnees['Auto_increment'];
 ?>
Amazone
  • 426
  • 4
  • 14
0

For those finding a similar solution I found it out myself

$lastinvoice=mysqli_query($db, "SELECT * FROM `sales` 
order by `id` DESC LIMIT 1");
$last_invoice=mysqli_fetch_assoc($lastinvoice);

then we build the prefix

$invoice=$last_invoice['id']+1;
if ($invoice <'10'){$invoice = "0000000$folio";}
elseif ($invoice <'100'){$invoice = "000000$invoice";}
elseif ($invoice <'1000'){$invoice = "00000$invoice";}
elseif ($invoice <'10000'){$invoice = "0000$invoice";}
elseif ($invoice <'100000'){$invoice = "000$invoice";}
elseif ($invoice <'1000000'){$invoice = "00$invoice";}
elseif ($invoice <'10000000'){$invoice = "0$invoice";}
elseif ($invoice <'100000000'){$invoice = "$invoice";}

And last we set the input hidden or text etc like this

<input type="hidden" name="set_invoice"
value="<?php echo "$invoice"?>">

No need of complications or reading a lot of unussefull things

Liberator
  • 67
  • 6
  • It is working so perfect that I cannot believe I did it myself, maybe is a wrong way to do it but it works – Liberator Jul 31 '16 at 02:40