Hi everyone,

I am creating a invoice page, for that in the invoice table I have fields like(id, ionvoice_no, invoice_date,client_name,address,service_cost,tax,etc).
While I enter invoice, I don not want to enter invoice, but it should change automatically from 1
previously it has to check whether already invoice number is or not. if it is there each time after submitting it has to increase by one, else it has to set one number by default.

How to do?

Recommended Answers

All 5 Replies

Hi i have used below function in my project. i am not expert of php but trying.below code is for Codeigniter. hope it help you.

function generate_invoice($l, $c = 'abcdefghijklmnopqrstuvwxyz1234567890') {
        for ($s = '', $cl = strlen($c) - 1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i)
            ;

        return $this->check_invoice_id_before($s);

    }

    public function check_invoice_id_before($invoice_id) {

        $this->db->select('st.invoice_id');
        $this->db->from('solobanking_transactions st');
        $this->db->where('st.invoice_id', $invoice_id);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $this->generate_invoice(4, '0123456789ABCDEF');
        } else {
            return $invoice_id;
        }
    }

Aren't you using an autoincrement field in your table?

is it autoincrement or the manual increment of your own desired coding?

like @pritaeas said use an autoincrement field for it

if you cant you can always use something like this select (max(ionvoice_no) + 1) from invoicetable in your insert code

auto increment will genereate number only like 1,2...etc.But if some one wants to generate some randome string and validate that string in mysql , so above function will works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.