×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Assign variable within a for array

Assign variable within a for array

Assign variable within a for array

(OP)
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.

CODE --> 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...

RE: Assign variable within a for array

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]) {                                                          
            $nextmeetdate = $meetdate[$index] ;
        } 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

RE: Assign variable within a for array

(OP)
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...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close