A
A
Aitd2015-12-12 13:36:56
PHP
Aitd, 2015-12-12 13:36:56

How to convert array to horizontal html table?

Greetings.
The task is to organize a horizontal drop-down list.
I found such a thing . Formation occurs with the help of ul - li elements.
If anyone knows what is better found - there will be a big gratitude.
What is the meaning of the question. In php there is a selection from the database, slightly normalized. This is an associative array, the array has an element that may be missing. The idea is that if this element is present, it contains the same array, with the same conditions (there can be several such elements), and all this goes deep. As I understand it, you need to do a recursive traversal, but there is a problem with the cycle, inside the element there may be a link to a parent element, and then it will bypass indefinitely.
Can you tell me the easiest way to convert such a dataset into a list?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shcherbakov, 2015-12-12
@Aitd

function fn_tree(a) {
    // где a - ответ полученный от сервера
    // до вызова функции надо проверить на typeof(a) === 'undefined' || a === null
  
    var arr1 = [];
  
    for (var i1 = 0; i1 < a.length; ++i1) {
  
      var arr2 = [];
      if (typeof(a.[i1].nextChild) === 'undefined' || a.[i1].nextChild === null) {
        for (var i2 = 0; i2 < a.[i1].nextChild.length; ++i2) {
          var html2 = '' + 
            '<div>' + 
              a.[i1].nextChild.[i2].id + 
            '</div>' + 
          '';
          arr2.push(html2);
        };
      }
  
      var html1 = '' + 
        '<div>' + 
          '<div>' + 
            a.[i1].id + 
          '<div>' + 
          '<div>' + 
            arr2.join('') + 
          '<div>' + 
        '</div>' + 
      ''; // рагульно строим DOM и чем-то его наполняем, нет времени :-)
  
      arr1.push(html1);
    };
    var b = arr1.join('');
    return b;
  }

If you streamline the structure of the data returned by the server, then you can simplify the code and make it less scary.
On the server, return the response in JSON (echo json_encode($a);)

A
Aitd, 2015-12-12
@Aitd

A regular php array taken from a muscle can be made in json
prntscr.com/9d8xjb

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question