Welcome Guest, Not a member yet? Register   Sign In
dealing with multiple versions of one field in a form
#1

[eluser]benmanson[/eluser]
Hi,

Hoping someone can help. My searches on this subject don't seem to find the right info (prob the terms I am searching on...)

I am trying to add functionality to my CMS built in CI that will allow the user to change the order of site pages or projects in a portfolio. I have a page that lists all the projects (for example) and then a 'pos' field next to each one to allow the position in the list to be set. (see attached screengrab)

I am not sure about a couple of things:

1. how to set up the field names in the form
2. how to process the resulting array when submitted

Currently my code looks like this:

Code:
<tr>
  <td><p>HW hotel</p></td>
  <td>&lt;input type="text" value="10" name="poslist[1][14]" /&gt;&lt;/td>
</tr>
<tr>
  <td><p>Medina on Crown</p></td>
  <td>&lt;input type="text" value="20" name="poslist[2][13]" /&gt;&lt;/td>
</tr>
...

Where '[1]' is the 'poslist' array row and '[14]' is the id of the project 'HW Hotel'. Am I on the right track here? I thought about using hidden fields to contain the id for each project but couldn't work it out.

Can someone point me in the right direction? I assume I need to get the name attribute set up correctly for each field and then use a foreach loop to process the submitted array.

thanks
Ben
#2

[eluser]jedd[/eluser]
Code:
<td>&lt;input type="text" value="10" name="poslist[1][14]" /&gt;&lt;/td>

You know that there's a [url="http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html"]form helper[/url], don't you? You shouldn't need to do manual &lt;input&gt; tags. Plus I'm guessing poslist needs a $ in front of it .. ?

Quote:I thought about using hidden fields to contain the id for each project but couldn't work it out.

Post some code, if you'd like comments. I used a hidden field approach for my forms .. I can post, but it's pretty lengthy and quite custom.
#3

[eluser]jedd[/eluser]
Okay .. I want any number of forms to be displayed on this page. My view is a loop that goes through the form array data, which is set in the controller. I'll rip out logic regarding success/fail on validation etc as that doesn't seem appropriate here.

First, the view code. There's a little bit of HTML preamble here, and then straight into the loop. I suspect there's a better way of assigning the 'name' and 'value' bits within hidden() or submit() calls.
Code:
foreach ($search_form_data as $form)  {
    echo form_open ($form["target"]);
    echo $form["preamble"] . " &nbsp; &nbsp; ";
    foreach ($form["input"] as $input_preamble=>$input_data)  {
        echo $input_preamble . " : ";
        echo form_input ($input_data);
        echo " &nbsp; &nbsp; &nbsp; ";
        }
    echo form_hidden ($form["hidden"]["name"], $form["hidden"]["value"]);
    echo form_submit($form["submit"]["name"], $form["submit"]["button"]);
    echo form_close ();
    echo "<hr>";
    }


The code in the controller to generate the array. I have this being loaded via a call to a private function but you could do it within your search method if you preferred. I also needed a way of having multiple text boxes within each form. Note that I haven't got any facility for other types of form elements (radios, etc) .. but will implement that stuff when I need it. I expect it will fit into this structure reasonably well.

Oh, and people that know what they're doing -- be gentle when you read this.

Code:
$search_form_data = array (
            array (
                "preamble" => "Search on scientific name",            // This determines the form preamble
                "target"   => "organism/search",                    // Goes into form_open()
                "input"    => array (                            // Multiple form_input() in a single form
                    "Genus" => array (                        // Preamble for text box
                        'name'  => 'search_genus',                // form_input takes 'name' and 'value'
                        'value' => $posted_values['value_search_genus'],
                        ),
                    "Species" => array (
                        'name'  => 'search_species',
                        'value' => $posted_values['value_search_species'],
                        ),
                    ),
                "hidden" => array (                            // All forms are identified by hidden()
                    "name"  => "search_type",
                    "value" => "sci",
                    ),
                "submit" => array (                            // Submit button and name
                    "button" => "Go looking!",
                    "name"   => "submit_sci",
                    ),
                 ),
             // Obviously this stuff is repeated for extra forms
             );

Hmmm .. the forum has munged my tabs. Oh well.
#4

[eluser]benmanson[/eluser]
[quote author="jedd" date="1236758428"]

You know that there's a [url="http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html"]form helper[/url], don't you? You shouldn't need to do manual &lt;input&gt; tags. Plus I'm guessing poslist needs a $ in front of it .. ?

Post some code, if you'd like comments. I used a hidden field approach for my forms .. I can post, but it's pretty lengthy and quite custom.[/quote]

Thanks jedd,

Apologies if I have caused confusion. The code I posted was the resulting html, not the php to create it. I posted it as an example of what i am trying to achieve. I am OK with using the form helper, creating hidden fields etc.

I will try to explain a bit better.

As an example, I have a list of projects:

Project 1
Project 2
Project 3
Project 4

I want to allow the CMS user to change the order those projects appear on the website. I have created a column in the dbase projects table called 'pos'. By setting a pos for each project, I can then orderby that 'pos' when outputting the projects.

So to order the projects like:

Project 4
Project 2
Project 1
Project 3

The user would assign a value to the 'pos' field for each one.

Rather than having to edit the 'pos' for each project individually, I want to display a list of all the projects on the page and the form field 'pos' for each one (As per screengrab in first post).

I am not sure how to do that. I will also need the 'id' for each project so when I update the dbase I can match the 'pos' to the project.

So I am guessing I need to end up with an array like

row | id | pos
---------------
[1] | 13 | 10
[2] | 12 | 30
[3] | 15 | 20
...

But I am not sure how to do it correctly. Does that help explain? Maybe I am going about it the wrong way entirely - if anyone can set me straight I would appreciate it.

cheers
Ben
#5

[eluser]jacobc[/eluser]
Submitting arrays from forms is quite simple...

And a useful thing, although maybe not in this case is a blank [].
&lt;input type="textbox" name="test[]" value="a"&gt;
&lt;input type="textbox" name="test[]" value="b"&gt;
&lt;input type="textbox" name="test[]" value="c"&gt;

In $_POST that would give
$_POST['test'][0] = 'a';
$_POST['test'][1] = 'b';
$_POST['test'][2] = 'c';

&lt;input type="textbox" name="test[5][2]" value="a"&gt;
&lt;input type="textbox" name="test[2][5]" value="b"&gt;
&lt;input type="textbox" name="test[1][1]" value="c"&gt;

In $_POST that would give
$_POST['test'][5][2] = 'a';
$_POST['test'][2][5] = 'b';
$_POST['test'][1][1] = 'c';

For your case... I don't understand why you would need multidimensional...

Why not just a textbox with name="pos[&lt;?=$id?&gt;]"?
#6

[eluser]benmanson[/eluser]
[quote author="jacobc" date="1236776007"]

For your case... I don't understand why you would need multidimensional...

Why not just a textbox with name="pos[&lt;?=$id?&gt;]"?[/quote]

Thanks heaps Jacob,

Ahh yes it could be that simple, maybe I have been overthinking it. Will try it out and see how it goes.

cheers
Ben




Theme © iAndrew 2016 - Forum software by © MyBB