July 16, 20188 yr 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 } ?>
July 16, 20188 yr 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?
July 16, 20188 yr 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 July 16, 20188 yr by jwbjnwolf
July 16, 20188 yr Hmm ok I figure you'll need $_SERVER['REQUEST_URI'] to grab the current page, array_search to find the key where the url matches and then I guess the returned index-1 for previous and index+1 for next?
July 21, 20188 yr 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 July 21, 20188 yr by webdeveloper93
Create an account or sign in to comment