I am trying to make a calculator to calculate your age in dog years, the following is the code I have so far, minus the html portion of it

``````<?php

if (!isset(\$_POST['your_age']) ) {
exit;
}
if (\$_POST['your_age'] == "your_age") {

\$result = (\$_POST['your_age'] *7;

if (\$_POST['your_age'] >= 100 && \$_POST['your_age'] <= 199) {
\$result = "An Old Dog";
}

else if
(\$_POST['your_age'] >= 200 && \$_POST['your_age'] <= 299) {
\$result = "Wicked Old Dog";
}

else if
(\$_POST['your_age'] >= 300) {
\$result = "Ancient Dog";

}

?>

<HTML>
<TITLE>Calculation Result</TITLE>
<BODY><H2><center>

<P>The result of the calculation is: <?php echo \$result; ?></p>

</BODY></H2></center>
</HTML>
``````

I am having trouble multiplying the Your_age variable by 7 which would be the dog years compared to human years. Anyway, i am kind of a newbie at this and have been sitting here for a few hours, struggling with where to place it or how to go about doing it.

It needs to multiply by 7 and then be printed to the screen upon hitting submit.

Any input would be greatly appreciated.

## All 11 Replies

I really can’t see how this code is working properly, In line 7 you open an if that is never closed. Using an IDE like Eclipse or Netbeans would reveal you that in the first place. I believe that IDE’s are not only for programmers but for beginners as well.

yeah i deleted that from my code, I meant to copy and paste it without that. I was in the middle of deleting code and adding stuff to see if I could make it work.

without lines 7-9, i am not getting any errors and it displays fine in my browser. But still am having trouble with what i stated above.

What exactly do you know about programming? … Don’t get me wrong but if you don’t understand what the if statement means you need to read a little (less than a quarter of an hour) (how can somebody can’t understand what if means ? ) …

You deleted those lines so there is no if to that… and nothing happens… The question that arose to my head is why taking time to post such kinds of questions … If it is just a school project then Google is your friend , if you really interesting to learn programming one day , then again reading the basics (conditions , loops etc) is a good idea.

again, that post is no help. im not claiming to be a pro at php, because i never want to be. this is for a school project and like i said i am stumped. All i was asking was how i would go about taking the number that is entered in the submit box, which is the your_age variable and multiply it by 7 so it will display one of the different ages of dogs that are in my code.

Programming is one of my weakest subjects and i have googled this problem, but what i come across doesnt seem to fit what i am doing.

The ages of dogs are not multiplied by 7 of human years , but this is something that goes deeper than your question. I answered in my first post that you are missing a bracket in the if statement in lines 7 -9 and you resulted not to add the bracket but to delete those lines (why ?) . Add the bracket in correct position. You said that you Googled .. maybe you did the final result of what you are asking… Google first PHP basics , than PHP conditions if then else … and if you have time Programming basics as well …

There are so many errors in there,
1. on line 7, What is 'your_age'? If it is a PHP variable, it should be written as \$your_age.
2. There should be a } for the if statement in line 10.
3. \$result is a local variable and it doesnt exist outside the if-else statements.

Solution: make the \$result a global variable by, declaring it above all if/else statements and outside any brackets like this,
\$result = "";

Now you will get the result

the whole code is wrong, as everyone says there is so many errors.
since it is for your school project, i will help you but plz take some time and learn php. :)

``````<?php
\$dogage_yours='';
\$output='';
if (\$_SERVER['REQUEST_METHOD'] == 'POST' && \$_POST['form_name'] == 'ageform')
{
\$dogage_yours= \$_POST['age']*7;

if (\$dogage_yours>=100 && \$dogage_yours<=199)
{
\$output = "An Old Dog. age= ";
}
if (\$dogage_yours>=200 && \$dogage_yours<=299)
{
\$output = "Wicked Old Dog. age=";
}
if (\$dogage_yours>=300)
{
\$output = "Ancient Dog. age=";
}

}

?>
<!DOCTYPE HTML>
<html>
<body>
<form name="ageform" method="post" action="<?php echo basename(__FILE__); ?>" id="signupform">
<input type="hidden" name="form_name" value="ageform">
<input name="age" type="text" id="fullname" style="width:300px;height:25px; border-radius:0px;" placeholder="age">
</form>
<?php echo \$output; ?><?php echo \$dogage_yours; ?>
</body>
</html>
``````

this code works. i have modified variable names according to my needs, feel free to change them.

Solution: make the \$result a global variable by...

That's not creating a global variable, that's initialising it / giving it a default value.

again, that post is no help.

You're right that this is a help forum, but it's for advice and discussion as opposed to "service on a plate". You have provided code with errors and jkon was pointing out that you made a mistake and suggested that you use an IDE. You returned stating that you posted the "wrong code". That's a little annoying as contributors give of their free time to help those who try to help themselves, so it's very important that we use their resources carefully. jkon proceeded to give you tips on where to find info on the issue.

im not claiming to be a pro at php, because i never want to be... Programming is one of my weakest subjects

What are we to make of that? A teacher would give you some indication as to what your issues were and guide you to some resources so that you could complete the exercise yourself. jkon, take a bow.

@diafol: You pointed out that its not a global variable.. But by technicality isn't that a global variable? Please tell me why we cant say it is a "Global Variable"

commented: fair one +14

No you're right. It is a global variable - I was thinking more along the global keyword. My bad. :)

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.