2

I'd like to ask for your help. I have a CSV file in which there are values stored in 4 different categories. ( A B C D ) I have these values displayed in order by date, however, I'd like my page to display them by their category, and after that sort them by their date.

$_news_date is where the date is stored, and $news_category is where the category is stored.

For example: I'd want category A to be the first and B to be the secound, C the third, etc. Than, if I'd have 2 items as A, I'd want those to be in order by date, so the most recent would be on the top of the list. This would mean that a category A item would be above a category B item, even if it's older.

  if (!count($news_headlines)>0){
            echo 'Nothing to display for the moment. ';
        }else{
            foreach ($news_headlines as $key => $item){
                list($news_id,$news_date,$news_title,$news_body,$news_category) = $item;
                $formatted_date = date('d.m.y',$news_date);


                if($news_category == 'A') {
                    $color = '#FF0000';
                    $weight = 'bold';
                }

                else if($news_category == 'B') {
                    $color = '#FF9900';
                    $weight = 'normal';
                }

                else if($news_category == 'C') {
                    $color = '#000000';
                    $weight = 'normal';
                }

                else {
                    $color = '#33CC33';
                    $weight = 'normal';
                }





                echo '<h2><p style="color: '.$color.'; font-weight: '.$weight.';">'.$formatted_date.' - '.$news_category.' - '.$news_title.'</p></h2>';
5
  • 1
    Looks like you are trying to do the same thing I wanted to do a few months back. Check my link here stackoverflow.com/questions/13605937/… Commented Apr 1, 2013 at 11:54
  • @deceze It isn't, looked at a ton of those before asking the question. You're right about how similar those may be, but none of them worked in my case. Thank you for your quick answer! Commented Apr 1, 2013 at 11:55
  • @Bolli looked at that too, the main difference is that in my case the values are already stored in the CSV file. Thank you though! Commented Apr 1, 2013 at 11:59
  • @MSZ What have you tried and what didn't work? Where do those cases differ from yours? You have to read the entries into an array, then sort it by a nested key. Both of these things have been explained sufficiently in other answers. Commented Apr 1, 2013 at 13:19
  • @deceze that's what dreamCoder did in his Answer if I'm right - yet it doesn't work properly for some reason, although it was very useful. Could you perhaps help me out with that, please? Commented Apr 1, 2013 at 13:31

2 Answers 2

1

First you need to create an array then sort .. Try something like this

      if (!count($news_headlines)>0)
      {
        echo 'Nothing to display for the moment. ';
      }
      else{
            $req_array = array();
            foreach ($news_headlines as $key => $item)
            {
              list($news_id,$news_date,$news_title,$news_body,$news_category) = $item;
              $formatted_date                                = date('d.m.y',$news_date);
              $req_array[]['id']                       = $news_id;
              $req_array[sizeOf($req_array)-1]['date'] = formatted_date;  
              $req_array[sizeOf($req_array)-1]['title'] = $news_tile;
              // similarly add othe values to this array
            }

            foreach($req_array as $key=>$value)
            {
                $category[] = $value['category'];
                $date[]     = $value['date'];   
            }
            $req_array = array_multisort($category,SORT_ASC,$date,SORT_DESC,$req_array);
           ----
          // rest code

now $req_array should contain the desired array ..

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

2 Comments

sorry, I was wrong the last time - it does modify the order I beleive, however only displays 1 item (1 row)
@MSZ what is the output of $req_array .. ?
1

Very simple example, assuming an array structure like this, which is already read from the CSV file:

$array = array(
    array('category' => 'A', 'date' => 1364823620, ...),
    array('category' => 'B', 'date' => 1364823610, ...),
    ...
);

The timestamps are UNIX timestamps for the sake of simplicity.

usort($array, function (array $a, array $b) {
    static $categoryOrder = array('B', 'A', 'C'); // arbitrary preferred order

    $order = array_search($a['category'], $categoryOrder) - array_search($b['category'], $categoryOrder);
    if ($order !== 0) {
        // different categories, order by category
        return $order;
    }

    // same category, order by timestamp within category
    return $a['date'] - $b['date'];
});

5 Comments

Thank you for your answer! The thing I don't get is the beginning: does it read the data from the file, or do you manually put it into the code? (Because in the first part it seems like you manually put it in the code)
As I wrote, I am just assuming this array structure for illustration purposes, but you have to read this from the CSV file. I am demonstrating the ordering logic, I assume you already have the CSV reading part down and can handle the output.
So, would it be like this: $array = array( array('id','date','title','body'.'category'), );
I use this to read from the CSV: code(public function readCSV(){ $newsarray = array(); $handle = fopen($this->csv_url, "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $newsarray[$data[0]] = $data; } fclose($handle); $this->news_array = $newsarray; })code
Where exactly are you stuck now, what is the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.