Rearrange position of data listed with php

Asked

Viewed 52 times

0

I have 3 tables, one with the name of ordinance, another with the name of materia, and another one with the name of portaria_has_materia. The focus is on port has_materia. It takes the foreign key fields of the ordinance and matter tables, along with an order name field, which is the order in which the matter will appear. Thus remaining:

tabela portaria_has_materia
id | idportaria | idmateria | ordem |
1  |      6     |     3     |   1   |
2  |      6     |     6     |   2   |
3  |      6     |     5     |   3   |
4  |      6     |     2     |   4   |

The order is always registered in sequence in the table, but the subjects have a correct order to appear in the view, and if they are not in the correct order, I have to put them, changing the order field, because the query will be ordered by it. How can I change it and keep the sequence in the database, so that after the change it appears in the correct order?

the return of the data is done in json.

inserir a descrição da imagem aqui

public function getMateriaByPortaria()
{

  $this->layout=""; //retornar dados numa tela sem layout

  $materias = $this->PortariaMateriaM->get_all_portaria_materia(); //pega todos os dados da tabela portaria_has_materia

  $data = array(); //array data

  foreach ($materias as $mat) { //foreach que percorre o objeto e armazena no array data
        $data[] = array(
            "idportaria_materia" => $mat->idportaria_materia,
            "ordem" => $mat->ordem,
            "numerada" => $mat->numerada,
            "idportaria" => $mat->idportaria,
            "data_inicio" => $mat->data_inicio,
            "data_fim" => $mat->data_fim,
            "titulo" => $mat->titulo,
            "descricao" => $mat->descricao,
            "descricao_internacional" => $mat->descricao_internacional,
            "assinatura_instrutor" => $mat->assinatura_instrutor,
            "sigla" => $mat->sigla,
            "idmateria" => $mat->idmateria,
            "nome_materia" => $mat->nome_materia,
            "nome_ingles" => $mat->nome_ingles,
            "numero_tempo" => $mat->numero_tempo,
            "carga_horaria" => $mat->carga_horaria,
            "modulo" => $mat->modulo
        );
    }

    print_r(json_encode($data));
    return json_encode($data); //retorna dados do array em json

}
  • I don’t quite understand the question.. if the primary key is id.. in the table will always sort by id.. In the view is to put Where order for it to bring the way you need it.. I didn’t understand the question :S Sorry I can’t help

  • In this case I am not ordering by the primary key, because the position, which in this case is the order field will be changed. Once changed, it will return me the values in the right position, I just want to know how I can change this data dynamically.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.