Jump to content

Need Help with simple PHP program


silentfury45

Recommended Posts

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 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;
        }
    }

}
 

than of course echoing all my desired values (sum, avg, max, min, and pos int count)

 

- Thanks

Link to comment
Share on other sites

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 by Stew_822
Link to comment
Share on other sites

$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 by silentfury45
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.