Forum Moderators: coopster

Message Too Old, No Replies

New to PHP, could use some help

         

mattwiw

9:23 am on Oct 8, 2010 (gmt 0)

10+ Year Member



Thanks for taking a look.

I'm working on a somewhat basic PHP form, user fills in fields, upon submission the fields are posted into a text document for print.

There are however a few variables, and an equation that needs to be done.

By answering three questions, the form needs to establish which of the three tiers the person is in. I have been able to work out an if/else statement for this.

I now need to apply an equation to that specific tier, it really isn't that complex but rather long. I could probably figure out an extensive if/else statement to handle the whole thing, continually nesting if/else as necessary (would be quite deep) but there has got to be a much simpler way of doing this.

If someone could take a look and perhaps point me in the right direction I should be able to figure it out on my own.

This determines which of the three tiers. There's only one possible combination of the three questions to be in tier 1, only one possible combination for tier 2, and the rest are simply tier 3. Below is what I came up with from what I learned reading about PHP today. It works, not sure if it's perfect. The echo "Tier x" was simply a test for me to see that it works, I would expect that this is where I will have to continue the statement to run the equation.

<?php
if ($_POST["timeinbusiness"] == "six") {
if ($_POST["homeowner"] == "hyes") {
if ($_POST["bankruptcy"] == "bno") {
echo "Tier 2";
} else { echo "Tier 3";}
} else { echo "Tier 3";}
} else { if ($_POST["timeinbusiness"] == "three") {
if ($_POST["homeowner"] == "hyes") {
if ($_POST["bankruptcy"] == "bno") {
echo "Tier 1";
} else { echo "Tier 3";}
} else { echo "Tier 3";}
} else { echo "Tier 3";}
}
?>


The equation: (xy)/1000 = answer
Where x is a # provided when the form is filled out. And y is determined by a chart like so:

    [where does x fall?]       [y]         [y]         [y]         [y]
    $ 1,000 - $ 2,500       55.33      40.15      32.69      28.31
    $ 2,500 - $ 5,000       54.24      38.99      31.46      27.02
    $ 5,000 - $10,000       53.88      38.60      31.05      26.60
    $10,000 - $15,000      53.63      38.35      30.79      26.32
    $15,000 - $20,000      52.92      37.75      30.25      25.80
    $20,000 - $30,000      52.21      37.16      29.71      25.30
    $30,000 - $50,000      51.61      36.70      29.31      24.94
    $50,000 +                 51.03      36.24      28.92      24.57

Basically, the equation needs to be run four times, to each column, depending on which range x falls between. So there a four separate posts of the answer in the document.

You can see how this is rather long, but not terribly difficult. I can think of a very very very long winded if/else statement that could do it but there must be a different way to handle things like this.

Again, I'm looking for someone to point me in the right direction, as this is quite a bit of work.

Note, the above is just one of the three tiers.

Thanks for taking a look.

Anyango

10:12 am on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting maths. When i get a little free i will give it a shot and see if i can help you with it. But what i would surely like to mention here right away is that lets try to improve that if else structure, this way its in its toughest possible form and almost "bad" to use. Lets simplify it so your concept about an if else is improved and you dont tire yourself in future.

Lesson 1) to run an IF on multiple variables or multiple conditions, we dont have to use as many IF as many variables. for example This is complicated and error prone here

if ($_POST["timeinbusiness"] == "six") {
if ($_POST["homeowner"] == "hyes") {
if ($_POST["bankruptcy"] == "bno") {
echo "Tier 2";


We can use 1 IF to get this job done


<?php
if ($_POST["timeinbusiness"] == "six" && $_POST["homeowner"] == "hyes" && $_POST["bankruptcy"] == "bno")
{
echo "Tier 2";
}
?>


That will check for all 3 of your conditions, and echo Tier 2 only if they are all true.
So while we are waiting, please restructure your if else to make your life easier.

&& in the statement means "AND"

mattwiw

5:24 pm on Oct 8, 2010 (gmt 0)

10+ Year Member



Great, thanks for the simplification, I am always striving to have clean code and as little of it as possible. This reminds me of when I started using CSS many years ago and my stylesheets were really long, now they are very short and efficient.

<?php
if ($_POST["timeinbusiness"] == "three" && $_POST["homeowner"] == "hyes" && $_POST["bankruptcy"] == "bno")
{
echo "Tier 1";
}
elseif ($_POST["timeinbusiness"] == "six" && $_POST["homeowner"] == "hyes" && $_POST["bankruptcy"] == "bno")
{
echo "Tier 2";
}
else
{
echo "Tier 3";
}
?>

Anyango

5:43 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool, so if x=21000 then we pick the following 4 values of y one by one ?

52.21,37.16,29.71,25.30

multiply them with x and then divide with 1000 and print 4 answers ?

mattwiw

5:52 pm on Oct 8, 2010 (gmt 0)

10+ Year Member



You're on the right track but it occurred to me that the equation could be simplified to make this easier.

I should just apply the y/1000 to all those #s in the table first and then the equation is simply x*y.

So x*0.05221 instead of (x*52.21)/1000. This seems a lot more intelligent and should cut down the PHP.

I've started working on something, not sure if it's the right direction:
<?php
$c = $_POST["cost"];
if ($c > 20000 && $c < 29999)
{
$c *= 0.05221;
echo $c;
}
?>


I used #s from your example in your last post. Is this in the right direction? Thanks.

Also, I foresee that I will end up with answers that have more than 2 decimal places, I am going to need a way to make the answer post to only 2 places.

Matthew1980

6:11 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there,

if (($c > 20000) && ($c < 29999))


You need to parenthesize your components in the evaluation clause, I'm not saying this will cure your issue, but it makes the code easier to read, and logically makes sense now as it reads:-

if ((this evaluates true) && (this evaluates true)){
do this
}
else{
do this instead
}

Hope that makes sense.

Cheers,
MRb

Anyango

7:42 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$a=2.26232323260;
$a=round($a,2); // this will echo upto 2 decimal places only

mattwiw

6:37 pm on Oct 19, 2010 (gmt 0)

10+ Year Member



thanks for the help guys, I was able to teach myself how to do it (using your advice) and it looks a lot more streamlined than I had originally envisioned.

Matthew1980

7:26 pm on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there mattwiw,

Cool, glad as you have got to a point where your happy with how it functions. Streamlining is a good learning tool as you will come back to something in a few months and think, "I can re write that and make it better", which will give you a great sense of achievement.

Kinda funny as I am doing just that very thing now - and it's amazing how much your scripts can change in just a few revisions...

Have fun with the rest of the project ;-p

Cheers,
MRb