0

Is it possible to send an array as a parameter to a function? Searched this online but didn't help me much further. This is what I have :

In my twig I have:

<a id="exporttocsv" href="{{ path('dashboard.index', {'data': tableresults|json_encode}) }}" class="btn btn-default">Export to CSV</a>

And in my Controller:

public function exporttocsvAction(Application $app, Request $request, $data)
{
    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    header("Pragma: no-cache");
    header("Expires: 0");

    $output = fopen("php://output", "w");

    foreach ($data as $row)
    {
        fputcsv($output, $row, ',');
    }

    fclose($output);
    exit();
}

But when I click my button, my link changes to something like this and just refreshes:

http://thelinktomypage.com/en/dashboard?data=%5B%7B%22Code%22%3A%22010%22%2C%22Name%22%3A%22Rebecca+Zygmunt%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22014%22%2C%22Name%22%3A%22Maria+Berhe%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22021%22%2C%22Name%22%3A%22Patrick+J%5Cu00f6hnk%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22030%22%2C%22Name%22%3A%22Alisha+Helena+Storr%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22033%22%2C%22Name%22%3A%22Jan+Nicolai+Kaminski%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22034%22%2C%22Name%22%3A%22Sebastian+Mantel%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22035%22%2C%22Name%22%3A%22Sabrina+Herrmann%22%2C%22PA%22%

Is it possible to send an array? Or should I do this with an ajax call and send the data?

nielsv
  • 6,540
  • 35
  • 111
  • 215
  • You want to send an array to your Action via HTTP Get right? But right now you are attatching the parameter json encoded and you do not want to just decode it in your Action for some reason? Couldn't you save your results in the session and use that data in your Action? – Andresch Serj Apr 30 '14 at 08:39
  • The results without the |json_encode is the same. Ok, but isn't this easier with just an ajax call then? – nielsv Apr 30 '14 at 08:45
  • How is that easier with an ajax call? Your data is rendered/calculated in the backend somewhere so it can be stored in the session. Then you can provide a temporary download link to an action that converts the session data to CSV. – Andresch Serj Apr 30 '14 at 13:13

1 Answers1

3

To pass an array in a GET request check this answer how to pass an array in GET in PHP?.

You can also do that on a POST request, for instance using a form as described here Passing arrays from HTML form to PHP.

But in your case it seems the best way would be to pass it using ajax+json and then decode on the server side. Check the following answer How to pass an array using PHP & Ajax to Javascript? :)

Community
  • 1
  • 1
manei_cc
  • 175
  • 2
  • 10
  • Wrong. You can pass Arrays via get. Example_ http://example.org/index.php?array[foo]=bar&array[x]=y – Andresch Serj Apr 30 '14 at 08:41
  • 1
    One more moment, - when you pass data through GET, you need to remember that most webservers have a limit of 8192 bytes (8KB). And here @nielsv need to pass csv, and it for sure can be longer... – pomaxa Apr 30 '14 at 10:07