Page 2 of 2

Last echo statement doesn't display

Posted: Tue Mar 20, 2007 12:16 pm
by pendragon
Thanks for the explanation. I need to buy some better PHP books that do a better job of explaining validation concepts.


After the final else statement in the above code, why doesn't the line "There is nothing to display" actually display below the table header if there are no entries submitted by the user in the form? I've looked for missing or misplaced brackets but can't find any.

Posted: Tue Mar 20, 2007 12:36 pm
by RobertGonzalez
The way that code was working, it always resulted in a 5 count array. So there would always be something to display. Play with this code and see if works a little different. Again, this is not production ready code, but it can be a little lesson type code... ;)

Code: Select all

<html>
<head>
<title>Test to get variables from form</title>
</head>
<body>
<table width="90%" border="1" cellspacing="5" cellpadding="0">
<tr><th>Course</th><th>Units</th><th>Letter Grade</th></tr>
<?php
if (!empty($_POST))
{
    for ($c = 0; $c < 5; $c++)
    {
        $course = isset($_POST['course' . $c]) ? $_POST['course' . $c] : '&nbsp;';
        $units = isset($_POST['units' . $c]) ? $_POST['units' . $c] : '&nbsp;';
        $grade = isset($_POST['letterGrade' . $c]) ? $_POST['letterGrade' . $c] : '&nbsp;';
        
        echo '<tr>';
        echo '<td>' . $course . '</td>';
        echo '<td>' . $units . '</td>';
        echo '<td>' . $grade . '</td>';
        echo '</tr>';
    }
}   
else
{
    echo '<tr><td colspan="3">There is nothing to display</td></tr>';
}
?>
</table>
</body>
</html>

Thanks

Posted: Tue Mar 20, 2007 12:43 pm
by pendragon
I'm a student taking a beginning PHP class so everything is a "lesson" for me.
I'll play around with the code a bit and see if I can get it to work.

Trying to learn to be "lean and mean" in my code.

Thanks.

Back for another lesson

Posted: Tue Mar 20, 2007 3:11 pm
by pendragon
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

Sorry to come back here so many times but I'm obviously missing the "concept."
I've been working on a program as follows and can get values returned from the 
original form now (thanks to your help).  I also can successfully show values on 
my results table from my Function calculateQualityPoints.

However I want to print the results from Function calculateGPA on a final table row
of my second page but cannot get anything to print. Did I screw up the print statement, mess up
my brackets or am I missing the concept of passing by reference in the functions? 

Could you take a look at my code and indicate where I'm off?

Thanks - I appreciate any help.

Code: Select all

<?php

/** 
 
 *  This program consists of a form (form_semesterGPA.php) that allows the
 *  user to enter the classes they took during a semester.  Up to 5 course titles, 
 *  units, and final letter grades can be entered in the form.
 *  
 *  A separate PHP page (script_semesterGPA.php) will process the form.  This page
 *  calculates the student's GPA for the semester.
 *
 *  Steps to calculate the semester GPA:
 *     1.  Conversion of the letter grade entered for a course into a numeric
 *          equivalent with A = 4 points, B = 3 points, C = 2 points, D = 1 point,
 *          F = 0 points.
 *     2.  The point value of the letter grade is multipled by the number of units
 *          for the course to obtain a value called "quality points."
 *     3.  In order to get the GPA, the quality points for all courses are added up
 *           and then divided by the total number of units.
 *   
 *  The solution is to include a function called calculateQualityPoints which should
 *  take a letter grade and a number of units, and return the quality points for that
 *  course.
 *  
 *  The page should calculate GPA correctly irregardless of how many courses and info
 *  are entered.
 */
 
	
?> 

<?php 
// Set up array holder 
$form_info = array(); 

// Set default count for the data 
$count = 0; 

// Function used to calculate the overall semester GPA = totalofQualityPoints/totalUnits
   Function calculateGPA ($grades, $units);	
   $sum = 0;
   $size = count ($grades);
   if ( $size == 0 ) {
       echo "Empty parameter list<br />";
	   exit (); }
       for ($i = 0; $i < $size; $i++){
         $gpaSum += $grades[$i];
         $unitsSum += $units[$i];
	   }
	   return $gpaSum/ $unitsSum;
	}   
   
