3

I'm using JavaScript would produce the following entries

<input type="hidden" name="my_img[]" value="dad6e52b274690409835.jpg">
<input type="hidden" name="my_img[]" value="252529b6e21f872c7675.jpg">
<input type="hidden" name="my_img[]" value="3cce4128c366216fsfaf.jpg">
<input type="hidden" name="my_img[]" value="48697e8516caa3cc15d4.jpg">

But I've had trouble inserting them in the database

my controllers :

   $dd=$this->input->post('my_img');
   for($i=0;$i<count($dd);$i++)
   {
   $img_box[$i] = array ('img_url' => $dd[$i]);
   }
   $this->posts_model->add_new_ads_img($img_box);

my model :

public function add_new_ads_img($img_box)
{
    $q = $this->db->insert('advertise_gallery',$img_box);
    if ($q)
    {
        return TRUE;
    }
    else return FALSE;
}

And to eat error

Severity: Notice

Message: Undefined variable: img_box

Please guide me

1
  • $img_box only lives within the for loop scope. Define it outside of the loop like this: $img_box = array(); Commented Feb 19, 2016 at 15:32

2 Answers 2

4

The error could be this line in your controller:

$img_box[$i] = array ('img_url' => $dd[$i]);

Just before your for-loop add:

$img_box = [];
Sign up to request clarification or add additional context in comments.

6 Comments

I think newer versions of php prefer = array() over = [] (not that it matters too much for this question)
Only newer versions support [], previously you had to use array(). (Since 5.4 I think)
Ah that makes sense.
tnx fix it but now have other problem i have 10 input but i print_r($img_box); show >>> arry() is empty and echo count($dd) show >>> 0
If count($dd) === 0, it seems that you did not post your page. If you comment out your code controller code except for the $dd=$this->input->post('my_img');, is the count different?
|
0

Based on what I think you are trying to do, you could try to give each input a different id. That way you could insert based on the id name.

for exampe:

    <input type="hidden" name="my_img[]" id="img1" value="dad6e52b274690409835.jpg">
<input type="hidden" name="my_img[]" id = "img2" value="252529b6e21f872c7675.jpg">
<input type="hidden" name="my_img[]" id = "img3" value="3cce4128c366216fsfaf.jpg">
<input type="hidden" name="my_img[]" id = "img4" value="48697e8516caa3cc15d4.jpg">

if you need to add them to an array, you can do so in the java script

var img=[$('#img1').val(),$('#img2').val(),$('#img3').val()]

Hope this helps

1 Comment

No I want all entries are sent with a one name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.