www.webdeveloper.com
Results 1 to 14 of 14

Thread: if control for array

  1. #1
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64

    if control for array

    Hello,

    array post
    Sample:
    form
    Code:
      
      <select size="8" name="day_of_week[]" id="day_of_week" multiple>  
    		<option value="-1" selected="selected">*</option>
    		<option value="0">Sunday</option>
    		<option value="1">Monday</option>
    		<option value="2">Tuesday</option>
    		<option value="3">Wednesday</option>
    		<option value="4">Thursday</option>
    		<option value="5">Friday</option>
    		<option value="6">Saturday</option>		
      </select>
    if(is_array($_POST['day_of_week']) && in_array(GOING TO HAPPEN HERE?, $_POST['day_of_week'])){
    echo "Selected day(s) of week";
    }
    PHP Code:
    if(is_array($_POST['day_of_week']) && in_array(GOING TO HAPPEN HERE?, $_POST['day_of_week'])){
    echo 
    "Selected day(s) of week";
    }


    // this is okay
    if(is_array($_POST['day_of_week']) && in_array('-1'$_POST['day_of_week'])){
    echo 
    "Disabled day(s) of week";

    Thanks
    Last edited by Adem; 09-14-2013 at 09:31 AM.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    What is your question???

  3. #3
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64
    Quote Originally Posted by ginerjm View Post
    What is your question???
    I'm sorry, I do not know english

    This question, Selected day of the week
    if(is_array($_POST['day_of_week']) && in_array(GOING TO HAPPEN HERE?, $_POST['day_of_week'])){
    echo "Selected day(s) of week";
    }

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    Sorry - don't know what you want to find here.

    Good luck.

  5. #5
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64
    Is selected day(s) of the week
    if(??????????){

    }

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    Sorry...

  7. #7
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64
    Quote Originally Posted by ginerjm View Post
    Sorry...
    Do you have any idea on this subject?
    http://www.webdeveloper.com/forum/sh...s-Can-you-help
    Last edited by Adem; 09-14-2013 at 04:20 PM.

  8. #8
    Join Date
    Mar 2012
    Posts
    1,250
    You need to use the date function. There are a few variations:
    - date('D') returns "Mon" to "Sun".
    - date('l') returns "Monday" to "Sunday".
    - date('N') returns "1" (Monday) to "7" (Sunday).

    So use syntax like:

    if (date('N')=='1') { *** do this for Monday *** }
    elseif (date('N')=='2' { etc... }

    or to test multiple days in a single if statement use STRPOS. However, strpos has a weakness in that matching the first character returns 0, which can be interprted as false instead of true. So you need to either use '===' as in:

    if !(strpos('1357',date('N')) === false) { *** do this if Mon, Wed, Fri or Sun *** }
    else ( *** do this if Tue, Thu or Sat *** }

    or use a dummy character as the first character in the test string that will never match. E.g. Using '_' (underscore) as the first character:

    if (strpos('_1357',date('N')) == true) { *** do this if Mon, Wed, Fri or Sun *** }
    else ( *** do this if Tue, Thu or Sat *** }

    Note: I haven't tested the above code, so it may need tweaking, but this should give you an idea of how to do it.

  9. #9
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64
    jedaisoul, Thank you for reply.

    Okay, not the problem
    1....7
    I need the code to control two
    Only if() the code

    array name="day_of_week[]"

    Disable week days, selected(*) value(-1)
    HTML Code:
      <select size="8" name="day_of_week[]" id="day_of_week" multiple>  
            <option value="-1" selected="selected">*</option>
            <option value="1">Sunday</option>
            <option value="2">Monday</option>
            <option value="3">Tuesday</option>
            <option value="4">Wednesday</option>
            <option value="5">Thursday</option>
            <option value="6">Friday</option>
            <option value="7">Saturday</option>        
      </select>
    First control: Disable week days, a selection, a value (-1)
    if($_POST['day_of_week']=='-1'){
    Codes for disable week days
    I have this code available
    }



    Second control: Enable week days, selection a or multi day(s).
    Note: Are excluded * value (-1) "Not select " * " and day(s) of week the same time!"
    // days "1,2,3,4,5,6,7"
    if($_POST['day_of_week']>-1){ // I need some help here
    Codes for aktive week day(s)
    I have this code available
    }



    I've been using it for the dates of the week
    I change the, 1....7, no problem
    and I use this date('N'), no problem
    PHP Code:
                   if($sonraki_gun=='0'){
              
    $h_tarihi=date('d.m.Y'strtotime('noon sunday')); // Pazar
              
    }elseif($sonraki_gun=='1'){
              
    $h_tarihi=date('d.m.Y'strtotime('noon monday')); // Pazartesi
              
    }elseif($sonraki_gun=='2'){
              
    $h_tarihi=date('d.m.Y'strtotime('noon tuesday')); // Salı
              
    }elseif($sonraki_gun=='3'){
              
    $h_tarihi=date('d.m.Y'strtotime('noon wednesday')); // Çarşamba
              
    }elseif($sonraki_gun=='4'){
              
    $h_tarihi=date('d.m.Y'strtotime('noon thursday')); // Perşembe
              
    }elseif($sonraki_gun=='5'){
              
    $h_tarihi=date('d.m.Y'strtotime('noon friday')); // Cuma
              
    }elseif($sonraki_gun=='6'){
              
    $h_tarihi=date('d.m.Y'strtotime('noon saturday')); // Cumartesi
              


    For this project: http://www.zamanlayici.antenfiyati.com/?lng=en
    Last edited by Adem; 09-14-2013 at 07:54 PM.

  10. #10
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64
    Sorry, I wrote wrong, fixing it now
    HTML Code:
      <select size="8" name="day_of_week[]" id="day_of_week" multiple>  
    		<option value="-1" selected="selected">*</option>
    		<option value="7">Sunday</option>
    		<option value="1">Monday</option>
    		<option value="2">Tuesday</option>
    		<option value="3">Wednesday</option>
    		<option value="4">Thursday</option>
    		<option value="5">Friday</option>
    		<option value="6">Saturday</option>		
      </select>

  11. #11
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64
    Hello,

    True? or False?
    PHP Code:
    $week=array(1,2,3,4,5,6,7);

    // if active days of week
    if(is_array($_POST['day_of_week']) AND array_intersect($week$_POST['day_of_week'])){
    // if active days of week
    }

    // if disable days of week
    if(is_array($_POST['day_of_week']) AND in_array('-1'$_POST['day_of_week'])){
    // if disable days of week


  12. #12
    Join Date
    Mar 2012
    Posts
    1,250
    I've never used arrays as $_POST parameters, so I have no idea whether the code is valid, let alone what it will do. More importantly, I do not understand why you need to pass the day-of-week as an array? You are creating an array, $week, anyway.

    P.S. Shouldn't $week include 0 (not set) as date("N") can return that value for invalid input?

    P.P.S. If you do need to pass info of the "valid" days of the week, I would do so as a string and convert it to an array locally.
    Last edited by jedaisoul; 09-18-2013 at 06:30 AM.

  13. #13
    Join Date
    Oct 2012
    Location
    istanbul
    Posts
    64
    Active week: http://awesomescreenshot.com/0521qgcbaf
    Passive week: http://awesomescreenshot.com/09b1qgco5e
    Required to submit the form
    and
    Only me needed if() control
    PHP Code:
    $week=array(1,2,3,4,5,6,7);

    // if active days of week
    if(is_array($_POST['day_of_week']) AND array_intersect($week$_POST['day_of_week'])){
    // if active days of week
    }

    // if disable days of week
    if(is_array($_POST['day_of_week']) AND in_array('-1'$_POST['day_of_week'])){
    // if disable days of week

    Not the day of the week to determine!

  14. #14
    Join Date
    Mar 2012
    Posts
    1,250
    I do not understand your post, so I cannot offer any assistance.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles