Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to sort a table containing csv file data
    primarykey
    data
    text
    <p>I have to sort the file data in a table depending on how the user chooses it to be sorted (only options are ascending and descending)</p> <p>Here is my code for it:</p> <pre><code>if($submit=="Display"){ if ($headings=="0"){echo "&lt;h2&gt;Error&lt;/h2&gt;";} elseif ($search==""){echo "&lt;h2&gt;Error&lt;/h2&gt;";} else { if($headings==$headings_array[0]) echo "&lt;table border='1'&gt;"; $f = fopen("data.csv", "r"); while ($line = fgetcsv($f)){ echo "&lt;tr&gt;"; foreach ($line as $cell) { echo "&lt;td&gt;&lt;center&gt;".$cell."&lt;/center&gt;&lt;/td&gt;"; } echo "&lt;tr&gt;"; } fclose($f); function my_sort($a, $b){ if ($a == $b) return 0; return ($a &gt; $b) ? -1 : 1; } $arr = $cell; usort($arr, "my_sort"); print_r ($arr); echo "&lt;/table&gt;"; } } </code></pre> <p>But it's not sorting. Can someone spot what's wrong with it? And if you have a more efficient way of doing this, please let me know as I have 15 <code>headings</code> and it would be a pain to do <code>if statements</code> for each of those headings.</p> <p><strong>To be clear, here is what I have to do:</strong></p> <p>For each sort field (which are the headings) that the user chooses, I have to arrange the display according to that heading</p> <p>i.e <code>if($headings==$headings_array[0])</code> </p> <p><code>$headings_array[0]</code> is equal to <code>Names</code> therefore, the table should display the values in alphabetical (or reverse alphabetical) order of <code>Names</code></p> <p><strong>Extra info:</strong> </p> <p><code>$headings</code> is the name of the select box<br> <code>$headings_array</code> is the array values for the select box<br> <code>$search</code> is the radio buttons containing whether the table should be sorted as ascending or descending<br><br> <em>PHP only</em></p> <p>Any help is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload