Jump to content


  • Content count

  • Joined

  • Last visited

About jwbjnwolf

  • Rank
    Forum Newcomer
  • Birthday April 28

Contact Methods

  • Website URL

Profile Information

  • Gender
  • Location
  1. jwbjnwolf

    Go to next/previous page using php folder array

    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.
  2. 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 } ?>
  3. Ok thanks! Worked it out that needs to be just before the foreach array slice line. Got the pagination and pager scripts in their own files including in the array script, so keeps the array script files very neat as well as obviously don't need to duplicate the pagingation/pager scripts for other array scripts for other parts of my site etc. Thanks for all the help
  4. Brilliant thanks! Something I worked out yesterday whilst I was waiting for you to get back on that, how to print the categories from the json into the html as links to the categories of the same names. As well as also finding how to use htaccess for changing uppercase to lowercase. Very impressed with all this now. Lastly, how do I can I add this pager in another place as well such as above the folder array at the top of the page so that people can use it without scrolling down?
  5. Brilliant. Played about with css as well as changing the active page from <span> to <a class="active">, and have it looking quite nicely! Though, one thing I notice is previous keeps going back to page 1 regardless of current page. That's the only thing that's now been done incorrectly. Is that because I've changed <span> to <a class="active">? Edit: Just changed the last text to last page number and removed the first, as really knowing the total pages is all that's needed really, and allows me to have two number buttons each side of the active page without it going off onto a second row on mobile, whilst keeping it at a reasonable size.
  6. I think it may be that the script doesn't like "?=" being right after "/" in thr url or something as I also tried with having "{$_SERVER['REQUEST_URI']}" in but that did the same thing. So I've just simply put "index" and now that works as it should. Now to play about with styling the pager for my liking. Though any possibility of adding first/last buttons which show accordingly like the prev/next buttons do?
  7. Spent a while actually getting a better understanding of what you originally sent. So ['folder'] and ['dir'] is the same thing pretty much, so all I needed to do actually was simply replace foreach($dirinfo_arr as $key=>$dir) { $dirpath = $dir['dir']; with foreach(array_slice($dirinfo_arr, $offset, $rowsperpage) as $dir) { $dirpath = $dir['dir']; That works flawlessly. Now regarding the pager navigation.. The 1st page link keeps reloading the current page not page=1, and the prev button doesn't load the last page either. The only difference I've done with your code for the pagination is as you said, make the pagination_url empty so that it's /?page=# instead of /index.php?page=#. Also, regarding the page titles, the logic there I am doing is for on say a monthly archive page such as 2017/04, which has a title "April 2017". I am taking this title to use as key to only include posts with a matching key "April 2017" in post's post-info.json file. I did try your multi dimentional array, though that is not sorting by date, only by the folder names. But anyway it is working now with the original script just changing the above, so now to get the pager sorted. And that multi dimentional array is quite a bit longer whilst excluding where I want to filter the array as described for the script being used by the category/month menu pages. I use this script in 3 differing include files, so keeping it that much shorter does visually help.
  8. Right I've included just two results for ease of understanding, along with the post-info.json contents for each. 1: print_r result: Array ( [0] => 2018-05-03 [1] => Array ( [0] => May 2018 ) [2] => Array ( [0] => Tutorials ) [3] => tech/posts/test2 ) Contents of json file: { "Arraysortdate": "2018-05-03", "Month": ["May 2018"], "Category": ["Tutorials"] } 2: print_r result Array ( [0] => 2017-04-03 [1] => Array ( [0] => April 2017 ) [2] => Array ( [0] => Attention [1] => Friendships [2] => Love [3] => Love & Hate [4] => Pressure ) [3] => staystrong/posts/test-blog-post ) Contents of json file: { "Arraysortdate": "2017-04-03", "Month": ["April 2017"], "Category": [ "Attention", "Friendships", "Love", "Love & Hate", "Pressure" ] } This script is just taking the value of Arraysortdate to order the posts by date in the menu. But if I also want to filter posts to only a certain category/month, I take the title of the current category/month page that the menu is on, and use in_array to only include posts if the value of either Month or Category matches the page title,, which is used as key. // get current page title from file $pagetitle = file_get_contents("includes/title.php"); // before cycling through $dirinfo_arr for output if(!in_array($pagetitle, $dirinfo['Month']) && !in_array($pagetitle, $dirinfo['Category'])) { continue; } This is done before this part of the script: // add current dir to new array where date is the key $dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo; }
  9. <?php // initial array containing the dirs $dirs = glob('*/posts/*', GLOB_ONLYDIR); // new array with date as key $dirinfo_arr = []; foreach ($dirs as $cdir) { // read date 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; } // now we sort the new array krsort($dirinfo_arr); foreach($dirinfo_arr as $key=>$dir) { // getting current dir from new array $dirpath = $dir['dir']; ?> <div class="paragraph blog-menu-item paging-menu-item"> <img class="blog-img lazy-load lazy-hide" data-src="<?=$dirpath?>/images/main.jpg"/> <noscript> <img class="blog-img" src="<?=$dirpath?>/images/main.jpg"/> </noscript><br><br> <a href="<?=$dirpath?>"> <h1><?php include("$dirpath/includes/title.php");?></h1> </a><br> <span class="blog-date"> <strong>Date Published:</strong> <?php include("$dirpath/includes/date.php");?> </span><br><br> <span> <?php include("$dirpath/includes/post-summary.php");?><br><br> <a href="<?=$dirpath?>" style="color:darkred">Continue reading ></a><br><br> </span> </div> <?php }; ?> This is my PHP script for doing my menu, where $dirpath is the final variable for each array item. Would I change $your_results to $dirpath and if I want the curent page as the base page, I assume I can just put "index.php" meaning as the index.php in the current folder? Regarding the second part, I assume I take the echo $sa['folder'] and to add that in the way I have been using $dirpath as in my original script, assign another variable to that echo so I just am then able to do the same like this? <?php foreach(array_slice($dirpath, $offset, $rowsperpage) as $sa) { $dirpaginpath = echo $sa['folder']; ?> <div class="paragraph blog-menu-item paging-menu-item"> <img class="blog-img lazy-load lazy-hide" data-src="<?=$dirpaginated?>/images/main.jpg"/> <noscript> <img class="blog-img" src="<?=$dirpaginpath?>/images/main.jpg"/> </noscript><br><br> <a href="<?=$dirpaginpath?>"> <h1><?php include("$dirpath/includes/title.php");?></h1> </a><br> <span class="blog-date"> <strong>Date Published:</strong> <?php include("$dirpaginpath/includes/date.php");?> </span><br><br> <span> <?php include("$dirpaginpath/includes/post-summary.php");?><br><br> <a href="<?=$dirpaginpath?>" style="color:darkred">Continue reading ></a><br><br> </span> </div> <?php } ?> And then finally I take it that the last part is the PHP for the pager menu? Edit, seems that it doesn't like this throwing up that the array slice expects an array so using $dirinfo_arr with the slice outside of the bottom foreach seems more correct as it doesn't through up the error, but how do I handle the ['folder'] and ['dir'] with this? Changed back my html to have $dirpath like previously as I assume I go back using that now. Btw if you need to read up my thread where I built this script to get an understanding if your lost, here's the thread on Web Developer. Also, if a mod wants to move this to server side forum, feel free as I did mean to do javascript but if php will work then definitely doing that instead.
  10. Hi, I have a PHP array of all folders in a directory, and creating a list of divs from them all with a specified class. I have this jQuery script (CodePen link) that just simply requires me to specify the class of what elements I want to paginate, as well as have a place holder element for the pager below. That works fine. No other things needed. But as of right now, this is a very basic script where the pager numbers just keep adding up. What I want to add is the previous/next/first/last buttons as well as say a limit of 5 numbers shown. How can I add this to the script or is there any scripts with this already, that work just as simply like this, just dynamically paginating content of a specified class without doing anything more? All I can seem to find are scripts requiring bootstrap, databases, editing the html to differ each item or putting the dynamic content into the javascript file. Thanks, Jase