php post array process non-input text references

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
grNadpa
New php-forum User
New php-forum User
Posts: 21
Joined: Fri Dec 02, 2016 8:32 am

Sat May 26, 2018 6:41 am

(Used PHP forum search keywords "post array". Results not exactly addressing my issue.)

For the billing component of my application, I provide a HTML5 form listing all the items eligible for inclusion on an invoice. My plan is for each row to provide item information from my model array along with a form input text. The user then has the ability to specify an amount to be billed next to one or more of the rows on the list. (input as name="invoiceamt[]";)

My issue is that the key fields I will need to associate with the invoiceamt or invoiceamts when I produce the invoice .

On user response, I need to assign the item key to the relevant invoiceamt[].
  • I cannot assume the database table will remain unchanged, so repeating the query is not an option
  • creating and unenable-ing form input array fields on the row to hold the key information seems awkward,
  • rendering an array to $_SESSION for this purpose appears to be the best of the bad options
Is there a better approach?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 711
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 26, 2018 7:24 am

I cannot assume the database table will remain unchanged, so repeating the query is not an option

creating and unenable-ing form input array fields on the row to hold the key information seems awkward,

rendering an array to $_SESSION for this purpose appears to be the best of the bad options
Are all moot:

Once the form has been validated and is correctly filled in, create the invoice, job done; there is no need to keep state, wait for or do anything else in between creating the form and producing the invoice.

If you have any specific examples of where any of the above would or could create a problem, post here as I don't think there should be.

grNadpa
New php-forum User
New php-forum User
Posts: 21
Joined: Fri Dec 02, 2016 8:32 am

Sun May 27, 2018 1:29 pm

hyper wrote:
Sat May 26, 2018 7:24 am

Are all moot:

Once the form has been validated and is correctly filled in, create the invoice, job done; there is no need to keep state, wait for or do anything else in between creating the form and producing the invoice.
If the project initiation and the billing were one-time events done at the same time and on the same form, I agree. But that is not the case here. The project entry involves multiple phases entered and billed as different events and at different times. Further, a single phase can involve multiple partial billings.

Here is the code snippet in the view script

Code: Select all

<form name="billform" class="billform" action="" method="post">
            <div id="listdiv">
                <span>Title</span>
                <span class="amt">Amount</span>
                <span class="cntr">Date</span>
                <span class="amt">Billed</span>
                <span class="amt">Paid</span>
                <span class="amt">New Bill Amt</span>
<?php
        foreach($this->model->workunits as $workunit) {
					...
?>
                    <span><?php echo($workunit['title']); ?></span>
                    <span class="amt"><?php echo($amount); ?></span>
                    <span class="cntr"><?php echo($date); ?></span>
                    <span class="amt"><?php echo($billed); ?></span>
                    <span class="amt"><?php echo($paid); ?></span>
                    <span class="amt"><input type="text" name="amount[]" /></span>
<?php
        } // end foreach
?>
Note that only the amount is a form input. The other 5 fields are display-only. So a var_dump of the $_POST (for my two test phases) shows only
array(2) { ["amount"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["sub"]=> string(6) "Submit" }
So I need another way to match the $_POST entry to the project phase.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 711
Joined: Mon Feb 22, 2016 5:52 pm

Mon May 28, 2018 2:34 am

In that case, create a temporary holding area for the information until all of the information is available to generate the invoice. An invoice should be final, any amendments to it should only be using other documents (credits etc..) I wouldn't / haven't / don't generate an invoice until I have all the details. Think of it like shopping, you don't get the competed bill until all the items have been scanned or put into your online basket a clicked checkout.

I think that you will have problems trying to store the information using sessions if the details are entered at different times and certainly if entered from different places. Sessions are intended to be temporary, PHP defaults to storing sessions for 30 minutes; although you can change this, it's not always a good idea. Cookies last longer; but this opens to the door to insecure applications and lack of portability.

I would keep the details in a file or on a database (in a basket if you like) until complete.

grNadpa
New php-forum User
New php-forum User
Posts: 21
Joined: Fri Dec 02, 2016 8:32 am

Mon May 28, 2018 5:07 am

Thank you Hyper.
hyper wrote:
Mon May 28, 2018 2:34 am
Think of it like shopping, you don't get the competed bill until all the items have been scanned or put into your online basket a clicked checkout.
This is an application for a construction contractor. The prices are set (sans change orders and extras) in the contract. Billing occurs at specific points in the project. Most of these billings involve partial payments billed at the start of the phase with the remainder upon phase completion. So unlike shopping, there is no "all items in an online basket" per se -- if I understand your analogy.
hyper wrote:
Mon May 28, 2018 2:34 am
I would keep the details in a file or on a database (in a basket if you like) until complete.
I wanted to avoid hitting the database again to generate the invoice upon user submit. I tried a number of options, and finally settled on hiding the database keys by adding a hidden input type from my above code thus:

Code: Select all

<span><?php echo($workunit['title']); ?></span>
<span class="amt"><?php echo($amount); ?></span>
<span class="cntr"><?php echo($date); ?></span>
<span class="amt"><?php echo($billed); ?></span>
<span class="amt"><?php echo($paid); ?></span>
<span class="amt"><input type="text" name="amount[]" /></span>
<span><input type="hidden" name="workunitid[]" value="<?php echo($workunitid); ?>" /></span>
This raises havoc with my css grid forcing a null column (thus screwing up my justify-content), but appears to be the least painful. And, alas, it does require repeating my database query after the post.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 711
Joined: Mon Feb 22, 2016 5:52 pm

Mon May 28, 2018 8:02 am

This raises havoc with my css grid forcing a null column (thus screwing up my justify-content), but appears to be the least painful. And, alas, it does require repeating my database query after the post.
Don't put the hidden field in a span tag, the input maybe hidden, but the span isn't.
This is an application for a construction contractor. The prices are set (sans change orders and extras) in the contract. Billing occurs at specific points in the project.
You're falling into the realms of accountancy: what is eventually billed (for the whole job) doesn't matter, what is quoted doesn't matter, what is actually billed and when it is billed does matter. Tax and end of year depend on when the invoice is issued. What you make on a specific job is another thing, accountants don't care (as such) all that they are interested in is what was invoiced (sales and purchase), any equipment purchases (they depreciate and are held as capital) etc.. during your year start to year end so that they can work out what you need to pay the government.

Keep in mind that the invoicing and job specific profits are completely separate, you have a tax year and a job life; some jobs can last hours and others can go on for years; your invoice relates to one specific time period, the year end profits and losses relate to a time period; invoices were invented so that your government get their taxes.
I wanted to avoid hitting the database again
That's what they are for and perfect for keeping tabs on what is what and creating views (invoice, profit and loss sheet etc.. all from the same data without have to enter anything else)

Another way of thinking of this is to create a work sheet which keeps details of what is done, then at the end of the month / quarter, you can generate an invoice based on the dates entered on the work sheet?

I hope that I have been clear, I know that accounts can seem unnecessarily complicated (sounds like programming :D ) it's so easy to get mixed up. Keep the idea of the invoice as a separate thing and not a part of something bigger [the job]. The something bigger is your quote and daily work sheets from site, keep them separate and you will find it easier to tackle.

Post Reply