Welcome Guest, Not a member yet? Register   Sign In
Working with Multi-Select & Dropdowns and SQL
#1

[eluser]Bionicjoe[/eluser]
I'm really not much of a coder but CI is getting me places. However I am lost on using multi-select fields and dropdowns.

I think I'm good on dropdowns for now.

I have the multi-select fields and I can select an item and submit to the db table and repop the form with that value to edit. However I can only store one item. Choosing 2 or more items I usually get the first item selected. I'm just storing the values as text in my table.

I can't find a good tutorial or info on multi-selects & SQL. Usually just find basic info on creating the field.

Any help is greatly appreciated.
#2

[eluser]Philipp Gérard[/eluser]
Code:
<select name="lineup[]" multiple="multiple"></select>
...returns an array of options selected.
#3

[eluser]Bionicjoe[/eluser]
Thanks, that works, but I get this error now.

Code:
Unknown column 'Array' in 'field list'

UPDATE `outage` SET ...blahblah... `servicename` = Array, `location` = Array, ...blah...

What should I change to get the values out of that nested array?
#4

[eluser]Abdul Jamal[/eluser]
$values=$this->input->post('lineup');

you can check output of $values.

print_r($values);
#5

[eluser]Philipp Gérard[/eluser]
Compare: http://php.net/manual/en/language.types.array.php
#6

[eluser]Bionicjoe[/eluser]
I did this.

Code:
function updateoutage()
  //Submit the updated outage data to the DB.
  {
    $this->load->model('outage_model','',TRUE);
    
    if (is_array($_POST))
        {
            foreach ($_POST['servicename'] as $data)
            {
                $_POST['servicename'] = $data['servicename'];
            }
            foreach ($_POST['location'] as $data)
            {
                $_POST['location'] = $data['location'];
            }
        }

But it dumps out these values.
Code:
UPDATE `outage` SET `outageid` = '1',  ...blah... `servicename` = 'V', `location` = 'L', ...
Values should be Video & Lexington, so it's using the first char as an index. I've done this before, but can't remember how I fixed it.

(Plus I got this error: Duplicate entry '1' for key 'PRIMARY'...grrr SQL)
#7

[eluser]Philipp Gérard[/eluser]
We are talking at cross purposes here I believe.

You want to have a multi-select dropdown where a user can select more than one entry. You now want to access these entries that the user selected to use them in some way (for instance in a database query).

1. Create a select like this: <select name="lineup[]" multiple="multiple"></select>
2. Add options: <option value="x">y</option>
2. Access the ARRAY of submitted data from this select: $array = $this->input->post('lineup');
4. Use print_r($array) to see how the array is composed.
#8

[eluser]Bionicjoe[/eluser]
I'm creating the multi-select just fine in the form. I'm just having a problem extracting it from the form, or specifically the Array in the POST data.
#9

[eluser]Philipp Gérard[/eluser]
Please post the output of print_r($array); so that I can tell you how to extract the desired information from the array.
#10

[eluser]Bionicjoe[/eluser]
Code:
Array (
[outageid] => 1 [ticketnumber] => INC000000000001
[servicename] => Array ( [0] => Video )
[location] => Array ( [0] => Columbus [1] => Lexington )
[impact] => 16000
[starttime] =>
[endtime] =>
[summary] => Sample Ticket 1 [details] => These are some details.  
)

This is how I get the above array.
Code:
if (is_array($_POST))
        {
            foreach ($_POST['servicename'] as $data)
            {
                //$_POST['servicename'] = $data['servicename'];
                $data = $this->input->post('serviccename');
            }
            foreach ($_POST['location'] as $data)
            {
                //$_POST['location'] = $data['location'];
                $data = $this->input->post('location'); //array('location' => $location);
            }

Servicename & location cause problems in the SQL UPDATE statement because they are arrays.
Quote:Unknown column 'Array' in 'field list'




Theme © iAndrew 2016 - Forum software by © MyBB