Welcome Guest, Not a member yet? Register   Sign In
DB Update with multiple inputs in a loop
#1

[eluser]Ludovic-r[/eluser]
Hi!

I'm stuck on a problem and hope you could help me with that, I've found some thread about similar problems but they not fix my problem :

I have this on the VIEW :
Code:
echo form_open('admin/update_categories/');
    
    if (isset($result)) {

        foreach ($result as $row) {

        echo form_input('category_name',$row->category_name);

        }
    } else { echo 'no categories'; }

    echo form_submit('','Save');

    echo form_close();

This is a result of a DB Query to show my "Categories" (each one in an input) in a form, now I would UPDATE them when changing the name in the INPUT

so in the CONTROLLER :

Code:
function update_categories(){
        
        $data = array(
            'category_name' => $this->input->post('category_name')
                    );
        
        $this->admin_model->update_categories($data);

    }

and the MODEL :

Code:
function update_categories($data) {

     $this->db->update('ft_categories', $data);
}

This will update my DB with the new "Category" name BUT the problem is that if I have many "categories" in the form it will update the DB with only the last input field. Maybe because I $POST one field!
Ok it's normal because I don't have any loops in the controller right? I've made a foreach loop and it doesn't work so now I'm a bit lost

Any help would be very very appreciated!!!

THANKS!
#2

[eluser]Dan Storm[/eluser]
This is not a CI problem, but actually just how MySQL works - to get the results you need, you would have to map your results to a temporary table and update between the two tables.

The other alternative is to loop through your inputs and use the update method for each input you have.
#3

[eluser]Frychiko[/eluser]
Each of your form inputs needs a separate name. Right now they are all named ‘category_name’ so only the (first, or last I’m not sure) input will be used. Give each form input a unique name.

EDIT: How do you want the category information stored in your database?
#4

[eluser]Dan Storm[/eluser]
[quote author="Frychiko" date="1306169761"]Each of your form inputs needs a separate name. Right now they are all named 'category_name' so only the (first, or last I'm not sure) input will be used. Give each form input a unique name.[/quote]

Ohh, you're quite right... didn't see that...
#5

[eluser]Ludovic-r[/eluser]
Oh you're right for the "category_name", I'll increment the name with a i++ for each input.

I just need to UPDATE all my DB fields called "category_name" with the new input in my table called "ft_categories"
So when a user type a new "name" in the input and press "Save" it will UPDATE the DB with the new "name" (hope I'm clear because my english is not as good as yours)


Thanks for answer by the way!
#6

[eluser]Ludovic-r[/eluser]
I've tried to loop the inputs then UPDATE but I can't figure it out (tried the foreach and huge amount of code). Could you give me a sample code to help me a bit?

Many thanks!
#7

[eluser]pmoroom[/eluser]
Show me your add form..... I suspect the reason it adds the last one is because you are not passing in an array.

should have something like this

<input type="text" name="category_name[]">

If you want more than 1 you can do
<input type="text" name="category_name[]">
<input type="text" name="category_name[]">
<input type="text" name="category_name[]">
<input type="text" name="category_name[]">
<input type="text" name="category_name[]">


Notice the []

Now you can do whatever you want with your data...

foreach($_POST['category_name'] as $key => $value)
{
$i = 0;
foreach ($value as $item)
{
$data[$i][$key] = $item;
$i++;
}
}
#8

[eluser]Ludovic-r[/eluser]
Hi, thanks for your answer but I can't achieve that I don't know why, here's my form :

Code:
echo form_open('admin/update_categories/');
    
    if (isset($result)) {

        foreach ($result as $row) {

        echo form_input('category_name',$row->category_name);

        }
    } else { echo 'no categories'; }

    echo form_submit('','Save');

    echo form_close();
#9

[eluser]Ludovic-r[/eluser]
Just modified a bit my view form to get this :

Code:
echo form_open('admin/update_categories/');
    
    if (isset($result)) {
            
        $i = 0;
            foreach ($result as $row) {

            $i++;
            $test = array($i => 'category_name');
            echo form_input($test[$i].'_'.$i ,$row->category_name);
        
        }
    } else { echo 'no categories'; }

    echo form_submit('','Save');
    echo form_close();

Which gave me something like this :

<input type="text" name="category_name_1" value="myname">
<input type="text" name="category_name_2" value="anothername">
<input type="text" name="category_name_3" value="andanotherone">
... there are as many input field as entries in the DB

These inputs can be edited to change the name of the category.


Ok now my DB Table called 'ft_categories' looklike this :

id = '1' category_name ="myname"
id = '2' category_name ="anothername"
id = '3' category_name ="andanotherone"

The only thing I need is when I press "Submit" the entire table updates with the new entries

Any ideas please?
#10

[eluser]pmoroom[/eluser]
[quote author="Ludovic-r" date="1306251750"]Hi, thanks for your answer but I can't achieve that I don't know why, here's my form :

Code:
echo form_open('admin/update_categories/');
    
    if (isset($result)) {

        foreach ($result as $row) {

        echo form_input('category_name',$row->category_name);

        }
    } else { echo 'no categories'; }

    echo form_submit('','Save');

    echo form_close();
[/quote]

Couldn't you do?

echo form_input('category_name[]',$row->category_name);

If memory serves me well that will bring the values over in an array and from there you can use my code above to iterate and do whatever.




Theme © iAndrew 2016 - Forum software by © MyBB