PHP Sorting issue -
this question has answer here:
i'd ask help. have csv file in there values stored in 4 different categories. ( b c d ) have these values displayed in order date, however, i'd page display them category, , after sort them date.
$_news_date date stored, , $news_category category stored.
for example: i'd want category first , b secound, c third, etc. than, if i'd have 2 items a, i'd want in order date, recent on top of list. mean category item above category b item, if it's older.
if (!count($news_headlines)>0){ echo 'nothing display moment. '; }else{ foreach ($news_headlines $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>';
first need create array sort .. try
if (!count($news_headlines)>0) { echo 'nothing display moment. '; } else{ $req_array = array(); foreach ($news_headlines $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; // add othe values array } foreach($req_array $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 desired array ..
Comments
Post a Comment