Jump to content
fruid

5-dimensional array

Recommended Posts

so I basically have a 5-dimensional array.

main page
/ level 2 pages
// level 3 pages
/// level 4 pages
//// fields

Here's what I have so far:

    $level3 = $pages->find("template=level3");

    foreach ($level3 as $level4) {
        foreach ($level4->children as $thefields) {
            $level4_details = array(
                'field1' => $artikel->field1,
                'field2' => $artikel->field2,
                'field3' => $artikel->field3,
                …
            );
        };
    };


I need to store all the $level4_details arrays in another array so I can json_encode it, request with ajax and further process with javascript.

The above works alright in php but when I encode it to json, it only gives me the last of the last pages (level 4) because, I assume, with each foreach the array $level4_details is overwritten.

I also need to have all the $level4_details arrays to be named after their parent so I can reference them properly.

Thanks for help, a lot of this is probably rather general php than specific processwire understanding, help is therefore all the more appreciated. 

Share this post


Link to post
Share on other sites

Can we see your complete code? Because currently you're looping over the individual levels, but nothing gets added to your array, so I don't quite see how you're building your 5-dimensional array.

In any case, you can use array_map to convert an array of page objects to an array of page fields. This function expects a normal array, whereas $level4->children returns a WireArray. But you can use WireArray::getArray() to get the objects in the WireArray as a plain array:

$pagesData = array_map(function ($artikel) {
	return [
      'field1' => $artikel->field1,
      'field2' => $artikel->field2,
      'field3' => $artikel->field3,			
	];
}, $level4->children->getArray());

// $pagesData now holds an array of arrays corresponding to pages which in turn contain the fields for that page

 

  • Like 1

Share this post


Link to post
Share on other sites

the more I think of it, all I really need is an array of the last level with some information of the above leve.

So what I did is the following:

<?php

    $allarticles = $pages->find("template=article"); // find by template of the last level
    $catalogue = array();
    $i = 0;
    
    foreach ($allarticles as $onearticle) {
        $temp = array ( // create a temporary array for the lowest level
            'id' => $onearticle->id,
            'title' => $onearticle->title,
            'coverimage' => $onearticle->cover->url,
            'url' => $onearticle->url,
            'author' => $onearticle->author,
            'date' => $onearticle->date,
            'year' => $onearticle->parent->parent->title,
            'issue' => $onearticle->parent->title,
            'offline' => $onearticle->offline,
            'body' => $onearticle->body
        );
        $catalogue[$i] = $temp; // add the results of each foreach (i.e. an array) to the main array at position $i 
        $i++; // go to the next position of the main array 
            
    };

    print_r ($catalogue);
    
?>

This seems to kind of work (though I don't know why I can only use print_r and not echo).

The code is saved in test.php which is in the root folder.

Now I'm trying to request this php file via AJAX. 

    <script type="text/javascript">
      
        var request = new XMLHttpRequest();
        if (request) {
            request.onreadystatechange = ReloadRequest;
            request.open("GET", "test.php", true);
            request.send(null);
        } 
                    
        function ReloadRequest() {
            request.readyState;
            var str = request.responseText;
            document.getElementById("boxedcontent").innerHTML = (str);
        }
        
    </script>

This script is at the bottom of the template file that is applied to a specific page.

The code works when I request an html file but it doesn't work with a php file. I receive a "the server responded with a status of 500 (Internal Server Error)".

Also, when I put the test.php inside the template file, the header and footer and whatnot is prepended and appended which is obviously not what I need, that's why I moved it to the root folder. Maybe then it lacks permission to access it?

Thanks for help!  

Share this post


Link to post
Share on other sites
Quote

This seems to kind of work (though I don't know why I can only use print_r and not echo).

Because it is an array. 


---------------

If you want to echo all data on the page anyway ( which you put in $catalugue)

Why not directly echo it in the foreach loop instead of putting it in to an array?

Basic Example;

foreach ($allarticles as $onearticle) {
		echo '<div>Article Number : '.$onearticle->id.'</div>;
        echo '<div>Article Title : '.$onearticle->title.'</div>;
.
.
.

 

For running Proceswire codes outside of the templates folder you need to include the index.php on top of your php file.

Share this post


Link to post
Share on other sites

@huseyin yes I tried that to test if the array is working and it works fine. But I will need to request specific array positions via AJAX so I don't see how echoing the values would help.

 

Share this post


Link to post
Share on other sites

Hello,

the echo suggestion was only because you tried to echo the $catalogue array.

In your case the problem is php is running on server side and ajax is running on client side. So you need to transfer the serverside pulled data to client side first.
This could be done with a json post call. (there may be many other ways but this one is my favorite which i use often)
You can pass also some variables via ajax to your php so make your filtering right while calling data.

If you have not used ajax json post before google it. There are many results.
Example https://www.webslesson.info/2017/06/jquery-ajax-call-to-php-script-with-json-return.html

Hope this helps...

Share this post


Link to post
Share on other sites

server log says

PHP Fatal error: Uncaught Error: Call to a member function find() on null in path/to/test.php

The AJAX request seems to work (I tried with echo "Hello World!") but the PW-objects, methods and functions and its API seem to not be recognised. I guess the test.php file needs to be in a different folder (I put it in the root folder) or I need to load another script where the objects and functions are defined. 

Share this post


Link to post
Share on other sites
3 hours ago, huseyin said:

For running Proceswire codes outside of the templates folder you need to include the index.php on top of your php file.

Did you include the pw index.php in test.php

(additionaly processwire namespace could be required. I am not sure. Please try) 

  • Thanks 1

Share this post


Link to post
Share on other sites
34 minutes ago, huseyin said:

Did you include the pw index.php in test.php

@huseyin thanks, that did it! Didn't read that earlier today.

Getting there…

But now I get an image problem like:

Forbidden
You don't have permission to access /path/to/site/assets/files/1062/ on this server.

thanks so far!

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...