Move items btw 2 listboxes and insert one array into mysql

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
Ruthik
Forum Newbie
Posts: 2
Joined: Wed Nov 15, 2006 1:59 pm

Move items btw 2 listboxes and insert one array into mysql

Post by Ruthik »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have found that using implode will take an array and get it ready for entry to a mysql database. My only problem now is getting the listbox values into an array. Normally you would just name the SELECT with brackets at the end. I don't think this will work for me. I am using a javascript snippet to allow values to be passed between 2 listboxes.

How should I make the values of listbox Active an array?

[b]The script that allows values to be moved from one listbox to another using an add and remove button[/b]
[syntax="html"]
<SCRIPT>
    <!--
    function MoveOption(objSourceElement, objTargetElement)
    {
        var aryTempSourceOptions = new Array();
        var x = 0;
        
        //looping through source element to find selected options
        for (var i = 0; i < objSourceElement.length; i++) {
            if (objSourceElement.options[i].selected) {
                //need to move this option to target element
                var intTargetLen = objTargetElement.length++;
                objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
                objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
            }
            else {
                //storing options that stay to recreate select element
                var objTempValues = new Object();
                objTempValues.text = objSourceElement.options[i].text;
                objTempValues.value = objSourceElement.options[i].value;
                aryTempSourceOptions[x] = objTempValues;
                x++;
            }
        }
        
        //resetting length of source
        objSourceElement.length = aryTempSourceOptions.length;
        //looping through temp array to recreate source select element
        for (var i = 0; i <aryTempSourceOptions>
    </SCRIPT>
[/syntax]

Code: Select all

<form action=\"\" name=\"MoveList\">
			<table>
				<tr>
					<td align=\"center\"><font face=\"Arial,Helvetica,sans-serif\" size=\"2\"><b>Roster</b></font></td>
					<td><p>&nbsp;</p></td>
					<td align=\"center\"><font face=\"Arial,Helvetica,sans-serif\" size=\"2\"><b>Active</b></font></td>
				</tr>
				<tr>
					<td>";
						$get_players = mysql_query("SELECT player_id, clan_id, player_name FROM ".$sql_prefix."_players WHERE clan_id = $current_clan");
						if ($get_players){
							echo "<SELECT NAME=\"roster\" MULTIPLE SIZE=10 style=\"width: 100px;\">";
							for ($p=0; $p < mysql_num_rows($get_players); $p++){
								list($player_id, $clan_id, $player_name) = mysql_fetch_row($get_players);
								echo "<OPTION VALUE=\"$player_id\">$player_name</OPTION>";
							}
							echo "</SELECT>";
						} else {
							echo "Could not retrieve player data.";
							die();
						}	
					echo "</td>
					<td>
						<input type=\"button\" name=\"Disable\" value=\"&nbsp;&nbsp;&nbsp; ADD -> \" style=\"width: 100px;\" onClick=\"MoveOption(this.form.roster, this.form.active)\"><br>
						<br>
						<input type=\"button\" name=\"Enable\" value=\" <- REMOVE &nbsp;&nbsp;&nbsp;\" style=\"width: 100px;\" onClick=\"MoveOption(this.form.active, this.form.roster)\"><br>
					</td>
					<td>
						<select name=\"active\" size=\"10\" multiple style=\"width: 100px;\"></select>
					</td>
				</tr>
			</table>
			</form>
Right now, it just moves values back and forth. I don't know how to make the second listbox an array though. If I rename it to Active[], it breaks in the javascript function and wont allow data to be moved into it.

Much appreciation,
Ruthik


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply