Page 2 of 2

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Fri Feb 03, 2012 1:46 pm
by djkazoo2002
Thank you so much again, I will try. I really really appreciate your help!!!!!

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Fri Feb 03, 2012 2:57 pm
by califdon
djkazoo2002 wrote:Once again, I am having a problem. the professor changed his mind, and he wants us to diplay -------- $Studentnumber gets $Grade because his/her score is $his/her score.---------
for exapmle, 123 gets A because her/his score is 95

A, I want to make a space, like sentence, "his score is78," I want "his score is 78"
B, Grade did not show up. how can I write correctly on php.
C,This F " F123's" kept showing up
D, I want to one sentence echo not like,

echo $_POST["studentnumber"] ;
echo "'s grade is <br/>";
echo $grade = isset($_POST['grade']) ? $_POST['grade'] : 0;
echo "because his score is";
echo $_POST["grade"];
A. & D. Wherever you want a space, you must put space in your quoted string. You can combine all of the output into one echo command, in fact a good way to do it is to build the string first, using the . PHP operator, which is the concatenation operator (means put together), then just echo the string, like this:

Code: Select all

$str = $_POST['studentnumber'] . "'s grade is " . $grade " . " because his score is " . $_POST['grade'];
echo $str;
Notice that I removed the <br>, which is the line-break HTML entity, and I have a space everywhere in the string where I want a space.
B. One source of confusion is that you are using the word "grade" to mean 2 things in different places. On the form (and thus the POST variable index) it means the number score, then when you assign a letter grade, you also call that "grade". In your code, you used $_POST['grade'] for where you want the score to show up, but that is the number score! So you need to use the variable $grade for that. It would be better to call the number "score" instead of "grade", but if you change it, you must remember to change it in all the places: the name of the input HTML entity in the form, and that changes the value you look for in the 2nd part of the script, from $_POST['grade'] to $_POST['score']. So then you will have to change the code in your if ... elseif section.
C. Because the first time we only wanted to print out the letter grade, the example I gave you just did that--there wasn't a need to save the grade as a variable, to be used again. Now, with these new requirements, you must change that if ... elseif section so that, for each case, instead of echoing the grade, it assigns the value to the variable $grade.

This is exactly what your professor is leading you to see. Programming is all about noticing little differences and changing how you think about what the script is doing. When I was learning a language called Forth many years ago, our instructor told us the 2 secret rules of programming, and I have always remembered them:
  1. Everything matters!
  2. Never Give Up!
I think that will get you to the next step. :)

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Fri Feb 03, 2012 8:16 pm
by djkazoo2002
Thank you so much again, I will try. I really really appreciate your help!!!!!

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Sat Feb 04, 2012 1:05 pm
by djkazoo2002
Ok, I need your help again, even though Mardi Gras starts today, I am till sturuggling this homework....

ok, so I did like this

<html>
<head>
</head>
<body>
<form name="input" action="grade.php" method="post">
Student Number:<input type="text" name="studentnumber"/> <br/>
Your Score:<input type="text" name="score"/> <br/>
<input type="submit" value="submit" />
</form>

</body>
</html>

and


<?php
$score = $_POST['score'];
$student_Number = $_POST['studentnumber'];
$grade = $_POST['score'];

if($_POST['score'] > 89)
echo "A";
elseif($_POST['score'] > 79)
echo "B";
elseif($_POST['score'] > 69)
echo "C";
elseif($_POST['score'] > 59)
echo "D";
elseif ($_POST['score'] < 58)
echo "F";

$strg = $student_Number . "'s grade is" . " " .$grade." " . "because his score is" ." ". $score;

echo $strg;


and showed up this

C4444's grade is 77 because his score is 77





so, before $student_Number 4444, showed C, because I echo $grade before $strg, I want to know that

I could echo out $grade,C, so why, $grade in $strg, showed up number,77?

i want to move that "C" to 77, how can I fix php code? could you show me appropriate php code?

I am guessing something is wrong $grade = $_POST['score']; and $score = $_POST['score']; which should not be the same value. so i want to know how to write correctly Php to get echo out.

Thank you again

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Sat Feb 04, 2012 1:28 pm
by califdon
That's terrible! NOBODY works in New Orleans on Mardi Gras! :lol:

You're right, you don't want to assign 2 values to the same variable. Remove that second line: $grade = $_POST['score'];. Now think about how you must determine the value of the letter grade: it is by comparing the number score to several numbers, in the if ... elseif section, right? That's where you must remove the lines like echo "C"; and replace with lines like $grade = "C";. Then you will have the calculated grade stored in the variable $grade, so after that you can use $grade wherever you need to add it to the string to be echoed. Try that.

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Sat Feb 04, 2012 3:57 pm
by djkazoo2002
Yes, finally I did,



<html>

