Increments and Additions not working properly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
originaluser
Forum Newbie
Posts: 6
Joined: Fri Feb 20, 2009 4:04 am

Increments and Additions not working properly

Post by originaluser »

Code: Select all

 
if ( $nrolled >= 1 &&  $nrolled <= 12)
    {
        echo "1st Doz";
        echo "<br>Doz1 = " . $doz1 . "<br />";
        $doz1++;
        echo "<br>Doz1 = " . $doz1 . "<br />";
    }
 
Hey everyone. I feel a bit silly for posting this but I have no idea why it's acting this way.
When I run the above script $doz1 does not get increased, it's value remains 0.
I also tried doing $doz1 += 1 and it still returns 0.

When I do $doz1 = $doz1 + 1; the value goes from 0 to 1
However it needs to run several times and when I run it again the value goes from 1 to 11 (instead of 2).

Any help would be appreciated.
nmreddy
Forum Commoner
Posts: 25
Joined: Wed Feb 18, 2009 5:36 am

Re: Increments and Additions not working properly

Post by nmreddy »

first u have to intilaise $doz1 value like $doz1=0 .

you have to give $nrolled value also for loop iteration ...
originaluser
Forum Newbie
Posts: 6
Joined: Fri Feb 20, 2009 4:04 am

Re: Increments and Additions not working properly

Post by originaluser »

Maybe more of the code will give you a better idea.
$doz1 gets set by reading a text file where the value is "0".

Code: Select all

 
$nrolled = $_REQUEST["number"];
$content = file('data.txt');
$doz1 = $content[43];
$doz2 = $content[44];
$doz3 = $content[45];
 
    function chdoz()
        {
        global $doz1;
        global $doz2;
        global $doz3;
        global $nrolled;
        
        if ( $nrolled < 1) { echo "Zero"; }
 
        if ($nrolled >= 1 &&  $nrolled <= 12)
            {
            echo "1st Doz";
            echo "<br>Doz1 = " . $doz1 . "<br />";
            $doz1 = $doz1 + 1;
            echo "<br>Doz1 = " . $doz1 . "<br />";
            }
            
        elseif ($nrolled >= 1 && $nrolled <= 24)
        {
            echo "2nd Doz";
            $doz2++;
            echo $doz2;
        } elseif ($nrolled >= 1 && $nrolled <= 36)
            {
            echo "3rd Doz";
            $doz3++;
            }
        }
nmreddy
Forum Commoner
Posts: 25
Joined: Wed Feb 18, 2009 5:36 am

Re: Increments and Additions not working properly

Post by nmreddy »

you have call that function like $var = chdoz();

now you can use $doz1++
originaluser
Forum Newbie
Posts: 6
Joined: Fri Feb 20, 2009 4:04 am

Re: Increments and Additions not working properly

Post by originaluser »

Thanks for your replies.

OK, I've done:

Code: Select all

$vchdoz = chdoz($doz1, $doz2, $doz3, $nrolled);
But $doz1 still ouputs the value "0" when $doz++ is used.
$doz1 += 1 and $doz1 = $doz1 +1 still also does not work.

Doesn't seem to make a difference if a pass parameters to the function or whether I do 'global $doz1'.
nmreddy
Forum Commoner
Posts: 25
Joined: Wed Feb 18, 2009 5:36 am

Re: Increments and Additions not working properly

Post by nmreddy »

passing parameters is good idea,

here $vchdoz = chdoz($doz1, $doz2, $doz3, $nrolled); should be placed in a loop

because in $vchdoz only one time the function is calling

if you want to increment , then you have to call the function chdoz number of times based on your $nrolled vlaue .

one more thing you are doing wrong in looping , below is correct

if ( $nrolled < 1){
echo "Zero";
}
elseif ($nrolled >= 1 && $nrolled <= 12){
echo "1st Doz";
echo "<br>Doz1 = " . $doz1 . "<br />";
$doz1 = $doz1 + 1;
echo "<br>Doz1 = " . $doz1 . "<br />";
}
elseif ($nrolled >12 && $nrolled <= 24){
echo "2nd Doz";
$doz2++;
echo $doz2;
}
elseif($nrolled >24 && $nrolled <= 36){
echo "3rd Doz";
$doz3++;
}
originaluser
Forum Newbie
Posts: 6
Joined: Fri Feb 20, 2009 4:04 am

Re: Increments and Additions not working properly

Post by originaluser »

I've done eveyrthing you have suggested but $doz1++ still does not work regardless of how many times it's called or how the statement is written out.

The increment works in other places like in for loops for example.

Code: Select all

 
                for($z=1; $z<=$nrolled; $z++)
                {
                $doz1++;
                echo "lol";
                }
 
This will output "lollollollollollollollollollol" but the value of $doz1 still = "0"

I don't get it!!
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Increments and Additions not working properly

Post by papa »

Did you do as suggested ?

Code: Select all

 
$doz1 = 0;
for($z=1; $z<=$nrolled; $z++)
{
echo $doz1;
$doz1++;
}
originaluser
Forum Newbie
Posts: 6
Joined: Fri Feb 20, 2009 4:04 am

Re: Increments and Additions not working properly

Post by originaluser »

I cannot do $doz1 = 0 each time the function is called. This is the only problem.

The value of $doz1 is read from a text file. The value of $doz1 maybe be 5 for example. So when the script is run once, it's value is to increase to 6. Next time the script is run $doz1 must be equal to '6'.

If I reset the variable to 0 each time I cannot do this. Do you know what I mean?

Code: Select all

 
$content = file('data.txt');
$doz1 = $content[43];
$doz2 = $content[44];
$doz3 = $content[45];
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Increments and Additions not working properly

Post by papa »

Code: Select all

 
<?php
$nrolled = $_REQUEST["number"];
$content = file('data.txt');
$doz1 = $content[43];
$doz2 = $content[44];
$doz3 = $content[45];
 
if (empty($nrolled)) { 
    echo "Zero"; 
} else {
           if ($nrolled >= 1 &&  $nrolled <= 12) {
           echo "1st Doz";
           echo "<br>Doz1 = " . $doz1 . "<br />";
           $doz1++;
           echo "<br>Doz1 = " . $doz1 . "<br />";
           
           } elseif ($nrolled >= 1 && $nrolled <= 24) {
           echo "2nd Doz";
           $doz2++;
           echo $doz2;
           
        } elseif ($nrolled >= 1 && $nrolled <= 36) {
        echo "3rd Doz";
        $doz3++;
        }
}
?>
 
This code works for me so make sure you extract the correct values from the file.
originaluser
Forum Newbie
Posts: 6
Joined: Fri Feb 20, 2009 4:04 am

Re: Increments and Additions not working properly

Post by originaluser »

That's what I've been trying to do for the last while. I'm outputting the data after it's loaded into the variables and then outputting the variables before they go back into the text file and they seem to come out the text file ok.
Post Reply