MediaFire API: want to sort...

Status
Not open for further replies.

Mr. Goodie2Shoes

Active Member
74
2012
11
0
Hello again,
I am using MediaFire's API to share my stuff... I just post the folder link and the files are automatically parsed... and this is the code snippet that I use (including an example):
PHP:
<?php
$loadxml_folder_info = simplexml_load_file('http://www.mediafire.com/api/folder/get_info.php?folder_key=wl7j50kmccaci');
$max = $loadxml_folder_info->folder_info->file_count;
$output = '';
for ($i = 0; $i < $max; $i++) {
    $mf_url_link = 'http://www.mediafire.com/?'.$loadxml_folder_info->folder_info->files->file[$i]->quickkey;
    $mf_file_name = $loadxml_folder_info->folder_info->files->file[$i]->filename;
    $output .= '<a href="'.$mf_url_link.'" target="_blank" class="downloadBtn"><span><strong>'.$mf_file_name.'</strong></span></a><br />';
}
echo $output;
?>
As you can see, the files are shown perfectly, but the problem is that I want them sorted alphabetically... and I am not that good with PHP... so any help is welcomed :)
 
2 comments
Here:

PHP:
<?php
function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) {
        if(is_array($array) && count($array)>0) {
            foreach(array_keys($array) as $key)
            $temp[$key]=$array[$key][$index];
            if(!$natsort) {
                if ($order=='asc')
                    asort($temp);
                else   
                    arsort($temp);
            }
            else
            {
                if ($case_sensitive===true)
                    natsort($temp);
                else
                    natcasesort($temp);
            if($order!='asc')
                $temp=array_reverse($temp,TRUE);
            }
            foreach(array_keys($temp) as $key)
                if (is_numeric($key))
                    $sorted[]=$array[$key];
                else   
                    $sorted[$key]=$array[$key];
            return $sorted;
        }
    return $sorted;
}
$loadxml_folder_info = simplexml_load_file('http://www.mediafire.com/api/folder/get_info.php?folder_key=wl7j50kmccaci');
$max = $loadxml_folder_info->folder_info->file_count;
$output = '';
for ($i = 0; $i < $max; $i++) {
    $mf_url_link = 'http://www.mediafire.com/?'.$loadxml_folder_info->folder_info->files->file[$i]->quickkey;
    $mf_file_name = $loadxml_folder_info->folder_info->files->file[$i]->filename;
    $links[$i]['url'] = $mf_url_link;
    $links[$i]['filename'] = $mf_file_name;
}
$links = sortmulti($links,'filename','asc',true,true);
foreach($links as $link) $output .= '<a href="'.$link['url'].'" target="_blank" class="downloadBtn"><span><strong>'.$link['filename'].'</strong></span></a><br />';
echo $output;
?>
 
awesome! thanks!
I found another way myself:
PHP:
<?php
function sortByOneKey(array $array, $key, $asc = true) {
    $result = array();
       
    $values = array();
    foreach ($array as $id => $value) {
        $values[$id] = isset($value[$key]) ? $value[$key] : '';
    }
       
    if ($asc) {
        asort($values);
    }
    else {
        arsort($values);
    }
       
    foreach ($values as $key => $value) {
        $result[$key] = $array[$key];
    }
       
    return $result;
}

$loadxml_folder_info = simplexml_load_file('http://www.mediafire.com/api/folder/get_info.php?folder_key=wl7j50kmccaci');
$max = $loadxml_folder_info->folder_info->file_count;
$output = '';
$array = array();
for ($i = 0; $i < $max; $i++) {
    foreach($loadxml_folder_info->folder_info->files->file[$i]->quickkey as $mf_quickkey){
        $subarray = array();
        $subarray['key'] = (string)$loadxml_folder_info->folder_info->files->file[$i]->quickkey;
        $subarray['name'] = (string)$loadxml_folder_info->folder_info->files->file[$i]->filename;
    }
    $array[] = $subarray;
}
$sortedByNameAsc = sortByOneKey($array, 'name');
?>
but I guess yours is better and less complicated :)
 
Status
Not open for further replies.
Back
Top