0

Is it possible to have a php array sorted by clicking on a button?(Alphabetically or by year)

I am using fullpage.js and Columnizer jQuery plugin. Unfortunately, the problem is that I have to make a new table for each new slide. (Maybe someone knows a better solution?)

Or is it more useful javascript / jquery to use?

here is the Code:

// get table data from Plugin TablePress
$table = TablePress::$model_table->load( $atts['table-id'], true, true );

//only get the important data
$data = $table['data'];

$output = '<div id="tablecontest-slider">';

$output .= '<div class="section" id="section0">';

    array_shift($data);
    $counter = 1;
    foreach ($data as $value) {

        if ($counter == 1) {

            $output .= '<div class="slide">';
            $output .= '<div class="columnize">';

            $output .= '<table>';
        }

        $output .= '<tr>';
        $output .= '<td>' . $value[0] . '</td>';
        $output .= '<td>' . $value[1] . '</td>';
        $output .= '<td>' . $value[2] . '</td>';
        $output .= '</tr>';

        $counter++;
        if ($counter == 21) {
            $counter = 1;
            $output .= '</table>';

            $output .= '</div>';
            $output .= '</div>';
        }
    }

$output .= '</div>';

$output .= '</div>';


return $output;

I dont know how to sort the array by click on a button..

Thank you for ideas and suggestions

Nima
  • 3,309
  • 6
  • 27
  • 44
azrm
  • 199
  • 2
  • 13
  • You can sort by parsing every `````` and then switching the possition of them. But that isn't good way (especially SEO). Do it with sorting methods build in PHP on server-side Here are PHP sorting docs: http://php.net/manual/en/array.sorting.php – pkolawa Aug 21 '17 at 12:37

2 Answers2

0

You can't directly sort anything on PHP from JS. BUT you can send an Ajax query on the button click, and respond with the PHP array sorted.

Iván Rodríguez Torres
  • 4,293
  • 3
  • 31
  • 47
  • Yes, I thought so.. how can i do this with ajax? – azrm Aug 22 '17 at 08:25
  • @azrm that question is very wide. It depends a lot on your server and it would be better for you to elaborate a new question showing how your server is. Anyway, if the array is not very big, you can sort it using JS. You should read this answer: https://stackoverflow.com/a/979289/2127296 – Iván Rodríguez Torres Aug 22 '17 at 09:10
0

No, you can't. PHP is running on your server and delivers the page. So there is no PHP-Array anymore once you render the site in your browser.

If you would get your data via an AJAX-call you COULD implement sorting and filtering on the server.

Argee
  • 1,216
  • 1
  • 12
  • 22