PHP makes a tree-structured array of 4 arrays

I have page categories (presented as checkboxes). On the next page, I have subcategories of categories from the first page. The following subcategories are for items on the second page and so on. I store all the selected items in arrays representing the selections made at different levels. The problem is, I don't know how to create a tree structure from all of these arrays. Let me tell you what I have at the moment:

I have 4 arrays, think of them as 4 levels (each level represents the checkboxes selected in the # page). I store the checkboxes selected by the user in them. Below is some sample data: (value is the id of the item in the database)

The l1 value of $_SESSION['0'] is '1' 

The l2 value of $_SESSION['0'] is '2' 
The l2 value of $_SESSION['1'] is '3' 

The l3 value of $_SESSION['0'] is '3' 
The l3 value of $_SESSION['1'] is '4' 
The l3 value of $_SESSION['2'] is '5' 
The l3 value of $_SESSION['3'] is '6' 

The l4 value of $_SESSION['0'] is '1' 
The l4 value of $_SESSION['1'] is '2' 
The l4 value of $_SESSION['2'] is '3' 
The l4 value of $_SESSION['3'] is '4' 
The l4 value of $_SESSION['4'] is '5' 

      

For each ID in the database, I have the ID of the item it belongs to (for example, for level 2 :)

id=1; name = programming, category_id = 1;

      

I can find the relationship by querying the database.

in the above case, both elements from l2 are subcategories of l1 [0]. The first 2 items of level 3 are subcategories of l2 [0], and the last 2 items are subcategories of l2 [1], and at level 4, some of them are subcategories of smth at level 3 (not every item can have subcategories).

How can I plot tree JSON for all this data? Any ideas or suggestions? Thank!

+3


source to share


1 answer


First of all, I apologize if I don't understand your problem well.

Let me explain your problem the way I understand it:

First page

You are showing a form that contains a checkbox called "category" and the values ​​are identifiers of a certain main category

On landing page two

You populate an array (I would call it $ _SESSION ['categories_checked']) with check categories

On the second page

You show Level 1 subcategories the same way you show categories on the home page. Let say the name of the form element is "subcat1"

When landing the third page

You populate an array (I would call it $ _SESSION ['subcats1_checked']) with check subcategories

On the third page

You show Level 2 subcategories the same way you show categories on the home page. Let's say the element element name is "subcat2"

On fourth landing

You fill an array (I would call it $ _SESSION ['subcats2_checked']) with check subcategories

On the fourth page

You show Level 3 subcategories the same way you show categories on the home page. Let's say the element element name is "subcat3"

On final landing page

You fill an array (I would call it $ _SESSION ['subcats3_checked']) with check subcategories



My answer

From my point of view, I would prefer to do the following:

  • always uses the same variable name (in HTML forms and in PHP), so the code can be greatly improved.
  • optionnaly uses a hidden form element that will contain the level
  • nest your arrays

    $_SESSION['categories'] = [
          3 => [
                56 => [
                     89 => [
                           121 => [1,2,3,4,5,],
                           ],
                      ],
               ],
    ];
    
          

This view seems closer to reality and should be easier to display.

The next step is to create a recursive function in which you pass the resulting array of the one-step form and the $ _SESSION ['categories'] variable.

Let me know if the solution works for you ... I could give you a hand if needed.

So recursion is the key to success, but ...

Something is missing ... In order to process your results and specify the PHP array I mentioned, you will have to create your forms like this:

First page

<input type="checkbox" name="category[0]" value="1" /> Cat1 name
<input type="checkbox" name="category[0]" value="2" /> Cat2 name

      

Second page

Subcats of cat 1
<input type="checkbox" name="category[1]" value="110" /> SubCat1 name
<input type="checkbox" name="category[1]" value="222" /> SubCat2 name

Subcats of cat 2
<input type="checkbox" name="category[2]" value="110" /> SubCat1 name
<input type="checkbox" name="category[2]" value="222" /> SubCat2 name

      

This way you will be able to avoid the level input element because the subcategory is now only defined by its parent. Of course, this means that each category and subcategory has a different ID (the trick is possible if it doesn't).

Here's a quick and dirty code to help you

<?php
/**
 * Created by PhpStorm.
 * User: dvienne
 * Date: 31/05/2017
 * Time: 15:16
 */
function treatCategories($parentCatId, $selectedId, $categories) {
  foreach($categories as $categoryID => $category) {
    if(empty($category) AND ($categoryID==$parentCatId)) {
      $categories[$categoryID][$selectedId] = [];
      break;
    } elseif(is_array($category) AND empty($category)) {
      /** RECURSION */
      treatCategories($parentCatId, $selectedId, $category);
    }
  }

  $_SESSION['categories'] = $categories;
}


$userSelection  = $_POST['categories'];
$level          = $_POST['level'];
if(!empty($_SESSION['categories'])) {
  $categories = $_SESSION['categories'];
} else {
  $categories = [];
}

if(!empty($categories)) {
  foreach($userSelection as $parentCatId => $selectedId) {
    treatCategories($parentCatId, $selectedId, $categories);
  }
} else {
  foreach($userSelection as $catId) {
    $categories[$catId] = [];
  }

  $_SESSION['categories'] = $categories;
}

      

PLEASE KNOW

This is clearly not an optimized way to handle the data, because the script will have to scan the entire array of categories before inserting its value. Maybe you should consider a one page solution using AJAX requests.

+1


source







All Articles