<head>
</head>
<body>
<form name="input" action="grade.php" method="post">
Student Number:<input type="text" name="studentnumber"/> <br/>
Your Score:<input type="text" name="score"/> <br/>
<input type="submit" value="submit" />
</form>

</body>
</html>

and


<html>
<head>
</head>
<body>

<?php
$score = $_POST['score'];
$student_Number = $_POST['studentnumber'];


if($_POST['score'] > 89)
$grade = "A";
elseif($_POST['score'] > 79)
$grade = "B";
elseif($_POST['score'] > 69)
$grade = "C";
elseif($_POST['score'] > 59)
$grade = "D";
elseif ($_POST['score'] < 58)
$grade = "F";



$strg = $student_Number . "'s grade is" . " " .$grade." " . "because his score is" ." ". $score;

echo $strg;

?>

</body>
</html>

showed up right,

Thank you so much again, now I can go to Parade!!!! I do not know how much I spent for this assignment,
Tomorrow I will start doing,

Prompt user for his or her birth date.
Calculate the user’s age.
Display the user’s birth date and age

Probably, I will need your help, but I will try as much as I can.

Thank you so much again.

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Sat Feb 04, 2012 6:01 pm
by califdon
Glad to help, as always. Yes, now you have the idea. It gets easier each time. :-)

Enjoy Mardi Gras!

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Wed Feb 08, 2012 1:23 pm
by djkazoo2002
Prompt user for his or her birth date.
Calculate the user’s age.
Display the user’s birth date and age

so I did like this, after I googled it how to do that, It seems like that there are so many ways, but after I tested it around, I think this is the most easiest way,

<form name="input" action="birthday2.php" method="post">
when is your birthday? <br/>

