Marlon
Marlon

Reputation: 111

How do I auto increase a number in increments of one using an IF statement?

I am trying to echo a variable number, but I am not sure the best way to go about it. My problem is this:

I have a series of 'IF' conditions and I echo a variable accordingly.

   if ($month == "8" || $month == "9" || $month == "10") {
    $comingseason   = 'winter';
    $elementsDue    = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "11" || $month == "12" || $month == "1") {
    $comingseason   = 'spring';
    $elementsDue    = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "2" || $month == "3" || $month == "4") {
    $comingseason   = 'summer';
    $elementsDue    = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "5" || $month == "6" || $month == "7") {
    $comingseason   = 'fall';
    $elementsDue    = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. 9";
}

I am wondering how I would replace the number in 'issue No. 9' with the next increment ++1 to output '10'? I don't want to have to come back into the code each time and type in the next issue number. I would like the number to increase on its own, essentially every quarter like you see below.

I was thinking of a php session, but from what I understand that will not be permanent. I need the number to remain even after a user has ended their session.

How can I accomplish this?

Upvotes: 0

Views: 113

Answers (4)

Sharp Shinoda
Sharp Shinoda

Reputation: 11

may be you're looking for this,

Explanation: add an integer variable say it is $issuNo, and than add a initial value of that. Then after if/else condition block put the incremental var of that like $issuNo++ and then you better make a program that can continue your if/else statement later.

$issuNo = 9;     
if ($month == "8" || $month == "9" || $month == "10") {
        $comingseason   = 'winter';
        $elementsDue    = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
    else if ($month == "11" || $month == "12" || $month == "1") {
        $comingseason   = 'spring';
        $elementsDue    = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
    else if ($month == "2" || $month == "3" || $month == "4") {
        $comingseason   = 'summer';
        $elementsDue    = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
    else if ($month == "5" || $month == "6" || $month == "7") {
        $comingseason   = 'fall';
        $elementsDue    = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. ".$issuNo;
    }
$issuNo++;

Upvotes: 1

DirtyBit
DirtyBit

Reputation: 16782

If this is what you're looking for:

<?php

$month = '3';
$year = '2015';
$issue = 9;
 if ($month == "8" || $month == "9" || $month == "10") {
    $comingseason   = 'winter';
 echo   $elementsDue    = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "11" || $month == "12" || $month == "1") {
    $comingseason   = 'spring';
        $issue = $issue + 1;

echo    $elementsDue    = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "2" || $month == "3" || $month == "4") {
    $comingseason   = 'summer';
        $issue = $issue + 2;
echo    $elementsDue    = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "5" || $month == "6" || $month == "7") {
    $comingseason   = 'fall';
    $issue = $issue + 3;
echo    $elementsDue    = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. $issue";
}
?>

PHP fiddle

Upvotes: 0

techwestcoastsfosea
techwestcoastsfosea

Reputation: 778

Ok this seems to be a very simple problem to solve. I would suggest reading a good entry level PHP programming book. Having said that, you need to set a while loop and some nested if else conditions.

$month = 0;

while ($month < 12) {
    $month++;

    If ($month > 7 && $month < 11)
    { 
        $comingsoon = "winter";
    }
    elseif 
    {
        some logic
    }
    elseif
    {
        some logic
    }
    else
    {

    }
} # end of while loop

I am intentionally leaving the remaining code for you to write. It would be a good exercise for you to learn some coding :) Good Luck!

Upvotes: 1

CodeBoy
CodeBoy

Reputation: 3300

Looks to me like the issue # is a function of year and quarter: you have 1 issue per quarter starting at some base year/quarter. So, something like this should automatically calculate it (you'll have to play with $base_year and $base_qtr to get it to match your issue numbering).

function currentIssue() {
    $base_year = 2014;
    $base_qtr = 2;
    $now_year = intval(date('Y');
    $now_qtr = ceil(intval(date'm')/3);
    return ($now_year - $base_year)*4 + ($now_qtr - $base_qtr);
}

Upvotes: 1

Related Questions