Sort classification according to the database | PHP

1

I have the following structure:

Ihave3typesofclassification,followingthecoherence:1.0.0,1.1.0,1.1.1,1.1.2-Ihavetodisplayinfrontofthetitles,eachcontentthatbelongstoitsclassification,beingasfollowsatthetimeofdisplayingonscreen:

>1.MeuconteudoAdministrativo>1.1NovoConteudo>1.1.1AndréRibeiro>1.1.2TestedeGestãodePessoas

Youneedtofolloweachfield.

I'musingcodeigniter,butitcanbedoneinbasicPHPsameSQL,andIsetitincodeigniter.

Itriedtodoitasfollows:

$this->db->select("coe_classificacao");
$this->db->where("set_base != ", 12); // conteudo sobre
$this->db->order_by("coe_classificacao", "ASC");
$this->db->group_by("coe_classificacao");
$consulta = $this->db->get('conteudos')->result();

foreach($consulta as &$valor){
    $this->db->select("coe_titulo, coe_classificacao, coe_classificacao_1, coe_classificacao_2");
    $this->db->where("set_base != ", 12); // conteudo sobre
    $this->db->where("coe_classificacao", $valor->coe_classificacao);
    $this->db->order_by("coe_classificacao_1", "ASC");
    $retorno = $this->db->get('conteudos')->result();

        foreach($retorno as &$valor_retorno){
            $this->db->select("coe_titulo, coe_classificacao_1, coe_classificacao_2");
            $this->db->where("set_base != ", 12); // conteudo sobre
            $this->db->where("coe_classificacao_1", $valor_retorno->coe_classificacao_1);
            $this->db->order_by("coe_classificacao_2", "ASC");
            $valor_retorno->sub = $this->db->get('conteudos')->result();                    
        }
}

But similarly, it does not bring me the expected result, I believe you can disregard what I did.

The question is: How do I turn everything into an array so that I can display the way I transcribed it?

    
asked by anonymous 10.10.2016 / 21:21

2 answers

2

Your logic seems to be correct, it seems to me that you just have to "save" the reference data to each item:

$this->db->select("coe_classificacao");
$this->db->where("set_base != ", 12); // conteudo sobre
$this->db->order_by("coe_classificacao", "ASC");
$this->db->group_by("coe_classificacao");
$consulta =  json_decode(json_encode($this->db->get('conteudos')->result_array()), True);

foreach($consulta as $key => $valor){
    $this->db->select("coe_titulo, coe_classificacao, coe_classificacao_1, coe_classificacao_2");
    $this->db->where("set_base != ", 12); // conteudo sobre
    $this->db->where("coe_classificacao", $valor->coe_classificacao);
    $this->db->order_by("coe_classificacao_1", "ASC");
    $retorno = $this->db->get('conteudos')->result_array();
    $consulta[$key][] =  json_decode(json_encode($retorno), True);

        foreach($consulta[$key] as $key_neto => $valor_retorno){
            $this->db->select("coe_titulo, coe_classificacao_1, coe_classificacao_2");
            $this->db->where("set_base != ", 12); // conteudo sobre
            $this->db->where("coe_classificacao_1", $valor_retorno->coe_classificacao_1);
            $this->db->order_by("coe_classificacao_2", "ASC");
            $consulta[$key][$key_neto][] = 
    $consulta[$key][] =  json_decode(json_encode($this->db->get('conteudos')->result_array()), True);                    
        }
}

I believe this will solve!

    
10.10.2016 / 21:25
1

PHP code, I do not understand what you mean by PHP pure, I use mysqli :

$sql = "SELECT coe_class, coe_class1, coe_class2, coe_titulo FROM TESTE ORDER BY coe_class,coe_class1,coe_class2;"
  or die("Erro na consulta: " . mysqli_error($sql));

$query = $conn->query($sql);

if ( $query->num_rows > 0 ){
  while ($dados = $query->fetch_assoc()){
      $out = '';
      $out = ($dados['coe_class']  > 0 ? $dados['coe_class']  .'.' : '') .
             ($dados['coe_class1'] > 0 ? $dados['coe_class1'] .'.' : '') .
             ($dados['coe_class2'] > 0 ? $dados['coe_class2'] .'.' : '');

      echo rtrim(trim($out), '.') . ' - ' . $dados['coe_titulo'];
      echo '<br>';
  }
} else {
    echo 'Nenhuma linha retornada.';
}

Example output:

  

1 - Administrative
  1.1 - New Content
  1.1.1 - Andre
  1.1.2 - Management Testing

    
10.10.2016 / 21:45