"; // first of all, we need to pull all the filenames that MIGHT contain stories we need, so we can narrow them down. // the directory we need to read from is _Pagenumbers. $dir = './_Pagenumbers/'; // now we open the directory and read each filename in it. $dh = opendir($dir) or die(); while (false !== ($filename = readdir($dh))) { // if the last four characters of the filename are ".csv", and the first four characters aren't "news" or "__ar", then it's one of the filenames we need, // because it's one of our ordinary volume csv files. if((substr($filename,-4) == '.csv') && (substr($filename,0,4) != 'news') && (substr($filename,0,4) != '__ar') && (substr($filename,0,4) != '1320') && (substr($filename,0,4) != 'temp')&& (substr($filename,0,4) != '-tem')) { // just like in pager.php , our volume/issue information is in the first part of the filename. so split the filename up on dashes, // then $temp is the actual volume and issue (retrieved by splitting the first part of the filename by underscore. $volume = explode("-",$filename); $temp = explode("_",$volume[0]); // here we add zeroes to the issue numbers for proper numerical sorting by str_padding the string to 3 places. $temp[1] = str_pad($temp[1], 3, "0", STR_PAD_LEFT); // now we assign $files as an array where each line is of the form $temp0-$temp1. ([volume]-[issue]). $files[] = $temp[0]."_".$temp[1]; } } // now sort the files ordinarily. This gives us a weird order for the volumes (which are roman numerals), but puts all of the issues in correct order. sort($files); // We're going through all of the following rigmarole because romansort CANNOT BE COUNTED ON to sort by anything BUT roman numerals. // that means our ISSUE numbers don't necessarily end up in order! // this is just an iterator variable. $j = 0; // iterate through all the lines in $files. in each line, pull BACK out the volume and issue number by splitting it along dashes. // then put it into a temp array where the zeroth value in a line is the whole line and the first value is the volume. foreach($files as $key => $line) { $temp = explode("_",$line); $temparr[$key][0] = $line; $temparr[$key][1] = $temp[0]; } // Obtain a list of columns as separate arrays, because we can't do some operations on a multidimensional array. foreach ($temparr as $key => $row) { $whole[$key] = $row[0]; // the whole volume+issue $vol[$key] = $row[1]; // just the volume } // now we sort JUST THE VOLUMES by roman numeral. romansort($vol); // grab the last volume number or numbers from our roman-numeral-sorted array, by getting the last two values out of $vol, then uniquing them. $newest = array_unique(array_slice($vol,-2,2)); // now iterate through $allissues, and grab ALL the issues that match the volume numbers we just grabbed. foreach($temparr as $key => $line) { // (we don't know if there are one or two values in $newest, so we need to do a for loop here to compare volume numbers.) for($i = 0; $i < sizeof($newest); ++$i) { $newpcs = $newest[$i]; $newpc2 = explode("_",$line[1]); if($newpcs == $newpc2[0]) { // if we match on volumes, then $newissues is the WHOLE value out of $allissues. (Remember, that includes volume+issue), preceded by the // roman numeral converted to arabic numerals and padded to 8 places for sorting. $newissues[] = str_pad(roman2num($line[1]), 8, "0", STR_PAD_LEFT).".".$line[0]; } } } // sort the issues. this should sort on that first volume number, and sort them in descending order, so we'll absolutely get the right volume numbers. rsort($newissues); // iterate through our current array and remove all the extraneous zeroes, as well as our sorting numbers! foreach($newissues as $key => $line) { // explode at the "." to retrieve our padded sort number. $line = explode(".",$line); // then ditch it by setting $line to just $line[1]. $line = $line[1]; // now replace out the zeroes that we put in way back when we first generated the sortable issue numbers. $line = str_replace('_00','_',$line); $line = str_replace('_0','_',$line); // this puts our edited $line into a new array. (what we work with in an iteration is basically a copy.) $newissues[$key] = $line; } // this is where we use the importcsv function from extrafunctions.php . It takes a .csv file as input and returns an array of values. // the other parameters: $head=true says that our file has a header row; $delim sets the delimiter; $len sets the max line length. // the first file we want to use is the csv file of $newissues[0], which we KNOW exists (there will be at least one issue in one volume). $filetouse = "./_Pagenumbers/".$newissues[0]."-pages.csv"; $pagearray = importcsv($filetouse,$head=false,$delim=",",$len=1000); // get rid of the top row, because it's the header info. array_shift($pagearray); // there may be more issues pulled; if so, also import the SECOND csv file we find (remember, we're pretty sure they're sorted at this point.) if(sizeof($newissues) > 1) { $filetouse2 = "./_Pagenumbers/".$newissues[1]."-pages.csv"; $pagearray2 = importcsv($filetouse2,$head=false,$delim=",",$len=1000); // get rid of the top row again, because it's still the header info. array_shift($pagearray2); } // now, all recent stories should include all stories from BOTH arrays, so we merge them and go from there. $allrecent = array_merge($pagearray,$pagearray2); // for each of those pages, we go through and see if the page has a date (making it the FIRST page of an article). We ONLY want those. foreach($allrecent as $key => $line) { // if there's a date, and it's not just a space (because spaces may get accidentally introduced here). if($line[7] && $line[7] != "" && $line[7] != '') { // add a new value to $line, which is the date converted to UNIX timestamp for sorting. $line[11] = strtotime($line[7]); // now make ANOTHER new array with only dated articles. $dated[] = $line; } } // Obtain a list of columns in $dated[], so we can multisort. foreach ($dated as $key => $row) { $link[$key] = $row[0]; // href $title[$key] = $row[1]; // title of story $date[$key] = $row[11]; // UNIX timestamp $realdate[$key] = $row[7]; // actual human date $shortname[$key] = $row[8]; // linking name in case we need it. $teaser[$key] = $row[9]; // any teaser copy that needs to go with the What's New entry } // Sort the data with the UNIX timestamp descending, then title ascending; make sure the other columns get added back in by sorting them too. array_multisort($date, SORT_DESC, SORT_NUMERIC, $title, SORT_ASC, $realdate, $link, $shortname, $teaser); // an iterator variable $j = 0; // the MAXIMUM date we find, as timestamp. $maxdate = 0; // we want to iterate through every $date[] to create a new sorted array grabbing all the (sorted) columns, as well as figuring out some other info. for($i = 0; $i < sizeof($date); ++$i) { $sorteddates[$i][0] = $link[$i]; $sorteddates[$i][1] = $title[$i]; $sorteddates[$i][2] = $date[$i]; $sorteddates[$i][3] = $shortname[$i]; $sorteddates[$i][4] = $teaser[$i]; $sorteddates[$i][5] = $realdate[$i]; // if our current date value is greater than our current $maxdate, then we want to replace $maxdate with the current value and iterate $j to keep track. if($sorteddates[$i][2] > $maxdate) { ++$j; $maxdate = $sorteddates[$i][2]; } // this array is JUST the dates; we want to grab all those values out separately. $alldates[$i] = $sorteddates[$i][2]; } // array_count_values returns an array where key = [old value] and value = [count]. $alldates = array_count_values($alldates); // now we want to reverse sort by KEY, so we get the dates in descending order with their counts. krsort($alldates); // this is the number of stories we want to have displayed in What's New. $numtoget = 7; // these are just counters, again. $it = 0; $it2 = 0; // this is storage for a date if there are too many stories on the same date -- we'll need this later. $bigdate = 0; // iterate through each entry in $alldates, which you will recall is a sorted list of dates with their story count. foreach($alldates as $key => $line) { // if the current value of $it is less than the number of stories we want to get, then add the count for the current date to $it. // Keep track of JUST the current count in $it2. if($it < $numtoget) { $it = $it+$line; $it2 = $line; // if the new value of $it is greater than the number of stories, then store our overflowing date in $bigdate. if($it > $numtoget) { $bigdate = $key; // remember, our date is the KEY of an $alldates[] line. } } } // if $it is equal to the number of stories we want, we'll do nothing in this loop, and that's how we want it! // now we iterate through $sorteddates[] and separate out all of the stories on our overflow date, $bigdate, into a NEW array so they can be randomized. foreach($sorteddates as $key => $line) { if($line[2] == $bigdate) { $torandomize[] = $line; } } // IF there is a $bigdate value... if($bigdate > 0) { // the stories ABOVE our overflow date are the ones we DEFINITELY want. So we take $it, which is the total number on or above our overflow date, // and subtract $it2, which is the number that sit ON our overflow date. $numstories = $it-$it2; } // ELSE we just use the number of stories we wanted originally. else $numstories = $numtoget; // now we grab just the $sorteddates[] lines that are above $bigdate by using that number. $newsstories = array_slice($sorteddates,0,$numstories); // and then we randomize the stories from $torandomize[], grabbing out the keys for as many entries as we need to make up the total $numtoget. $randkeys = array_rand($torandomize,($numtoget-$numstories)); // iterate through each of the stories in $torandomize, and pulling out the ones that match the random keys. // we array_push those onto the end of $newsstories, so they're sorted to the bottom of the list where they should be. foreach($torandomize as $key => $line) { for($i=0; $i < sizeof($randkeys); ++$i) { if($key == $randkeys[$i]) { array_push($newsstories,$line); } } } // note that at this point, $newsstories is our FINAL ARRAY, containing all the .csv-pulled data for just the stories we want! ?>
    $line) { echo("
  • "); $link = $line[0]; // our href $title = $line[1]; // our title $rd = $line[5]; // our human-readable date $teaser = $line[4]; // our teaser copy // the link is structured like the following: // [$title] [$rd] // if there's a $teaser, it follows. if not, we STILL echo the

    , just for spacing. echo("$title [$rd]"); echo('

    '); echo $teaser; echo('

  • '); } ?>