Hi, I want to be able to extract a comma seperated list into seperate variables that I can use to limit a loop that displays different <OPTION>s in a select list.

Here's the senario - user has access to different photo albums - each album has a name, and a row ID (it's primary key). Admin sets up users, selecting any combintation of albums that the user can access. Since I'm using a <select> with multiple turned on, the resulting data that is inserted into my DB is "1,2,5" or something similar.

How can i use explode on the

$rs['allowed_albums']

variable to get which album IDs the user is allowed to see.

I know that

$album = explode($rs['allowed_albums'])

is going to give me:

$album[0] = 1 
$album[1] = 2 
$album[2] = 5 

But how can I use the array variables above to do an if () {} statement while looping out the result set of a query of the albums table. Basically i want to build a <select> statement that only shows album 1, 2 and 5 for this user. It has to be dynamic, becuase, admin can add new albums at will.

ANY help on this is greatly apprecieated! Thanks!

    something like this should do the trick

    $sql = 'select `id`, `name` from `albums`';
    $result = mysql_query($sql);
    $albNames = array();
    while ($row = mysql_fetch_array($result))
      $albNames[$row['id']] = $row['name'];
    
    echo '<select name="myAlbums">';
    foreach($album as $val)
    {
      echo "<option value='{$val}'>{$albNames[$val]}</option>";
    }
    echo '</select>';
    

      everything when smoothly except the following:

      Notice: Undefined index: McKinnon Family Album in C:\www\htdocs\photoalbum\user.php on line 35
      
      Notice: Undefined index: Early McKinnon Photo Album in C:\www\htdocs\photoalbum\user.php on line 35
      
      Notice: Undefined index: McDonald Family Album in C:\www\htdocs\photoalbum\user.php on line 35
      
      Notice: Undefined index: Fawcett Family Album in C:\www\htdocs\photoalbum\user.php on line 35
      
      Notice: Undefined index: Faul Family Album in C:\www\htdocs\photoalbum\user.php on line 35
      
      Notice: Undefined index: New McKinnon Family Album in C:\www\htdocs\photoalbum\user.php on line 35
      

      As you can see it's spitting out the correct names, except that they are associated as the array's index...

      i think i has to do with this bit of code:

      $albNames[$val] in the following block...

        echo "<option value={$val}>{$albNames[$val]}</option>"; 
      

      thanks again for you help - this has def. got me started towards a working solution...

        Yeah, I was able to get it to work, however, all it did was loop out all the names and set them as the Label and Value for the select box...

        I needed it to list only the values that also appear in the $HTTP_SERVER_VARS['allowed_albums'] variable

        i need somthing like the following example, which i know doesn't work, but kind of illustrates where i'm going with this :

        ... query stuff up here ....
        ... set $row to mysql_fetch_assoc($qry) ...
        
        $allowedAlbums = explode($HTTP_SERVER_VARS['allowed_albums']);
        
        $i = 0;
        
        while ($i > $num_rows && $i < $num_array_elements) {
        
        if ($allowedAlbums[$i] == $row['id']) {
            echo <OPTION value=$row['ID']>$row['album_name']
        </OPTION>
        }
        
        ++ $i;
        }
        

          You can use the in_array() function to check to see if the album index is in the array.

          It would look something like the following:

          ... query stuff up here .... 
          ... set $row to mysql_fetch_assoc($qry) ... 
          
          $allowedAlbums = explode($HTTP_SERVER_VARS['allowed_albums']); 
          
          if (in_array($row['id'], $allowedAlbums)) { 
              echo "<OPTION value={$row['ID']}>{$row['album_name']}
          </OPTION>";
          } 
          

          I think that should do what you are wanting it to do, it will only show the photo album name if it is in the allowed albums... Tell me if it doesn't work!

          Hope that helps.

          -Percy

            Thanks. That looks about right, however, I noticed that PHP treats checkboxes or multiple selects a bit differently then I'm used to. I was expecting an output of 1,2,5 - as each checkbox has the same name. If i selected 1 2 and 5 then $HTTP_POST_VARS['checkbox'] always equaled 5, even if i tried a get and saw all the variables in the query string
            (i.e. "link.php?checkbox=1&checkbox=2&checkbox=5")

            anyway, i rewrote the whole system differently - just allowing for 9 albums - and using some loop code to name each checkbox for the album, set up the queries to always update / insert on the user table - all the albums, setting the values to either 0 or 1 and supplying variables with the correct value to the query.

            it's working pretty nicely, and with a quick addition of a show_album flag - only active albums are worked upon and show through out the app.

            however, i have a background in coldfusion programming, and they have a lot of simple list functions - that are very straight forward to use. I was looking for the same tools over in PHP - in_array is probably the one i've been looking for the most 🙂

            thanks to all!

              The problem with:

              link.php?checkbox=1&checkbox=2&checkbox=5
              

              Is that you are trying that have 3 variables witht he same name...

              A simple way to do it with checkboxes is:

              <INPUT type=checkbox name="checkbox[]" value=1>Album 1
              <INPUT type=checkbox name="checkbox[]" value=2>Album 2
              <INPUT type=checkbox name="checkbox[]" value=3>Album 3
              <INPUT type=checkbox name="checkbox[]" value=4>Album 4
              

              When the user checks off some of the boxes and hits submit, PHP handles the [] automatically and would make the array for you!

              For example, if the user checked off Albums 1, 3 and 4, PHP would give you the following array for $checkbox:

              Array
                  (
                      [0] => 1
                      [1] => 3
                      [2] => 4
                  )
              

              PHP is great in that way!!

              -Percy

                Then, I could have access to using the in_array() function. I guess I wasn't expecting different server-side scripting enviorments to handle form posts differently, yet similar.

                thanks, I will implement that in another version, now that i'm past it, and writing documentation for the app - i don't want to go back and change it.

                thnx :

                  Write a Reply...