Jump to content

foreach() sorting (like rsort)


codepoet

Recommended Posts

Hello!

 

I have the following code, but I want it to list the results in the opposite order.  Currently, it's listing it in numerical order (1, 2, 3, 4, etc), but I need it to list it in the opposite way (4, 3, 2, 1, etc). I've tried inserting rsort($pages) but that doesn't work.

 

The code:

 

<?php

if(!isset($_GET['pl'])){


# directory of the files
$allfiles = scandir('pages/');

# intialize a new array of files that we want to show
$goodfiles = array();

# add a file to the $goodfiles array if its name doesn't begin with a period
foreach($allfiles as $f){
if(strpos($f,'.')!==0){
	array_push($goodfiles,$f);
}
}

$pages = array_chunk($goodfiles, 5);

# pagination
for($i=1; $i< count($pages)+1; $i++): ?>
     <a href="?showpage=<?php echo $i;?>"><?php echo $i; ?></a>
<? endfor; ?>

<br />

<?php

$pgkey = (int)$_GET['showpage']; // forces $_GET['showpage'] to be an integer

if(!isset($_GET['showpage'])){
$pgkey = 1;
}

else if($_GET['showpage'] > $i++ or $_GET['showpage'] == null){
exit ("Sorry, the page you requested doesn't exist.");
}

$pages[$pgkey];

# display the paginated list
foreach($pages[$pgkey-1] as $file){ include('pages/'.$file); echo '<br /><br />'; }

} else {
$pl = $_GET['pl'];
$plpage = 'pages/'.$pl.'.txt';
if(file_exists($plpage)){
include($plpage);
} else {
	echo "Sorry, the prayer permalink '$pl' does not exist.";
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/182828-foreach-sorting-like-rsort/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.