Page 1 of 2
I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 2:32 pm
by djkazoo2002
Hi, I am really new to php, I started to learn about php 2 weeks ago, I am having really difficult time to figure out some questions, even though I am learning from many Youtube and webs. I am wondering if somebody can help me.
Q1
Prompt user to input an integer between 0 and 100 inclusive.
Using a 10 point curve determine the letter grade.
Display the integer and the letter grade..
Q2
Prompt user for his or her birth date.
Calculate the user’s age.
Display the user’s birth date and age
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 6:21 pm
by califdon
Have you learned to program in any other language?
What have you tried so far?
Is this to fulfill a class assignment?
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 7:16 pm
by djkazoo2002
I spoke Japanese, I just started to take this class in Us. I am having really difficult time to some assignment to translate. I checked many youtube which,for me, to easy to understand, and is similar to questions, I could d, but I am having hard time this 2 questions.
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 8:05 pm
by califdon
djkazoo2002 wrote:I spoke Japanese, I just started to take this class in Us. I am having really difficult time to some assignment to translate. I checked many youtube which,for me, to easy to understand, and is similar to questions, I could d, but I am having hard time this 2 questions.
I will try to use simple language. Is your problem to understand the questions, or to write the PHP?
Q1:
In USA, we have letter grades, A, B, C, D, F.
A is best,
F is fail. Each class may have different scale, like 90-100 =
A, 80-89 =
B, 70-79 =
C, 60-69 =
D, below 60 =
F (a '10-point curve'). So when user enters number between 1 and 100, your code must display what letter grade that number is. If user enters 83, your code should display
B.
Q2:
Ask user for their date of birth (month, day, year). Your code must compare that date with "today" date and calculate user's age in years. So if user enters date like "04/16/1994", your code must compare with today (01/30/2012). Since we are not yet to 04/16 this year, the user would be
17 years old (2011 - 1994) until 04/16 this year, then user would be
18.
Does that help?
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 10:01 pm
by djkazoo2002
this is really really helpful, I really appreciate this. I think I can write it down PHP. it will take times but I will post php after I done, I really appreciate thank you so much.
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 10:49 pm
by califdon
You are very welcome. I taught in college for over 10 years, so I understand how difficult it is for someone to learn at the same time as learning a new language! Good luck and return here if we can help again later.
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 10:51 pm
by djkazoo2002
So, I wrote PHP, when I enter the number, for example $grade= 77; or $grade= 96; F keeps coming up, so I did something wrong. I am wondering if you can tutor me
I wrote...
<?php
$grade= 99;
if($grade == "100,99,98,97,96,95,94,93,92,91 or 90")
echo "A";
elseif ($grade == "89,88,87,86,85,84,83,82,81 or 80")
echo "B";
elseif ($grade == "79,78,77,76,75,74,73,72,71 or 70")
echo "C";
elseif ($grade == "69,68,67,66,65,64,63,62,61 or 60")
echo "D";
else
echo "F";
?>
I use if/elseif/else, if I was right. If there is another way, please show me.
I have questions
A. I do not know how to write for example "from 100 to 90" on php. so I had to write it down ($grade == "100,99,98,97,96,95,94,93,92,91 or 90) or (100,99,98,97,96,95,94,93,92,91,90), is there way to write "from 100 to 90"?
B. "user enters number between 1 and 100" I do not know how to make or write 'space'? or 'tag'? for letting user to enter number between 1 and 100 on my Php code.
I am so sorry about my poor English.
Thank you so much.
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Mon Jan 30, 2012 11:51 pm
by califdon
Don't worry, your English is much better than my Japanese!
There are several ways to test if a value is within a range, or within a list of values. The simplest way in your case might be this:
Code: Select all
if($grade > 89)
echo "A";
elseif($grade > 79)
echo "B";
elseif($grade > 69)
echo "C";
elseif($grade > 59)
echo "D";
else
echo "F";
An alternative might be to use the PHP
in() function:
. . Wrong! See my second post below . .Code: Select all
if($grade in(90. 91. 92. 93, 94, 95, 96, 97, 98, 00, 100))
echo "A";
elseif($grade in(80, 81, 82, 83, 84, 85, 86, 87, 88, 89))
echo "B";
...etc.
and there are at least a couple of other ways. But I think you can see that the first one is probably easier in this particular case.
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Tue Jan 31, 2012 12:54 pm
by djkazoo2002
Thank you so much, this is really best help, I do not have any friends around me to know Php, so I can't ask anybody, I really appreciate it, especially in() function.
I have one more question
I do not know I can describe my question right, but I try,
when open Php page I meant localhost/homework.php I want to make there a space, so somebody can write the number in, for example 94, then click, that page shows A,
do you know how to write it on Php. A, make a space, for people can write the numbers. B, cliclk "button".
Thank you so much.
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Tue Jan 31, 2012 1:14 pm
by califdon
That is done with an HTML
form. It can be sent to the browser using PHP, but it is just HTML. You will need PHP to receive data that the user enters in an HTML form. Try these tutorials. If you need more help, return here.
http://www.w3schools.com/html/html_forms.asp
http://www.javascript-coder.com/html-fo ... l-p1.phtml
http://www.tizag.com/htmlT/forms.php
http://www.htmlgoodies.com/tutorials/fo ... rm-Huh.htm
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Tue Jan 31, 2012 8:04 pm
by califdon
califdon wrote:An alternative might be to use the PHP in() function
One of our other regular forum members caught my mistake. The above is NOT correct. There is no PHP function
in(). I was thinking about the in() function in SQL! There
is a PHP function
in_array(), which might be used in your example, with some extra code to create arrays, but I do not want to confuse you (if I haven't already confused you). Sorry about my mistake.
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Wed Feb 01, 2012 2:47 pm
by djkazoo2002
No problem!!!!, I really really appreciate your help
so I wrote like this, but it does not work. so if you can show me
<form name="input" action="" method="">
Grade:<input type="text name="Grade"/> <br/>
<input type="submit" value="submit" />
</Form>
<?php
if($grade > 89)
echo "A";
elseif($grade > 79)
echo "B";
elseif($grade > 69)
echo "C";
elseif($grade > 59)
echo "D";
elseif ($grade < 58)
echo "F";
?>
whenever I type the number, and click submit, it does not do anything, and F is always on the screen.
I am guessing <form name="input" action="" method=""> is wrong.
what should I have done?
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Wed Feb 01, 2012 5:42 pm
by califdon
OK, you're getting closer. There were several things wrong with your script:
- Your code doesn't look for the value that the user enters, so you have to add a line that will do that.
- As you suspected, you need to supply a value for at least the 'method' parameter of the <form>. There are 2 possible methods: GET and POST. Read about that here: http://myphpform.com/html-form-post-or- ... %20method..In most cases you will probably want to use the POST method.
- Your code used an uppercase G in the word Grade as the name of that form input element, but a lowercase g in the variable name $grade; remember, PHP is case sensitive!
- Your code was missing a quotation mark following the word "text" in the line that has the <form...> tag.
- Although it will work this way, you really should get in the habit of always sending a proper HTML page to the browser.
I made those changes and tested this modified script:
Code: Select all
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form name="input" action="" method="post">
Grade:<input type="text" name="grade"/> <br/>
<input type="submit" value="submit" />
</form>
<?php
$grade = isset($_POST['grade']) ? $_POST['grade'] : 0;
if($grade > 89)
echo "A";
elseif($grade > 79)
echo "B";
elseif($grade > 69)
echo "C";
elseif($grade > 59)
echo "D";
elseif ($grade < 58)
echo "F";
?>
</body>
</html>
Now, several comments:
As I did above, you CAN leave the "action=" parameter blank and it will default to calling the
same script again to read the value entered by the user and process it. That is OK in this example. But in typical scripts you will probably have your script that handles the input
in a different file, so this is where you must tell PHP what script to call. The important thing is to understand that it must return to the server so that PHP can look for the values entered by the user.
In this simple case, it takes only one line to look for the values sent by the POST method. A more complex form would take more lines. The syntax of that line, above, may look strange. What it means is that a value will be assigned to the variable
$grade as follows: if a value is
set for
$_POST['grade'], then that value will be assigned to the variable
$grade but if no value is set (the first time the page is displayed, before the user has entered a grade), it will assign the value
0. This is called a
ternary operator and you can read about it at
http://www.tech-evangelist.com/2007/11/ ... -operator/. You can also read about the
isset() function at
http://php.net/manual/en/function.isset.php.
Anyway, this is a good start. Soon you will be programming complete web pages with PHP!

Re: I am really new to Php, I wonde if somebody can show me,
Posted: Wed Feb 01, 2012 9:40 pm
by djkazoo2002
ok, so finally I could write!!!!!
<html>
<head>
</head>
<body>
<form name="input" action="grade.php" method="post">
Grade:<input type="text" name="grade"/> <br/>
<input type="submit" value="submit" />
</form>
</body>
</html>
and
<?php
$grade = isset($_POST['grade']) ? $_POST['grade'] : 0;
if($grade > 89)
echo "A";
elseif($grade > 79)
echo "B";
elseif($grade > 69)
echo "C";
elseif($grade > 59)
echo "D";
elseif ($grade < 58)
echo "F";
?>
now it's working!!!!! Thank you so much!!!!
Re: I am really new to Php, I wonde if somebody can show me,
Posted: Wed Feb 01, 2012 11:03 pm
by califdon
I'm happy that you have succeeded in getting your script to do what you want it to do. This is the joy that we all feel when we write code! Enjoy!