0

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?

5
Contributors
5
Replies
36
Views
2 Years
Discussion Span
Last Post by mangel.murti
0

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;
        }
    }

Edited by mangel.murti: code hints

0

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

0

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.

Edited by mangel.murti: spell correct

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.