0

Trying to sort an array in PHP that is being populated from a CSV. I would also, ideally, LOVE to be able to control the sort by clicking on tabs in the table here .. Right now, though, my first task at hand is just sorting the damn thing.. been working on this for over 3 days now.. any help is GREATLY appreciated!! Cheers!

PHP

<?php 

$fp = fopen("https://spreadsheets.google.com/pub?key=0AjZgwY03sLMGdHVoWjhucGowWWJBb2g2NnQzVG9HZFE&hl=en&single=true&gid=0&output=csv","r"); 
$rows = array(); 
while (($row = fgetcsv($fp)) !== FALSE) { 
    $rows[] = $row; 
}
fclose($fp); 

$headers = array_shift($rows);
foreach ($rows as $row) : list($ShowKey, $ShowFeedURL, $ShowLink, $ShowIcon, $ShowTitle, $ShowTag, $ShowCategory, $ShowEps, $ShowLastUpdate, $ShowNew) = $row;

$oddpost = ( empty( $oddpost ) ) ? '_odd' : ''; ?>
1
  • I would look in to usort with a custom callback function. Commented Dec 7, 2010 at 18:43

4 Answers 4

1

I recently did this. I had a multi-dimensional array of records from a database, and I needed to sort them based off of one specific column in the array. Here's what I did:

foreach($TimeRecords as $key => $value)
{
   $Rates[$key] = $value['rate'];
}
array_multisort($Rates, SORT_ASC, $TimeRecords);

I build an array of only the column I need, then I use the array_multisort() function to sort the array based off of that column.

You can write functions that will do this in PHP and then just call them with javascript ajax calls and reload that part of the page when it's done sorting.

Sign up to request clarification or add additional context in comments.

1 Comment

Ha! Seven years later and this was what I needed ... after hours of searching!
0

Instead of sorting the table in PHP, you may consider doing it client-side in Javascript. For instance have a look at this jQuery plugin: http://tablesorter.com/

4 Comments

wow, thanks chris.. I'm going to check into this right now.. I like it a lot and it looks pretty simple to implement. I'll let you know if this works for me..
Table Sorter plugin is awesome but, after implementing it I realized that the odd/even colored rows will move as well so it breaks up the pattern I'm trying to achieve with the design.. nonetheless.. this plugin saves heaps of time and I've definitely bookmarked it.. Thanks!!
I got it working, Chris! There is a widget called "Zebra" and it has to be activated via javascript and then the classes "odd" and "even" are added to TRs and TDs in the table automatically.. THANK YOU SO MUCH!!
My pleasure :-) Good to hear it's useful to you.
0

You may find usort() function helpful. It accepts callback argument, which may be used for sorting by specific field.

2 Comments

Thank you, Kel -- I am quite a novice at PHP and tried using usort() but couldn't quite figure out how/where to implement it in my code. It almost seemed that array_multisort() would have been the better choice but again, couldn't figure out how to make it work. Do you know, does the " foreach ($rows as $row) : list (..." portion of my code define the data keys for each array within my master array?
array_multisort() may be used as well - see answer from CrowderSoup. I did not get the question about "foreach" portion of code. list() function just allows to assign values from array to bunch of variables at once. You may take a look at ru.php.net/manual/en/function.list.php for details.
0

I had a similar issue to this today. Basically what I ended up doing is creating a temporary table which I loaded in the rows from the csv file that I needed. From there, I used php to sort and organize the data and update or add to the table I needed to alter.

Example, I made a table called 'temp' and loaded in all the rows of the category I needed. Then, once this was in the table, I made a php script which sorted the information by number of total sales in descending order. From there, I did a query to update my main table and used a limit to control this (only the top 200 items by number of sales).

It was very easy to do and hopefully it will help you or someone else.

Keep in mind. If you're going to do this more than once, you will need to truncate the temporary table to remove the old rows first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.