Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi all. I'm writing a web app for personal business use. It's a big job and will handle invoicing, payments, time scheduling and so on. I expect it will take the better part of 6 months or a year to finish in my spare time.

I've made a couple of false starts but seem always to end up with spaghetti code. My major obstacle is keeping the program aware of what it's current status is.

For example, there's a page to add a new customer, or a new supplier and they work fine. sometimes while entering an invoice it happens that the supplier or the customer or work location or something that does not exist. The app. needs to divert to the "New customer" page, get the data and return to the original point. While entering the new customer it might happen that the customer uses a bank that's never been entered before and so it needs to divert to the "New Bank" page and then backtrack to where it started.

To make the problem clearer - please consider the web-interface my bank uses. I have to log in to do anything at all. If I'm half way through a transaction and the session times out, I'm taken to the log-in page again, but after entering the login details I don't go to the main menu again ... it takes me to where I left off with the transaction. Somewhere behind the scenes a clever programmer kept track of what I was doing, so the log-in page can be re-used over and over at different points in a session. That's the kind of thing I need to do.

I've been using session variables and some hidden variables in the pages to assist but as the program grows it becomes cumbersome and confusing. There's an endless list of if's or's and's - rules and exceptions to rules.

Can anybody give some good generic advice about how to integrate and track all the various sub-components of a large application like this?

Thanks in advance.
Angus
Posted

1 solution

Good Question. There are many ways you can do this.

I would use an array stored in a session variable ("$tasks" - see below). I'd also have each page save data as you go along and refer to everything using a Unique ID - so that if you get halfway through and realise you need to do something else you can go off and do something else and the data will be stored.

I also suggest putting the code that stores a page's data to the database into a function so that you can reference it from any other file. This way, if your form has 5 possible exit points - you only need to write the code that stores the data once.

When you reach a point where they will be redirected to a new page add the current page to the tasks array:

$tasks = $_SESSION['tasks'];
$tasks[] = "/editInvoice.php?id=$invoiceid";
$_SESSION['tasks']= $tasks;

....
(on the next page: )

$tasks = $_SESSION['tasks'];
$tasks[] = "/editCustomer.php?id=$custid";
$_SESSION['tasks']= $tasks;

Then when a task is complete you can pop the array and redirect to the popped item:

$redirect = array_pop($tasks);
$_SESSION['tasks']= $tasks;

header("Location: $redirect");

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900