Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Select boxes to array

First the simplest option, which works fine

[Frozen]
Header
content
Sidebar
Footer
pageend

-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10




I can save these values in an array by using this
function createNugget() { $NewNugget = array( 'position' => ForceIncomingString('Position', '');, 'weight' => ForceIncomingInt('Weight', 0);, );}

What If i have a large number of the select boxes like below


[Frozen]
Header
content
Sidebar
Footer
pageend

-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10


[Frozen]
Header
content
Sidebar
Footer
pageend

-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10


[Frozen]
Header
content
Sidebar
Footer
pageend

-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10




How do i save all these values in the array?
The way i know is to generate a unique name for all the select boxes and then cycle through them one by one and store them. Is there a better way of doing this.
Can I pass all of them to createNugget function at once.

Comments

  • Options
    what i would do is to have them have unique names of the type : 1_position and 1_weight, 2_position and 2_weight etc. then cycle through with for instance
    $arr = array();$n=1; foreach ($_POST) { $arr[$n]['position'] = $_POST[$n."_position"]; $arr[$n]['weight'] = $_POST[$n."_weight"]; }
    this is of course a simplified example
  • Options
    I'd use an array:
    <select name="Position[]">options...</select><select name="Weight[]">options...</select><br>
    <select name="Position[]">options...</select><select name="Weight[]">options...</select><br>
    <select name="Position[]">options...</select><select name="Weight[]">options...</select><br>
    etc...
    which would come up in your variables as arrays (ie. $_POST['Position'] would be an array of all the 'Position' inputs, and $_POST['Weight'] would hold the weights)
  • Options
    even better. didnt realise you could :)
  • Options
    edited March 2007
    In vanilla we use the Select class to create these select boxes like so
    $this->PositionSelect = $this->Context->ObjectFactory->NewObject($this->Context, 'Select'); $this->PositionSelect->Name = 'Position'; $this->PositionSelect->AddOption('none', $this->Context->GetDefinition('none')); $this->PositionSelect->AddOption('header', $this->Context->GetDefinition('header')); $this->PositionSelect->AddOption('content', $this->Context->GetDefinition('content')); $this->PositionSelect->SelectedValue = htmlspecialchars($NuggetMng->Nugget[$GLOBALS['NuggetMng']->SentNuggetIndex]['position']);

    So can I use just one select box, but different names like Position[1], Position[2] etc. or no
    Or they have to be completely new instances

    Here is where i mentioned why I'm doing this. in case you want to know the details
    Nugget Management

    @Sirnot. IN your Page Management extension. When i click save u call the CreateTabs() function that stores the names of the inputfields and select fields passes them on to SaveTabs() function and ur done.
    In my case with all these Position and Weight select fields. How will I call the CreateTabs function? The PageSetting Variable will now contain an Array of Position and Weight?
  • Options
    I may be of more help if I could see more of your code, but really you'd just have to loop through them.
  • Options
    NickENickE New
    edited March 2007
    I can't really try it out at the moment (my screen's backlight decided to burn out on me...), but there are a variety of things you could do. one would be to have the position/weight selects as arrays, with the nugget ids as indices:
    <select> name="position[nugget1_id]">...</select><select name="weight[nugget1_id]">...</select>
    etc.
    then simply loop through the nuggets and assign positions/weights to $_POST['position'][$nugget_id]/$_POST['weight'][$nugget_id] (after some sanity checks, obviously).

    another option would be to have each select as an array named after the nugget id it refers to. eg.
    <select name="nugget1_id[position]">...</select><select name="nugget1_id[weight]">...</select>
    etc.
    then you'd loop through the post vars ( while( list($nugget_id, $arr) = each($_POST) ) ) and, after verifying the data, assign the nugget with id $nugget_id to values $arr['position']/$arr['weight'].

    and btw, in regard to the page selection, you could also use a multiselect input field.
This discussion has been closed.