<select name="BirthMonth">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="BirthDay">
<?php
for ($i=1; $i<=31; $i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<select name="BirthYear">
<?php
for ($i=2012; $i>=1900; $i--)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<input type="submit" value="submit" />



<?php
//he/she types Birthday
$uM = $_POST['BirthMonth'];
$uD = $_POST['BirthDay'];
$uY = $_POST['BirthYear'];

//Today's Date

$tM = date ("m");
$tD = date ("d");
$tY = date ("Y");



echo "His/her birthday is ".$uM.".".$uD.".".$uY.".";

echo "<br>";

echo "today's date is"." ".$tM.".".$tD.".".$tY.".";

echo "<br>";


if ($tM < $uM || ($tM == $uM && $tM < $uM))
{
echo $tY - $uY - 1;
}
else
echo $tY - $uY;

?>

however some people use "Function", like this

http://www.geekpedia.com/code38_Calcula ... g-PHP.html

it seems more complicated for me, Here are the questions

A. why do people use "function"? is there any advantages to use "function" on this question?
B. What is this Option Value? like "<option value='$i'>$i</option>";
C. what is ||? if ($tM < $uM || ($tM == $uM && $tM < $uM)),

I have a test next week, and I really want to practice more PHP more, so if anybody know website or anything, question like this

Prompt user for his or her birth date.
Calculate the user’s age.
Display the user’s birth date and age

please help me

Thanks




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: Wed Feb 08, 2012 2:13 pm
by califdon
djkazoo2002 wrote:Prompt user for his or her birth date.
Calculate the user’s age.
Display the user’s birth date and age

so I did like this, after I googled it how to do that, It seems like that there are so many ways, but after I tested it around, I think this is the most easiest way,

Here you need to test whether you want to display the form or calculate and display the results.
You could do this by testing whether there is a value for $_POST['BirthMonth'], for example.
Based on that, you can start an if ... else block here -- the first block is for when $_POST[/BirthMonth'] is empty:


<form name="input" action="birthday2.php" method="post">
when is your birthday? <br/>

<select name="BirthMonth">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="BirthDay">
<?php
for ($i=1; $i<=31; $i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<select name="BirthYear">
<?php
for ($i=2012; $i>=1900; $i--)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<input type="submit" value="submit" />

Here's where the first block ends and the else begins -- this is for when the user has filled in at least the BirthMonth:

<?php
//he/she types Birthday
$uM = $_POST['BirthMonth'];
$uD = $_POST['BirthDay'];
$uY = $_POST['BirthYear'];

//Today's Date

$tM = date ("m");
$tD = date ("d");
$tY = date ("Y");



echo "His/her birthday is ".$uM.".".$uD.".".$uY.".";

echo "<br>";

echo "today's date is"." ".$tM.".".$tD.".".$tY.".";

echo "<br>";

You have an error in the next line. What you want is if ($tM < $uM II ($tM <= $uM))

if ($tM < $uM || ($tM == $uM && $tM < $uM))
{
echo $tY - $uY - 1;
}
else
echo $tY - $uY;

Here's where the second block ends, so you must end the if block.:

?>

however some people use "Function", like this

http://www.geekpedia.com/code38_Calcula ... g-PHP.html

it seems more complicated for me, Here are the questions

A. why do people use "function"? is there any advantages to use "function" on this question?
A function is a way to clarify your code so that it is actually easier to program and MUCH easier for someone to read and understand, as well as be able to use the same function code many times, when that is required (not here). By keeping all the calculations together in the function, then just calling the function in the main part of your code, it makes the code much easier for anyone else or yourself to immediately see what the code is doing.
B. What is this Option Value? like "<option value='$i'>$i</option>";
This is HTML that creates a drop-down Select list lets the user select an option. See http://www.w3schools.com/tags/tag_select.asp. In this case, it will display whatever value is in $i and that's also the value that will be sent to the server in the $_POST array if that Option is selected. These 2 values don't have to be the same, see your month Select element, where it displays the names of the months, but sends the number of the month to the server.
C. what is ||? if ($tM < $uM || ($tM == $uM && $tM < $uM)),
II is the OR operator. Some languages use "OR", but PHP and Javascript use II. && is "AND".

I have a test next week, and I really want to practice more PHP more, so if anybody know website or anything, question like this.

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Wed Feb 08, 2012 8:47 pm
by djkazoo2002
Thank you so much again!!!! it's always best tutorial!!!! I can study a lot of things from here, and I think a lot of people are also learning too.

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Thu Feb 09, 2012 2:54 pm
by djkazoo2002
Since I have a test next week, while I am studying "Function", I try to do more practice for the test, even though this assignment is the last 2 of them in this semester.

 Prompt the user to enter 10 integers.
 Compute the average of the 10 integers.
 Count the number of integers entered that are greater than the average.
 Count the number of integers entered that are less than the average.
 Display the 10 integers, the average, the count greater and the count
less than the average.
 Sort the numbers in ascending sequence.
 Display the sorted numbers.
 Sort the numbers in descending sequence.
 Display the sorted numbers

so I did like this

<form name="input" action="hmwk72.php" method="post">
Please put your favorite numbers <br/>
1:<input type="text" name="num1"/> <br/>
2:<input type="text" name="num2"/> <br/>
3:<input type="text" name="num3"/> <br/>
4:<input type="text" name="num4"/> <br/>
5:<input type="text" name="num5"/> <br/>
6:<input type="text" name="num6"/> <br/>
7:<input type="text" name="num7"/> <br/>
8:<input type="text" name="num8"/> <br/>
9:<input type="text" name="num9"/> <br/>
10:<input type="text" name="num10"/> <br/>

<input type="submit" value="caluculate" />


and




<?php

$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$num4 = $_POST['num4'];
$num5 = $_POST['num5'];
$num6 = $_POST['num6'];
$num7 = $_POST['num7'];
$num8 = $_POST['num8'];
$num9 = $_POST['num9'];
$num10 = $_POST['num10'];

$average = ($num1 + $num2 +$num3 + $num4 +$num5 +$num6 +$num7 + $num8 + $num9 + $num10)/10;

c

echo ($num1 + $num2 +$num3 + $num4 +$num5 +$num6 +$num7 + $num8 + $num9 + $num10)/10;

?>


and I am stuck at  Count the number of integers entered that are greater than the average.


Q1,I tried to echo out the number or the numbers > $average, but I do not know how to write it right on php.
For example. $num1 , if I put 10, $num7 if I put 14 >if $average is 7 and displays 10,14

I did like this
if ($num1,$num2,$num3,$num4,$num5,$num6,$num7,$num8,$num9,$num10 > $average)
{
echo I do not know hot to write here;
}

or

echo ($num1,$num2,$num3,$num4,$num5,$num6,$num7,$num8,$num9,$num10 > $average);

or

if($num1,$num2,num3,num4,num5,num6,num7,num8,num9,num10 >$average)
{
$greaterthan = ($num1,$num2,num3,num4,num5,num6,num7,num8,num9,num10 >$average)
echo $greaterthan;
this was error...

Q2, after echoing out, I want to count, like assignment says "Count the number of integers entered that are greater than the average",
how can I count those and display?

Thank you so much

Re: I am really new to Php, I wonde if somebody can show me,

Posted: Sat Feb 11, 2012 3:26 pm
by califdon
As with nearly all programming tasks, there are several ways it can be done. An easy way would be to use an array instead of separate variables to store the 10 numbers the user inputs. In this way, you can write a simple foreach() loop (this might help: http://www.java2s.com/Code/Php/Data-Str ... oreach.htm) in which you loop through the array, comparing each value in the array to the average that you have previously calculated, adding 1 to a counter variable for each one that is larger than the average. This also has the advantage that if you wanted to do a similar thing with maybe only 5 numbers, or maybe 20 numbers, the foreach() loop would not require any changes, it automatically loops through all the members of the array.