Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Go to next/previous page using php folder array

Featured Replies

Hi, I've got a PHP array menu of all folders of a directory which is ordered using the "Arraysortdate" value in each page folder's Json file instead of the folder names. 

I've basically got everything sorted now, just this one last thing that I realise I like to have.

A previous/next page button on each page that takes the current page, matches it with the value in the all folders array, and links to the next/previous page in that array which is sorted like previously mentioned using the "Arraysortdate".

My json files are structuted as follows:

{
  "Arraysortdate": "2018-07-16-1",
  "Month": ["July 2018"],
  "Category": [
    "testcat1",
    "testcat2"
  ]
}

Here is the PHP I'm using for grabbing all folders and ordering them using the Arraysortdate value for each:

<?php
	// initial array containing the dirs
	    $dirs = glob('*/posts/*', GLOB_ONLYDIR);
	// new array with date as key
    	$dirinfo_arr = [];
        foreach ($dirs as $cdir) {
	
		// get post info from file
            $dirinfo_str = file_get_contents("$cdir/includes/post-info.json");
			$dirinfo = json_decode($dirinfo_str, TRUE);
		
        // add current directory to the info array
            $dirinfo['dir'] = $cdir;
        // add current dir to new array where date is the key
            $dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo;
    	}
		
	// sort new array
        krsort($dirinfo_arr);
?>

Using print_r to dump the array contents, this will return for an item with say the json data as above:

Array ( [0] => 2018-07-16-1 [1] => Array ( [0] => July 2018 ) [2] => Array ( [0] => testcat1 [1] => testcat ) [3] => blog/posts/test-blog-post )

I use this after sorting the array to output as just the folder path for using in html:

<?php

	// get current dir from new array
	    foreach($dirinfo_arr as $key=>$dir) {
	    	$dirpath = $dir['dir'];
?>

    <!--HTML such as-->
    <a href="<?=$dirpath?>">text</a>
<?php
}
?>

 

1 hour ago, jwbjnwolf said:

Hi, I've got a PHP array menu of all folders of a directory which is ordered using the "Arraysortdate" value in each page folder's Json file instead of the folder names. 

I've basically got everything sorted now, just this one last thing that I realise I like to have.

A previous/next page button on each page that takes the current page, matches it with the value in the all folders array, and links to the next/previous page in that array which is sorted like previously mentioned using the "Arraysortdate".

Errrm, just trying to understand the question. Are you trying to find out which page is currently being viewed in the 'all' array? If so, is it the [3] => blog/posts/test-blog-post bit of your array you want to match?

  • Author
11 minutes ago, BrowserBugs said:

Errrm, just trying to understand the question. Are you trying to find out which page is currently being viewed in the 'all' array? If so, is it the [3] => blog/posts/test-blog-post bit of your array you want to match?

Yes I'm wanting to get the current page folder being viewed to be marked as current or something in the array, and then have links at the bottom of my posts that go to the previous/next folders in the array, just as you a lot of times have with blogs. As got almost all my posts moved over and realised now that I definitely want the next/previous post links lol.

Edited by jwbjnwolf

On 7/16/2018 at 7:32 PM, jwbjnwolf said:

Yes I'm wanting to get the current page folder being viewed to be marked as current or something in the array, and then have links at the bottom of my posts that go to the previous/next folders in the array, just as you a lot of times have with blogs. As got almost all my posts moved over and realised now that I definitely want the next/previous post links lol.

You're referring to something called pagination. There are plenty of php classes that have the functionality to do exactly what you want. A class off the top of my head would be Zebra Pagination https://github.com/stefangabos/Zebra_Pagination

Edited by webdeveloper93

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.