1

I want to save the array from view to database but I can't get the array from view.

in view page

<?php
$nilai = array(
array(1,2,3,4),
array(a,b,c,d)
);

<a href="<?= BASEURL; ?>/penerima/tambah?nilai=$nilai" class="btn btn-l btn-info">simpan</a>

in controller page

<?php

class Penerima  extends Controller
{

    public function index()
    {
        $data['penerima'] = $this->model('Penerima_model')->getAll();
        $this->view('templates/header');
        $this->view('penerima/index', $data);
        $this->view('templates/footer');
    }
    public function tambah() and I'm using MVC concept
    {

        $nilai = $_GET['nilai'];
        $this->model('Penerima_model')->tambahData($nilai);
    }
}

I have two-dimensional array.

How can I resolve this problem?

tereško
  • 58,060
  • 25
  • 98
  • 150

4 Answers4

1

I found the solution. Here is what worked for me. I was passing array of arrays as a string value using

json_encode()

The solution was to escape the characters, so I had to replace the above line with

htmlspecialchars(json_encode(array))

Solution:

 $nilai = array(
    array(1,2,3,4),
    array('a','b','c','d')
    );
<a href="http://localhost/code/tambah?nilai=<?= htmlspecialchars(json_encode($nilai)); ?>" class="btn btn-l btn-info">simpan</a>

And in the controller, I had to get my array from JSON with the following lines

function tambah(){
$dataJson = $this->input->get('nilai');
$dataArray = json_decode(htmlspecialchars_decode($dataJson), true);
print_r($dataArray);
}

Best Solution

The best way is to use session

$this->session->set_userdata('nilai',$nilai);
<a href="http://localhost/code/tambah" class="btn btn-l btn-info">simpan</a>

Now, get in the controller

function tambah(){
$array = $this->session->userdata('nilai');
}

Note: Load session library in controller or autoload

Trupti Barad
  • 168
  • 1
  • 8
0

look reference Passing arrays as url parameter and if you are using codeigniter framework then to get value from URL try with

$nilai = $this->input->get('nilai');
navi
  • 102
  • 1
  • 11
0

You can json_encode the array and use urlencode to pass it.

In your view:

<?php
$nilai = array(
  array(1,2,3,4),
  array(a,b,c,d)
);
// encode to json string and encode to valid url
$nilai_urlencoded = urlencode(json_encode($nilai));

?>

<a href="<?= BASEURL; ?>/penerima/tambah?nilai=<?=$nilai_urlencoded; ?>" class="btn btn-l btn-info">simpan</a>

In your controller, decode the json string

<?php

class Penerima  extends Controller
{

    public function index()
    {
        $data['penerima'] = $this->model('Penerima_model')->getAll();
        $this->view('templates/header');
        $this->view('penerima/index', $data);
        $this->view('templates/footer');
    }
    public function tambah()
    {
        // Decode the url first and then convert the json_string to array
        $nilai = json_decode(urldecode($_GET['nilai'])));
        $this->model('Penerima_model')->tambahData($nilai);
    }
}
Bluetree
  • 1,324
  • 2
  • 8
  • 25
0
You can't send array in get request as it is. So you can change your code as shown below


 in view page
<?php
$nilai = array(
array(1,2,3,4),
array(a,b,c,d)
);

$nilai = json_encode($nilai);

<a href="<?= BASEURL; ?>/penerima/tambah?nilai=$nilai" class="btn btn-l btn-info">simpan</a>


in controller page

<?php

class Penerima  extends Controller`
{

    public function index()
    {
        $data['penerima'] = $this->model('Penerima_model')->getAll();
        $this->view('templates/header');
        $this->view('penerima/index', $data);
        $this->view('templates/footer');
    }
    public function tambah()
    {

        $nilai = $_GET['nilai'];
        $nilai = json_decode($nilai);

        $this->model('Penerima_model')->tambahData($nilai);
    }
}