silentfury45 Posted February 15, 2015 Share Posted February 15, 2015 Hey I'm working on a simple program that will take user input from a HTTP form and use that to calculate a few things. I a few problems with my counter and I was wandering if anyone could point out something that could be causing my problem? Also if anyone know how to properly get a count on the positive even int's??? This is what I'm working with. $count = 0;$PosCount = 0; $uname = array($uname1, $uname2, $uname3, $uname4, $uname5, $uname6, $uname7, $uname8); for ($i; $i < count($uname); ++$i) {if ($uname[$i] == '0' || $uname[$i] == null) { //look to see weather its good or badunset ($uname[$i]); //break that empty field$count = ($count - 1);} $count = $count + 1; if ($uname[$i] % 2 == 0 ) {//Even check$i = $uname[$i];if ($i == round($i) || $i < 1) { //Pos Check$PosCount = $PosCount + 1; } }} than of course echoing all my desired values (sum, avg, max, min, and pos int count) - Thanks Quote Link to comment Share on other sites More sharing options...
silentfury45 Posted February 15, 2015 Author Share Posted February 15, 2015 I'm not sure if I'm using the array the right way. When I enter text into number1field but nothing else it shows the count was 4 when it should be 1. The min sometimes doesnt show up too. Nothing shows up for my even check and pos check.Am i using the array functions correctly.? Quote Link to comment Share on other sites More sharing options...
Stew_822 Posted February 15, 2015 Share Posted February 15, 2015 (edited) G'day mate Could you re-paste your code using the <> button? It's hard to read without formatting. Try to narrow the problem down as much as possible. Is your user input coming in as strings, and you're trying to use them as numbers? You can check using a var_dump. What are you trying to do? If you're trying to count the number of positive, even integers, you could do something like: $array = array( 1, -2, 3, 4, 5, 6.5 ); $positive_even_integers = array(); foreach( $array as $number ) { if( is_int( $number ) and $number >= 0 and $number % 2 == 0 ) { $positive_even_integers[] = $number; } } echo count( $positive_even_integers ); // should output 3 I find foreach simpler than using for, and also copying the elements from your original array to another array might be easier than deleting the elements from the original array. Then, you can just count the number of elements in the final array. When testing your code for bugs, assume the bug could be anywhere. If you're not sure if the array coming from your user input is valid, then overwrite it with an array of values that you know is legitimate. Then you might find your code will work, and you'll know the problem lies within your user input code, which is then where you would start looking. Edited February 15, 2015 by Stew_822 Quote Link to comment Share on other sites More sharing options...
silentfury45 Posted February 16, 2015 Author Share Posted February 16, 2015 (edited) $count = 0; $PosCount = 0; $uname = array($uname1, $uname2, $uname3, $uname4, $uname5, $uname6, $uname7, $uname8); for ($i; $i < count($uname); ++$i) { if ($uname[$i] == '0' || $uname[$i] == null) { //look to see weather its good or bad unset ($uname[$i]); //break that empty field $count = ($count - 1); } $count = $count + 1; if ($uname[$i] % 2 == 0 ) {//Even check $i = $uname[$i]; if ($i == round($i) || $i < 1) { //Pos Check $PosCount = $PosCount + 1; } } } Okay so I've replaced my pos even check with something similar to what you showed me but I'm still getting an error. I'll need to work on it a little more and if I'm still having problems ill post the new updated code on this thread. Thanks for the help! Edited February 16, 2015 by silentfury45 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 17, 2015 Share Posted February 17, 2015 Is PHP set up to display all errors and warnings? To do that, you can add the following to the top of your script: <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> Of course, you'll want to remove this code when you are done debugging. In the following for loop, it looks like $i is undefined: for ($i; $i < count($uname); ++$i) { Maybe try something like this: for ($i=0; $i < count($uname); ++$i) { Also, it looks like you're overwriting $i here: $i = $uname[$i]; Is that being done on purpose? 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.