How can I get two uneven arrays to loop in one input tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

How can I get two uneven arrays to loop in one input tag

Post by gjb79 »

Anybody know how to accomplish this?

I have a multiselect list element that draws its content from a mysql database. When somebody chooses the items they want from the list, thier selections are given a number and saved to a different table in the same database.

I am trying to set it up so that the list is formed from table one, but the items that where saved to table two get "selected" added to the input code.

Code: Select all

<select name="selectfiles[]" size="5" multiple="multiple" class="textlist" id="selectfiles"> 
             <?php do { ?>
              <option value="<?php echo $row_filelist['ID']; ?>"<?php if ( $row_selectedfiles['ID'] == $row_selectedfiles['file'] ) { echo " selected"; }?>><?php echo $row_filelist['filename']; ?></option>
              <?php } while ($row_filelist = mysql_fetch_assoc($filelist)) || $row_selectedfiles = mysql_fetch_assoc($file)); ?>
            </select>
This is using the || operator. However this doesn't work. When I use an and opporator it only shows the items that have been selected, not the ones that haven't been selected as well.

filelist is the full list of items in the first database table.
selectedfiles is the limited list of items from the second database table.
Any ideas, any questions if you dont understand what I'm doing?

THANKS!!! :wink:
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Update

Post by gjb79 »

Code: Select all

<?php do { ?>
              <option value="<?php echo $row_filelist['ID']; ?>"<?php do { if ( $row_selectedfiles['file'] == $row_filelist['ID'] ) { echo " selected"; } } while ($row_selectedfiles = mysql_fetch_assoc($selectedfiles))?>><?php echo $row_filelist['filename']; ?></option>
              <?php } while ($row_filelist = mysql_fetch_assoc($filelist)) 
?>
I tried this variation. The thought behind this was that I would need to nest one loop within the other. This way the outter loop would load a number for the filelist['ID'] variable, then the second loop would compare all the selectedfiles variable/array to it. If there is a match between the filelist and selectedfiles variables during the inner loop, "selected" is echoed.

Then it goes onto the next item in the outter loop and again cycles through the inner loop looking for a match.

Does this sound crazy or logical? I can tell you now that what I put isn't working.

Thanks, :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

<select name="selectfiles[]" size="5" multiple="multiple" class="textlist" id="selectfiles">
while ($row_filelist = mysql_fetch_assoc($filelist)) {
    echo '<option value="'.$row_filelist['ID'].'"'. ($row_selectedfiles['file'] == $row_filelist['ID'] ? ' SELECTED' : '').'>'.$row_filelist['filename'].'</option>';
}
</select>
Not sure i wrote it entirely correct, but...
I'm using alternate syntax to if-then-else ( the (X == X ? TRUE : FALSE) bit) here that I might think will give some pointers. I also change from do-while to while alone, as I like that better.
Change and test.
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

I'm not sure if thats closer or not

Post by gjb79 »

Hi,

:arrow: Thanks for the suggestion. I tried it out and received the following results:

The list only displays the last item in the array, the first two items are missing.

Second, When I set it up so that all the entrys should be selected (to ensure that both arrays match up with each other), nothing happens.

The arrays should work along the lines of this 'item1 in arrayA'=='item1 in arrayB' echo "selected", the repeat with item2 in both arrays, etc...
If the two arrays are not equivelent, don't echo selected (but still place the value of option1 to the value of item1 in arrayA).

The trick is to get both arrays to loop in the same space. Also to recognize when the arrays are equal to each other, and yet allow the first array to continue if the second one doesn't match it or if it ends prior to the first array ending.

:?: I'm confuzzled as to how this can be accomplished. :?
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

New Idea

Post by gjb79 »

Ok I had this idea:

Code: Select all

<?php
  do { 
   do { 
?>
    <option value="<?php echo $row_filelist['ID']; ?>"<?php if ( $row_selectedfiles['file'] == $row_filelist['ID'] ) { echo " selected"; }?>><?php echo $row_filelist['filename']; ?></option>
<?php 
   } while ($row_selectedfiles = mysql_fetch_assoc($selectedfiles));
  } while ($row_filelist = mysql_fetch_assoc($filelist))
?>
So the out side loop will place its array values in where they need to go, then the inside loop will add its values, see if the two values are equal, loop again with the second item having a new value, check to see if they're equal again, and repeat for all the items in the second array. Then the out side loop repeats with a new value, and the second starts over again until it has a match.

In my head this works :? , in practice it does not :( . Anyone see whats wrong :?:

Thanks
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Have I explained this correctly?

Post by gjb79 »

Hi,

I was just wondering if I explained the problem correctly. I have reread the previous posts and it does seem a little confusing.

If there are any questions, or if I need to attempt to explain this again, Please let me know.

Thanks
Post Reply