SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting a Variable Value Based on Value of Array Element

    Hello All --

    I am currently using an open source piece of calendar software. This software inserts calendar entries into MYSQL based on an array of room id values like so:

    PHP Code:
    foreach ( $rooms as $room_id ) {

    insert into calendar_entrues values ... 


    I want to change the value of a variable within the foreach loop if one of the $rooms array elements equals a certain number. This variable would then be used as part of my insert.

    For example, I tried something like this:

    PHP Code:
    foreach ( $rooms as $room_id ) {

    if (
    $rooms == "22") {
    $hidden 1;
    }
    else {
    $hidden 0
    }

    insert into calendar_entries values ($hidden...)


    This code chugs right past my if statement and inputs 0 for each $hidden even when my $rooms = 22 is present.

    So I'm thinking I'm looking for the correct syntax to check if each array element equals a certain value. If it is, then set $hidden to one value, if not, set it to something else.

    Can anyone help me out?

    Thanks in advance!

    -- Chris

  2. #2
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be checking it against $room_id. The $rooms variable is the array.

    PHP Code:
    foreach ( $rooms as $room_id ) {
      if (
    $room_id == 22) {
        
    $hidden 1;
      }
      else {
        
    $hidden 0;
      }
      
    insert into calendar_entries values ($hidden...)


  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks spiderling.... I'm now trying something like what you provided and its not quite working:

    PHP Code:
    foreach ( $rooms as $room_id ) {

    if (isset(
    $_GET["full"])) {

    $hidden 0;

    if (
    $room_id == 3) {
    $hidden 1;
    }
    if (
    $room_id == 5) {
    $hidden 1;
    }

    }
    INSERT into....

    $rooms is an array that contains three values: 3, 5 and 22 and $[get("full"}] is set. I want $hidden set to 0 if $room_id equals 3 or 5. I just ran this and all three entries were entered into my DB with $hidden = 0.

    Any other thoughts?

    Thanks!

  4. #4
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use var_dump to check if $room_id is being seen as a string or int.

    PHP Code:
    foreach ( $rooms as $room_id ) {
    var_dump($room_id);
    print 
    '<BR>';
      if (isset(
    $_GET["full"])) {
        
    $hidden 0;
        if (
    $room_id == || $room_id == 5) {
          
    $hidden 1;
        }
      }
      
    INSERT into....


  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did that and here is the output:

    Code:
    string(1) "3" 
    string(1) "5" 
    string(2) "22"
    I tried to read them in as strings and that did not seem to work either...

  6. #6
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added
    PHP Code:
    print 'room id: '.$room_id.', hidden: '.$hidden.'<BR>'
    where the INSERT is and had the following result. I had the same results whether the array values were passed as strings or integers.

    room id: 3, hidden: 1
    room id: 5, hidden: 1
    room id: 22, hidden: 0

    Do you have the same results?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did, yes, at that particular point. HOwever, I ended up having additional foreach loops within the outer one that I did not account for. These inner ones is where the INSERTS actually take place and they did not see the change to my $hidden variable.

    Once I made that adjustment, it looks to be working great.

    Thanks spiderling!


Bookmarks

Posting Permissions

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