// Function calculateQualityPoints - takes a letter grade and a number of units &
//   returns the quality points for that course
	   function calculateQualityPoints($grades, $units)
	   {
         if($grades == "a" || $grades == "A"){ 
            $classPts = 4 * $units;
			$gpaSum += $classPts;
			$unitsSum += $units;
		    echo '<td><center>' . $classPts. '</center></td>';
			return ($gpaSum, $unitsSum); 
		 }
         if($grades == 'b' || $grades == 'B'){
            $classPts =  3 * $units;
			 $gpaSum += $classPts;
			 $unitsSum += $units;
			 echo '<td><center>' . $classPts. '</center></td>';
			 return ($gpaSum, $unitsSum);  
		 }
         if($grades == 'c' || $grades == 'C'){
		    $classPts =  2 * $units;
			 $gpaSum += $classPts;
			 $unitsSum += $units;
			 echo '<td><center>' . $classPts. '</center></td>'; 
			 return ($gpaSum, $unitsSum); 
		 }
         if($grades == 'd' || $grades == 'D'){
            $classPts =  1 * $units;
			 $gpaSum += $classPts;
			 $unitsSum += $units;
			 echo '<td><center>' . $classPts. '</center></td>'; 
			 return ($gpaSum, $unitsSum); 
		 }
         if($grades == 'f' || $grades == 'F'){
           $classPts =  0 * $units;
		    $gpaSum += $classPts;
			$unitsSum += $units;
		    echo '<td><center>' . $classPts. '</center></td>'; 
			return ($gpaSum, $unitsSum); 
         }
		}
//  Check to see if data is submitted
   
if (!empty($_POST)) 
{ 
    for ($c = 0; $c < 5; $c++) 
    { 
        $form_info[$c]['course'] = $_POST['course' . $c] ; 
        $form_info[$c]['units'] = $_POST['units' . $c] ;
	$form_info[$c]['grade'] = $_POST['letterGrade' . $c] ; 
    } 
    
    $count = count($form_info); 
	
   
?> 
<html> 
<head> 
<title>Test to get variables from form</title> 
</head> 
<body> 
<table width="90%" border="1" cellpadding="0" cellspacing="5" > 
<tr bgcolor=#FFCC99><th>Course</th><th>Units</th><th>Letter Grade</th><th>Quality Points</th></tr> 
<?php 


    for ($i = 0; $i < $count; $i++) 
    { 
        echo '<tr>'; 
        echo '<td><center>' . $form_info[$i]['course'] . '</center></td>'; 
        echo '<td><center>' . $form_info[$i]['units'] . '</center></td>';
		$units = $_POST['units' . $i] ; 
		echo '<td><center>' . $form_info[$i]['grade'] . '</center></td>'; 
		$grades = $_POST['letterGrade' . $i] ;
		calculateQualityPoints($grades, $units);
        echo '</tr>'; 
		$result = calculateGPA($grades, $units);
    } 
	    echo '<tr><td colspan="4"><center>' Your semester GPA is . $result . '</center></td></tr>';
} 
else 
{ 
   
	echo "<tr bgcolor='#FFFFCC'><td colspan="4"><center><h3> There is nothing to display </h3></center></td></tr>"; 
} 
}

?> 
</table> 
</body> 
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Whoops - a correction to one function

Posted: Tue Mar 20, 2007 3:21 pm
by pendragon
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


whoops!  

Here's a correction to my code.  I took out 3 lines from function calculateQualityPoints and placed them in a separate function so no
longer need them.  Sorry.

Code: Select all

<?php

