Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign variable within a for array 1

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
I am a pharmacist that maintains a small pharmacy organization web site. I have been using if elseif and else conditional statements to display the next meeting date based on today's date. This is becoming very cumbersome secondary to many date changes. I am trying to use an array to simplify this process.

I am trying to loop through the array and if the condition of today's date is before the next meeting then assign that date to a variable to display on the webpage. Here is what I have, but is not working. In the below example, we do not have any meeting in June, July or December, but could be added at a later date, so trying to keep them as a placeholder.

PHP:
<?php
     //date variable
    date_default_timezone_set("America/Chicago");    
    $date = date("Ymd");        //Date format as YYYYMMDD
    
    //create array
    $meetdate = array();
    $meetdate[0] = "20190110";  //January
    $meetdate[1] = "20190212";  //February
    $meetdate[2] = "20190320";  //March
    $meetdate[3] = "20180410";  //April
    $meetdate[4] = "20180525";  //May
    $meetdate[5] = "";          //June
    $meetdate[6] = "";          //July
    $meetdate[7] = "20180830";  //August
    $meetdate[8] = "20180922";  //September
    $meetdate[9] = "20181019";  //October
    $meetdate[10] = "20181103"; //November
    $meetdate[11] = ""; //December
    
    sort($meetdate);
    
    //find next meeting date
    
    for ($index = 0; $index < count($meetdate); $index++){
    
        if ($date <= $meetdate[$index] {                                                          
            $nextmeetdate = $meetdate[$index]
        } else {
            $nextmeetdate = "Date to be announced";
            }
    }
    
    echo $nextmeetdate; 
?>

Thank you in advance for any help.

You don't know what you don't know...
 
You have a couple errors there. You are missing a closing parenthesis in your IF statement, and a semicolon below that.

Code:
 if ($date <= $meetdate[$index][COLOR=#EF2929][b])[/b][/color] {                                                          
            $nextmeetdate = $meetdate[$index] [COLOR=#EF2929][b];[/b][/color]
        } else {
            $nextmeetdate = "Date to be announced";
            }

If you don't have errors turned on and set to display, its recommended to at least turn them on while developing so you can see errors like those while making changes.

Easiest thing to do is to add these 2 lines at the top of your PHP script, and remove them once you know everything is working.

Code:
<?php
ini_set('display_errors', true);
error_reporting( E_ALL );
...

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
vacunita,

That did the trick, except I forgot to Break out the loop when the condition was true.

I do have errors turned on, but the message was misleading to a novice. It stated "Unexpected Else", which I now understand what that meant.

Thank you.



You don't know what you don't know...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top