sgalatas Posted November 12, 2007 Share Posted November 12, 2007 Hello Again, I am writing a program that is supposed to accept input from a form (sports stats), verify the input, keep accepting from the form until complete, use the screen input to create an array, and then finally display the mean, mode, and median of the array. I have taken some of your advice in previous threads. However, I'm not sure that I followed it correctly. The program is still not working. Can someone take a look at it and let me know what's wrong and how to correct it? Please don;t be too harsh on me. I'm a beginner. Here is what I have: [<html> <head> </head> <body> <h1 style="text-align: center; color: navy" >Sports Stats</h1> <form> <fieldset> <label for="stat">Enter Stats (Separate each stats using a comma.)</label> <input type= "text" name= "stat" id="stat" size="5" /> </fieldset> <!----> <fieldset> <input type= "submit" name= "submit" id="submit" value= "Submit" /> <input type="reset" name="reset" value="Cancel" /> </fieldset> </form> <?php /** * Author: Sherrie Galatas * Date: November 15, 2007 * * Determine the Mean, Mode, and Median of the Array */ $max = 5; $statArray = array(""); $stat = $_GET['stat']; //echo " $stat <br />"; if ($i = 0; $i <= 5; $i++) if (is_numeric($$_GET['stat']) { $stat = $_GET['stat']; $stat = $statArray($stat); foreach ($stat as $key => $value) { echo "stat ($key + 1) = $value<br />"; } } sort($statArray); $mean= array_sum($stat) / count($stat); $median= $statArray[2]; function mode($data){ $temp = array_count_values($data); asort($temp); $mode = array(); $mode['count'] = current($temp); $mode['value'] = ket($temp); return $mode; } $mode = mode($array); $mode_value = $mode['value']; $mode_count = $mode['count']; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
revraz Posted November 12, 2007 Share Posted November 12, 2007 FYI, most people will put the PHP code before the HTML, only because once you start getting into Headers and Sessions, that code needs to come first. Review some FORMS threads and see how others use ISSET on SUBMIT to see if the form has been submitted yet, and if it has to process it and if it hasn't to create it. Also, I don't see your FORM ACTION or METHOD anywhere in your HTML. Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 12, 2007 Author Share Posted November 12, 2007 When I put the form action and method in, it gives me an option to open the actual php file. When I hit open, it opens the php code. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 12, 2007 Share Posted November 12, 2007 Sounds like you don't have .php installed on your webserver then. Make a small test.php file, put: <?php phpinfo(); ?> And run the test.php and see if you get a PHP info sheet. If it just tries to save the file to your PC, you don't have PHP installed or .htaccess isn't setup to use the extension. Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 13, 2007 Author Share Posted November 13, 2007 I get the following PHP Version 5.2.3 System Windows NT OFFICE 5.1 build 2600 Build Date May 31 2007 09:36:39 Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared" Server API CGI/FastCGI Virtual Directory Support enabled Configuration File (php.ini) Path C:\WINDOWS Loaded Configuration File C:\Program Files\PHP\php.ini PHP API 20041225 PHP Extension 20060613 Zend Extension 220060519 Debug Build no Thread Safety enabled Zend Memory Manager enabled IPv6 Support enabled Registered PHP Streams php, file, data, http, ftp, compress.zlib Registered Stream Socket Transports tcp, udp Registered Stream Filters convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, zlib.* This program makes use of the Zend Scripting Language Engine: Zend Engine v2.2.0, Copyright © 1998-2007 Zend Technologies Quote Link to comment Share on other sites More sharing options...
gin Posted November 13, 2007 Share Posted November 13, 2007 Your code is riddled with basic errors. You really need some kind of text editor with color formatting. I suggest Notepad++. Now, as for your code, the two lines you should look at: <?php //echo " $stat "; //"; what's this? the // comment only comments one line if ($i = 0; $i <= 5; $i++) // and this? this is either a for loop or should be commented out. ?> There are probably more, but this should start you out. Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 14, 2007 Author Share Posted November 14, 2007 Oops! I put the comment marks on every line after the form to see what exactly was working. I also made a few changes. I know the form works. I'm actully using phpDeigner 2007 Professional. When I debug & Run, I get a message stating that line 33 has undefined index: stat . Line 33 is $statstring = $_GET['stat]; { <form method="get" action=""> <fieldset> <label for="stat">Enter Stats</label> <input type= "text" name= "stat[]" size="5" />| <input type= "text" name= "stat[]" size="5" />| <input type= "text" name= "stat[]" size="5" />| <input type= "text" name= "stat[]" size="5" />| <input type= "text" name= "stat[]" size="5" />| <input type= "text" name= "stat[]" size="5" />| <input type= "submit" name= "submit" value= "Submit" /> <input type="reset" name="reset" value="Cancel" /> </fieldset> </form> <?php /** * Author: Sherrie Galatas * Date: November 15, 2007 * * Determine the Mean, Mode, and Median of the Array */ print_r($_GET); $statstring = $_GET['stat'];{ }echo "$statstring"; foreach ($stat as $key => $value) { echo "stat ($key + 1) = $value<br />"; } } sort($statArray); $mean= array_sum($stat) / count($stat); $median= $statArray[2]; function mode($data){ $temp = array_count_values($data); asort($temp); $mode = array(); $mode['count'] = current($temp); $mode['value'] = ket($temp); return $mode; } $mode = mode($array); $mode_value = $mode['value']; $mode_count = $mode['count']; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 That's because the first time you run your script (before submitting your form) the variable $_GET['stat'] doesn't exist. Hence your error. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 14, 2007 Share Posted November 14, 2007 Which is why most people check ISSET first Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 Hehe, yes indeed Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 14, 2007 Author Share Posted November 14, 2007 Ok, I've made a few more changes and I finally got the program to work with the exception of fgetting the mode. It's currently giving the mean and median. But I still can not get it to calculate the mode. What am I missing? <html> <head> </head> <body> <!-- Creating form --> <h1 style="text-align: center; color: navy" >Sports Stats</h1> <form method="get" action=""> <fieldset> <label for="stat">Enter Stats</label> <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "submit" name= "submit" value= "Submit" /> <input type="reset" name="reset" value="Cancel" /> </fieldset> </form> <?php /** * Author: Sherrie Galatas * Date: November 15, 2007 * * Determine the Mean, Mode, and Median of the Array */ // Aceppting input from form// $sstring = $_GET['stats'];{ }//echo "<p>Stats:$sstring</p>"; foreach ($sstring as $key => $value) { if ($value ==""){ unset($sstring[$key]); } } $null = array_values($sstring); $del = ""; $valstring = implode($del,$sstring); //echo $valstring; //verifying input if (is_numeric($valstring) == false) { echo "<p>Invalid entries, please re-enter.</p>"; } //End if $statchk = str_split($valstring); if (in_array("-", $statchk)) { echo "<p> Invalid entries, no negative numbers accepted, please try again.</p>"; }// End if $calstring = implode(",",$null); $statArray = explode(",",$calstring); sort($statArray); //Mean calculation $sum = array_sum($statArray); $ct = count($statArray); $mean = ($sum / $ct); $meanformat = number_format($mean); echo "<p> Mean: $meanformat yards</p>"; //Median calculation $md = intval($ct / 2); if ($ct % 2 == 0){ $median = ($statArray[$md] + $statArray[$md - 1]) / 2; } else { $median = $statArray[$md]; } $medianformat = number_format($median); echo "<p>Median: $medianformat yards</p>"; //Mode calculation asort($statArray); $mode['$ct'] = current($statArray); $mode['value'] = key($statArray); return $mode; $modeformat = number_format($mode); echo "<p> Mode: $modeformat yards</p>"?> </body> </html> Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 28, 2007 Author Share Posted November 28, 2007 Hey, I finally got it to work. I am posting the final program in case anyone else may need help with this kind of program. <html> <head> </head> <body> <!-- Creating form --> <h1 style="text-align: center; color: navy" >Sports Stats</h1> <form method="get" action=""> <fieldset> <label for="stat">Enter Stats</label> <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "text" name= "stats[]" size="5" />| <input type= "submit" name= "submit" value= "Submit" /> <input type="reset" name="reset" value="Cancel" /> </fieldset> </form> <?php /** * Author: Sherrie Galatas * Date: November 15, 2007 * * Determine the Mean, Mode, and Median of the Array */ // Aceppting input from form// //Created string $sstring = $_GET['stats'];{ }//echo "<p>Stats:$sstring</p>"; foreach ($sstring as $key => $value) { if ($value ==""){ unset($sstring[$key]); } }//creating array $null = array_values($sstring); $del = ""; $valstring = implode($del,$sstring); //echo $valstring; //verifying input if (is_numeric($valstring) == false) { //echo "<p>Invalid entries, please re-enter.</p>"; } //End if $statchk = str_split($valstring); if (in_array("-", $statchk)) { echo "<p> Invalid entries, no negative numbers accepted, please try again.</p>"; }// End if $calstring = implode(",",$null); $statArray = explode(",",$calstring); sort($statArray); //Mean calculation $sum = array_sum($statArray); $ct = count($statArray); $mean = ($sum / $ct); echo "<p> Mean: $mean</p>"; //Median calculation $md = intval($ct / 2); if ($ct % 2 == 0){ $median = ($statArray[$md] + $statArray[$md - 1]) / 2; } else { $median = $statArray[$md]; } echo "<p>Median: $median</p>"; //Mode calculation if($valstring != NULL) { $modect = array_count_values($statArray); arsort($modect); $modeKeys = array_keys($modect); $modeValues = array_values($modect); If ($modeValues[0] != null) { If ($modeValues[9] != null && ($modeValues[0] == $modeValues[9])) { echo "<p>Mode: None</p>"; //echo "<p>Mode: //$modeKeys[0], $modeKeys[1], $modeKeys[2], //$modeKeys[3], $modeKeys[4], $modeKeys[5], //$modeKeys[6], $modeKeys[7], $modeKeys[8] and //$modeKeys[9].</p>"; break; } // End If If ($modeValues[8] != null && ($modeValues[0] == $modeValues[8])) { echo "<p>Mode: $modeKeys[0], $modeKeys[1], $modeKeys[2], $modeKeys[3], $modeKeys[4], $modeKeys[5], $modeKeys[6], $modeKeys[7] and $modeKeys[8].</p>"; break; } // End If If ($modeValues[7] != null && ($modeValues[0] == $modeValues[7])) { echo "<p>Mode: $modeKeys[0], $modeKeys[1], $modeKeys[2], $modeKeys[3], $modeKeys[4], $modeKeys[5], $modeKeys[6] and $modeKeys[7].</p>"; break; } // End If If ($modeValues[6] != null && ($modeValues[0] == $modeValues[6])) { echo "<p>Mode $modeKeys[0], $modeKeys[1], $modeKeys[2], $modeKeys[3], $modeKeys[4], $modeKeys[5] and $modeKeys[6].</p>"; break; } // End If If ($modeValues[5] != null && ($modeValues[0] == $modeValues[5])) { echo "<p>Mode: $modeKeys[0], $modeKeys[1], $modeKeys[2], $modeKeys[3], $modeKeys[4] and $modeKeys[5].</p>"; break; } // End If If ($modeValues[4] != null && ($modeValues[0] == $modeValues[4])) { echo "<p>Mode: $modeKeys[0], $modeKeys[1], $modeKeys[2], $modeKeys[3] and $modeKeys[4].</p>"; break; } // End If If ($modeValues[3] != null && ($modeValues[0] == $modeValues[3])) { echo "<p>Mode: $modeKeys[0], $modeKeys[1], $modeKeys[2] and $modeKeys[3].</p>"; break; } // End If If ($modeValues[2] != null && ($modeValues[0] == $modeValues[2])) { echo "<p>Mode: $modeKeys[0], $modeKeys[1] and $modeKeys[2].</p>"; break; } // End If If ($modeValues[1] != null && ($modeValues[0] == $modeValues[1])) { echo "<p>Mode: $modeKeys[0] and $modeKeys[1].</p>"; break; } // End If If ($modeValues[0] != null && ($modeValues[0] != $modeValues[1])) { echo "<p>Mode: $modeKeys[0].</p>"; break; } // End If } // End If } // End If // } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.