• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AJAX ARRAY

#1
Hello there. I'm from Turkey. I use google translate because i don't speak english.

I am sending an array to the Controller with AJAX. There are 3 different IDs in the array I sent. I will pull data from the database with these IDs, but I don't know how to separate them.

Ajax
Code:
$.ajax({
            url: '<?php echo base_url('varyasyonlar/o_getir') ?>',
            type: 'POST',
            data: {opts: opts},
            dataType: 'JSON',
            cache: false,
            success: function (data) {
                const toast = swal.mixin({
                    toast: true,
                    position: 'top-end',
                    showConfirmButton: false,
                    timer: 3000,
                    padding: '2em'
                });

                toast({
                    type: 'success',
                    title: 'Çalıştı.',
                    padding: '2em',
                })
            },
            error: function (data) {
                swal({
                    title: 'Bir hata oluştu!',
                    type: 'error',
                    padding: '2em'
                });
            }
        });

Controller
Code:
public function o_getir() {
        $data = $this->input->post('opts');

        $this->varyasyonlar_model->o_get2($data);
       
        foreach ($data as $key => $row) {
            $output['opsiyon_id'][$key] = $row->opsiyon_id;
            $output['opsiyon_baslik'][$key] = $row->opsiyon_baslik;
        }

        echo json_encode($output);    
    }

Model
Code:
public function o_get2($data) {

        for ($i = 0; $i < count($data); $i++) {
            $this->db->where('varyasyon_id', $data[$i]);
            $result[] = $this->db->get('opsiyonlar')->result_array();
        }
       
        return $result;
    }

IDs in Array
[Image: s6OuIG.png]

The data I want to bring from the database.
[Image: IVwJGn.png]

The data of each ID must be in different arrays.
Reply

#2
You need to use json_decode to convert the json array into a php associated array.

Then you can get the values you need.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
I was able to export the data to the array variable in the model file.

My Model
Code:
public function o_get2($data) {
        $array = [];

        for ($i = 0; $i < count($data); $i++) {
            $this->db->where('varyasyon_id', $data[$i]);
            $query = $this->db->get('opsiyonlar');

            foreach($query->result() as $key => $row)
            {
                $array[$i][$key] = $row->opsiyon_baslik;
            }
        }
       
        return print_r($array);
    }

Print: Array ( [0] => Array ( [0] => S [1] => M [2] => L [3] => XL ) [1] => Array ( [0] => Kırmızı [1] => Sarı [2] => Yeşil ) )

but i can't print it on controller.

Controller???

Code:
    public function o_getir() {
        $data = $this->input->post('opts');

        $this->varyasyonlar_model->o_get2($data);
       
        foreach ($data as $key => $row) {
            $output['opsiyon_baslik'][$key] = $row->opsiyon_baslik;
        }

        echo json_encode($output);    
    }
Reply

#4
Maybe this will help you.

How to Handle AJAX request on the Same Page – PHP
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.