Hi, I have a PHP foreach array obtaining each folder inside a ‘files’ folder. By having *YYYYMMDD
That works fine, but if I want to change a date, that means changing the folder structure, so I would have to do redirects each time I do that.
Instead of having the YYYYMMDD in the folder names which the array is currently ordered by, is there any way I can use a $date variable assigned to get_file_contents to use YYYY-MM-DD written in a date.php file to organise the array?
Here’s my current php script:
<?php
foreach(array_reverse(glob(‘files/*’, GLOB_ONLYDIR)) as $dir) {
$dir = str_replace(‘files/’, ”, $dir);
?>
<!– html –>
<?php
}
?>
Now if I could do something in the lines of the below, that’d be great.
$date = file_get_contents(“$dir/includes/date.php”);
$sorted_dir = $dir;
rsort($dir by $date) as $sorted_dir;
I know that “`rsort($dir by $date) as $sorted_dir;
[My last thread here*YYYYMMDD
I thought that would I could do with that, have YYYYMMDD as parent folders instead, so I can then just hide them in the url. But it seems like you cannot do that as they’ll conflict.
So even though it’s linked this to my last thread, seperating as it is quite a different method I’m asking here.
If I can do in anyway this with sorting the array using the date.php, then that’ll be great.
Look forward to hearing back.
// initial array containing the dirs
$dirs = glob('files/*', GLOB_ONLYDIR);
// new array with date as key
$dirs_date = [];
foreach ($dirs as $cdir) {
// read date from file
$date = file_get_contents("$cdir/includes/date.php");
// add current dir to new array where date is the key
$dirs_date[$date] = $cdir;
}
// now we sort the new array
ksort($dirs_date);
Untested, I didn't have time to create an appropriate structure.$dirs_date
`</C> into the path. However it's just turning up an as "Array" instead of the path for each item. If I place <C>
`$cdir
``<div class="download-menu-item">
<a href="<?=$dirs_date?>">
<img class="download-menu-image" src="<?=$dirs_date?>/images/main/thumbs/image-small-thumb.jpg"/>
<span class="download-menu-title">
<?php include("$dirs_date/includes/title.php"); ?>
</span>
</a>
</div>
<?php
foreach(array_reverse(glob('files/*', GLOB_ONLYDIR)) as $dir) {
$dir = str_replace('files/', '', $dir);
?>
<div class="download-menu-item">
<a href="files/<?=$dir?>">
<img class="download-menu-image" src="files/<?=$dir?>/images/main/thumbs/image-small-thumb.jpg"/>
<span class="download-menu-title">
<?php include("files/$dir/includes/title.php"); ?>
</span>
</a>
</div>
<?php
}
?>
<?php
foreach($dirs_date as $key=>$dir) { // this line changed, getting the current dir from the new array
$dir = str_replace('files/', '', $dir);
?>
<div class="download-menu-item">
<a href="files/<?=$dir?>">
<img class="download-menu-image" src="files/<?=$dir?>/images/main/thumbs/image-small-thumb.jpg"/>
<span class="download-menu-title">
<?php include("files/$dir/includes/title.php"); ?>
</span>
</a>
</div>
<?php
}
?>
``<i>
</i>Arraysortdate: "2018-06-25"
Month: "June 2018"
Category: "cat1", "cat2"…etc
Tags: "tag1", "tag2", "tag3"…etc<i>
</i>
``
{
Arraysortdate: "2018-06-25",
Month: "June 2018",
Category: ["cat1", "cat2"],
Tags: ["tag1", "tag2", "tag3"]
}
Then you could read this information in two lines:$string = file_get_contents(""$cdir/includes/date.json"");
$json_a = json_decode($string, true);
// initial array containing the dirs
$dirs = glob('files/*', 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/date.php");
$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']] = $cdir;
}
// now we sort the new array
ksort($dirinfo_arr);
archives/2018/05/
`</C> with <C>
`includes/title.php
`</C> storing the name for each month page.
I am using my original script to generate a monthly archive link list on the side of the page, that all is working by just changing the glob path to have another <C>
`/*
`</C> at the end as well as change <C>
`GLOB_ONLYDIR
`</C> to <C>
`GLOB_BRACE
`</C>.
However as I am including the title.php into the generated link text, I am having errors throw up for the year directories which don't have a <C>
`includes/title.php
``/*/*
`</C> instead of just one <C>
`/*
`</C>, along with changing <C>
`GLOB_ONLYDIR
`</C> to <C>
`GLOB_BRACE
`</C> to grab all folders.
The php script has no error itself here. It's the includes that are doing that.
But if I can set say the first <C>
`/*
`</C> to <C>
`$year
`</C> and the second to <C>
`$month
`</C>, so in the include path, I can put <C>
`../$year/$month/..
``$dirinfo['Arraysortdate']
`</C>, then it'll load the rest but not ordered due to not having anything to order with.
<CODE>
`<i>
</i><?php
// initial array containing the dirs
$dirs = glob('blog/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']] = $cdir;
}
// now we sort the new array
krsort($dirinfo_arr);
foreach($dirinfo_arr as $key=>$dir) { // this line changed, getting the current dir from the new array
?>
<!--HTML-->
<?php
}
?><i>
</i>
`</CODE>
Also, I do, like in my last response, need to figure out getting GlOB_BRACE to ignore current the parent directories when I here (as I have 2 blogs) do <C>
`$dirs = glob('{blog1/posts/*,blog2/posts/*}', GLOB_BRACE);
``K
I
S
S
S
S
$dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo;
$dirinfo_arr[$dirinfo['Arraysortdate']] = $cdir;
{
"Arraysortdate": "2018-07-27",
"Month": "June 2018",
"Category": ["cat1", "cat2"],
"Tags": ["tag1", "tag2", "tag3"]
}
That's returning as "Array"[/quote]I assume that this is correct - $dirinfo_arr is an array of two dimension s now. Try this for the second loop:
foreach ($dirinfo_arr as $key => $dir) { // this line changed, getting the current dir from the new array
?>
<!--HTML-->
directory is: <?php echo $dir['dir']; ?>
<?php
}
?>
$dirs = glob('{blog1/posts/*,blog2/posts/*}', GLOB_BRACE);
// 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);
var_dump($dirinfo_arr);
foreach ($dirinfo_arr as $key => $dir) { // this line changed, getting the current dir from the new array
?>
<!--HTML-->
directory is: <?php echo $dir['dir']; ?>
<?php
}
?>
Still don't understand what the issue is?includes/title.php
`</C> of the current page, to decide which filter should be applied (Same exact name in the title.php as the value in the json).
As I will just be having one version of the array including to get what array filter should be applied, and including what the page name of the current page is (in the title.php) to decide on what value to use for the filter.
Oh and btw I've added under the last glob for <C>
`dir
`</C> line, <C>
`dirpath = $dir['dir'];
``how do I go about making the array see the includes/title.php of the current page[/quote]
``<i>
</i>- archives/
---- 2018/
-------- 06/
----------- index.php
--------------- includes/
--------------- title.php<i>
</i>
`</CODE>
The title.php has the month name written same way as in the post-info.json files. So for 2018/06, it's <C>
`June 2018
`</C>.
So by setting the filter to grab the title.php of the current pages via <C>
`includes/title.php
`</C>, for 2018/06, it'll be reading <C>
`June 2018
`` to filter to only posts of that month as stated in the post-info.json. As this is in a different directory the month pages to the posts, I assume I use the server document root option in the glob for the posts.
By doing this using the title.php to choose the value to filter by, this will mean it'll be the same for categories/tags too.
``<i>
</i>jasewolf/
blog1/posts/*blogpost*/includes/
post-info.json
blog2/posts/*blogpost*/includes/
post-info.json<i>
</i>
`</CODE>
<CODE>
`<i>
</i>jasewolf/
archives/2018/06/includes/
title.php<i>
</i>
`</CODE>
The archives folder is placed outside of blog1 & 2 because it's for both.
For each month page, where I have the array filtering by month (same applies on a categories/tags page), I only need to link to <C>
`includes/title.php
`</C> because I'm in the month folder already.
For the glob getting the blog posts, the difference here is I will be doing the path from the root as I am not on the blogpost pages. Here I understand I use the server document root to go start from /jasewolf.
Getting the <C>
`post-info.json
`</C> file, the only difference there is I include <C>
`$cdir
`</C> in the path instead of <C>
`includes
`</C>.
If I all I really am asking for regarding now how is how I use the title.php with contents
``June 2018" to filter the array to only blog posts with "June 2018" in the post-info.json.
Hopefully I have this more clear!
// initial array containing the dirs
$dirs = glob('archives/*/*', GLOB_ONLYDIR);
// new array containing month
$dirinfo_arr = [];
foreach ($dirs as $cdir) {
// read date from file
$cmonth = file_get_contents("$cdir/includes/title.php");
$dirinfo_arr[$cmonth] = $cdir;
}
var_dump($dirinfo_arr);
echo();
`</C> to test things are turning out right. I've discovered that whilst <C>
`$dirs
`</C> is changing the path for each array item as it should, <C>
`$cdir
`</C> is just using the first array item's path. Here's an example of that happening.
The directory I currently am in is <C>
`{public_html}/testblog/archives/2017/04
`</C>. <C>
`testblog
`</C> is where <C>
`jasewolf
`</C> is in your test structure shown in post 24. <C>
`staystrong
`</C> & <C>
`tech
`</C> is the <C>
`blog1
`</C> & <C>
`blog2
`</C> directories.
The first thing in my PHP I am doing is assigning a <C>
`$BASE_PATH
`</C> to the <C>
`public_html
`</C> directory. Then I am globing to <C>
`$dirs
`</C> to each directory inside of tech/ & staystrong/ <C>
`posts
`</C> directory using:
<CODE>
`<i>
</i>// initial array containing the dirs
$BASE_PATH = /'path/to/public_html';
$dirs = glob($BASE_PATH.'/testblog/*/posts/*', GLOB_ONLYDIR);<i>
</i>
`</CODE>
I've learnt I don't need to use GLOB_BRACE at all because I can simply put <C>
`*/posts/*
`</C> instead of specifying the 2 blog folders. The plus side with this is I can make even more blog folders if I want and the code will immediately get them too.
Next I'm assigning <C>
`$dirs
`</C> to <C>
`$cdir
`</C>. and opening a new array.
<CODE>
`<i>
</i>// new array with date as key
$dirinfo_arr = [];
foreach ($dirs as $cdir) {<i>
</i>
`</CODE>
In that new array, I am grabbing the current page title "April 2017" from the current page's <C>
`includes/title.php
`</C> and assigning it to <C>
`$pagetitle
`</C>:
<CODE>
`<i>
</i>// get current page title from file
$pagetitle = file_get_contents("includes/title.php");<i>
</i>
`</CODE>
Next I am getting the <C>
`post-info.json
`</C> for each post:
<CODE>
`<i>
</i>// get date & post info from file
$dirinfo_str = file_get_contents("$cdir/includes/post-info.json");
$dirinfo = json_decode($dirinfo_str, TRUE);<i>
</i>
`</CODE>
Then I am adding the current dir to the array with date as key, then closing the array, then sorting the array:
<CODE>
`<i>
</i>// 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); <i>
</i>
`</CODE>
Then I start the new array, as well as assigning <C>
`$dir['dir']
`</C> to <C>
`$dirpath
`</C>, and then remove the folders before the public_html in the path. (Also did it for <C>
`$cdir
`</C> for purposes of the screenshot)
<CODE>
`<i>
</i>foreach($dirinfo_arr as $key=>$dir) {
$dirpath = $dir['dir'];
$dirpath = str_replace('home/jasewolfco/public_html/', '', $dirpath);<i>
</i>
`</CODE>
Now to test how things are in the array, I am using echo() before I close te array:
<CODE>
`<i>
</i>echo("
Blog post: $dirpath <br>
Current page: $pagetitle <br>
Blog post info: $dirinfo_str <br>
Reason for error: $cdir <br><br>
");
};<i>
</i>
`</CODE>
As you see in the screenshot below, <C>
`$cdir
`</C> is only getting one directory and reusing it, therefore the <C>
`post-info.json
`</C> data is repeating that one directory's <C>
`post-info.json
`</C> which it's doing not based on any of the differences I've added.<br/>
<UPL-IMAGE-PREVIEW url="https://www.webdeveloper.com/forum/assets/files/2018-06-27/1530097609-192237-screenshot-2018-06-27-11-45-08.png">[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-06-27/1530097609-192237-screenshot-2018-06-27-11-45-08.png]</UPL-IMAGE-PREVIEW>
I'm guessing this is something to do with<C>
`foreach ($dirs as $cdir)
``$pagetitle
``['Category']
`</C>, or <C>
`['Tag']
`</C> after <C>
`$dirinfo
`</C>, I have to change my json to have categories & tags seperated on new lines as Category/Tag1, 2, 3 etc, so I can use the same exact script for months too as well. The trouble here though is if I want to print all categories/tags of a post in the page, that's more work with them seperated like that.
<CODE>
`<i>
</i>// before cycling through $dirinfo_arr for output
if(!in_array($pagetitle, $dirinfo)) {
continue;
}<i>
</i>
`</CODE>
Now I've added a <URL url="https://stackoverflow.com/questions/31920360/dynamic-pagination-in-jquery">[pagination js script (from StackOverFlow)](https://stackoverflow.com/questions/31920360/dynamic-pagination-in-jquery)</URL> that just takes all <C>
`.blog-menu-item
`</C> items from the page, dynamically creating a pagination.
Three issues I've found though is there's no back/forward buttons, no numbers being added after the hash in the url so they don't stick when reloading. And then they just seem kinda odd the way they just instantly switch to content for each page because obviously it's the same page. But that most probably is just me finding it weird also with the same content duplicated for testing. So I just need to stay looking finding a really nice paginating script that just takes a particular class of content without needing to specifiy anything else.
But anyway besides, the only thing I need to work out now is how I can expand in using the json for content to print out onto pages. So say the categories and tags, print them out hopefully also with some way of linking to the category/tag pages of the same names preferably without caps. And then by working that out how to print specific info, that'll also help with using a json for page titles and have all my specific download page info that I currently have set as seperate files being included, all in one place instead. Oh yea and then just work out how I create rss xmls which I know I have to do myself due to it being not a CMS. But working with folders, not something you really want automated having the RSS record what you may not want. But then having a test post folder or something will obviously be a good idea.
But anyway, thanks for all the big help <USERMENTION id="158406" username="Sempervivum">@Sempervivum</USERMENTION>. Really learnt some things here from all this. Maybe it's not the best or normal method of doing things where I'm not using a database or a cms. But coding is all about learning things, maybe not the correct way and finding out why they it wasn't correct yourself. I have a fair amount of posts from over the years I need to copy over before I am actually using this, so I'll see how things go.
Wish Flarum, Nodebb and Discourse had a solved feature so you could put the solution to the question at the top like a lot of other forums use. But anyway, I'll end this here considering I I'm done regarding array scripts in the way this thread was meant for.
Edit: Well that is very sweet that php supports <C>
`&&
`</C> just like in the way that you use && for commands in Unix/Linux. So now I have a script that works exactly how I want and it's just one script needed regardless if it's for tag, category or month pages by just repeating the <C>
`!in_array()
`</C> before closing the <C>
`if()
``