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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.