/** 
 
 *  This program consists of a form (form_semesterGPA.php) that allows the
 *  user to enter the classes they took during a semester.  Up to 5 course titles, 
 *  units, and final letter grades can be entered in the form.
 *  
 *  A separate PHP page (script_semesterGPA.php) will process the form.  This page
 *  calculates the student's GPA for the semester.
 *
 *  Steps to calculate the semester GPA:
 *     1.  Conversion of the letter grade entered for a course into a numeric
 *          equivalent with A = 4 points, B = 3 points, C = 2 points, D = 1 point,
 *          F = 0 points.
 *     2.  The point value of the letter grade is multipled by the number of units
 *          for the course to obtain a value called "quality points."
 *     3.  In order to get the GPA, the quality points for all courses are added up
 *           and then divided by the total number of units.
 *   
 *  The solution is to include a function called calculateQualityPoints which should
 *  take a letter grade and a number of units, and return the quality points for that
 *  course.
 *  
 *  The page should calculate GPA correctly irregardless of how many courses and info
 *  are entered.
 */
 
	
?> 

<?php 
// Set up array holder 
$form_info = array(); 

// Set default count for the data 
$count = 0; 

// Function used to calculate the overall semester GPA = totalofQualityPoints/totalUnits
   function calculateGPA ($grades, $units);	
   $sum = 0;
   $size = count ($grades);
   if ( $size == 0 ) {
       echo "Empty parameter list<br />";
	   exit (); }
       for ($i = 0; $i < $size; $i++){
         $gpaSum += $grades[$i];
         $unitsSum += $units[$i];
	   }
	   return $gpaSum/ $unitsSum;
	}   
   
// Function calculateQualityPoints - takes a letter grade and a number of units &
//   returns the quality points for that course
	   function calculateQualityPoints($grades, $units)
	   {
         if($grades == "a" || $grades == "A"){ 
            $classPts = 4 * $units;
			echo '<td><center>' . $classPts. '</center></td>';
	   }
         if($grades == 'b' || $grades == 'B'){
            $classPts =  3 * $units;
		    echo '<td><center>' . $classPts. '</center></td>';
			 
		 }
         if($grades == 'c' || $grades == 'C'){
		    $classPts =  2 * $units;
			 echo '<td><center>' . $classPts. '</center></td>'; 
			 
		 }
         if($grades == 'd' || $grades == 'D'){
            $classPts =  1 * $units;
			 echo '<td><center>' . $classPts. '</center></td>'; 
			  
		 }
         if($grades == 'f' || $grades == 'F'){
           $classPts =  0 * $units;
		    echo '<td><center>' . $classPts. '</center></td>'; 
		  }
		}
//  Check to see if data is submitted
   
if (!empty($_POST)) 
{ 
    for ($c = 0; $c < 5; $c++) 
    { 
        $form_info[$c]['course'] = $_POST['course' . $c] ; 
        $form_info[$c]['units'] = $_POST['units' . $c] ;
		$form_info[$c]['grade'] = $_POST['letterGrade' . $c] ; 
	 } 
    
    $count = count($form_info); 
	
   
?> 
<html> 
<head> 
<title>Test to get variables from form</title> 
</head> 
<body> 
<table width="90%" border="1" cellpadding="0" cellspacing="5" > 
<tr bgcolor=#FFCC99><th>Course</th><th>Units</th><th>Letter Grade</th><th>Quality Points</th></tr> 
<?php 


    for ($i = 0; $i < $count; $i++) 
    { 
        echo '<tr>'; 
        echo '<td><center>' . $form_info[$i]['course'] . '</center></td>'; 
        echo '<td><center>' . $form_info[$i]['units'] . '</center></td>';
		$units = $_POST['units' . $i] ; 
		echo '<td><center>' . $form_info[$i]['grade'] . '</center></td>'; 
		$grades = $_POST['letterGrade' . $i] ;
		calculateQualityPoints($grades, $units);
        echo '</tr>'; 
		$result = calculateGPA($grades, $units);
	 } 
	    echo '<tr><td colspan="4"><center>' Your semester GPA is . $result . '</center></td></tr>';
} 
else 
{ 
   
	echo "<tr bgcolor='#FFFFCC'><td colspan="4"><center><h3> There is nothing to display </h3></center></td></tr>"; 
} 
}

?> 
</table> 
</body> 
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]