Help with Multi Array

Asked

Viewed 66 times

1

Hello someone can help me how to create a list with php this type of array. The data comes from two tables, Categories and Products.

I am using Mysql to fetch data from both tables.

My tables:

Categories => id, name

Products => id, id_categoria, name, value

[
    {
        "id": "1",
        "nome": "Pizzas",
        "lista": [
            {
                "NOME": "Pizza Pequena",
                "VALOR": "30"
            },
            {
                "NOME": "Pizza Media",
                "VALOR": "30"
            }
        ]
    },
    {
        "id": "2",
        "nome": "Bebidas",
        "lista": [
            {
                "NOME": "Refri",
                "VALOR": "3"
            },
            {
               "NOME": "Suco",
                "VALOR": "2"
            }
        ]
    }
]

I’ve tried harder I can only list the categories.

  • You’re not getting the values inside "list"?

  • I actually create the list, I need to display it that way on the web. but I can’t, because it is a means q an api, for me to recover via Axios in React Native

  • Ixi, then you will have to put in question the codes you are using, the query, the table structure etc. so you can know how to mount the json.

  • You have to inform tb what type of database you are using (if it is Mysql) and the language (if it is PHP)

  • adjusted my question,.

  • I just think the average pizza should cost more than the small pizza, but... P

  • rsrs, also. Thanks for the help!

Show 2 more comments

2 answers

1

You can use the INNER JOIN to do SELECT. So:

SELECT * FROM categoria INNER JOIN produtos WHERE categoria.id = produtos.id_categoria ORDER BY categoria.nome

This way you will be able to select the products through the category. Then you implement in the best way, example:

 $query = mysqli_query($sql, "SELECT * FROM categoria INNER JOIN produtos WHERE categoria.id = produtos.id_categoria ORDER BY categoria.nome");

 $bebidas = array();
 $pizzas = array();

 $produtos = array();

  while($prods = mysqli_fetch_array($query)){

    if(in_array($prods[1], $produtos)){
        $produtos[$prods[1]][] = array($prods['nome'], $prods['valor']);
    } else {
        $produtos[] = $prods[1];
        $produtos[$prods[1]][] = array($prods['nome'], $prods['valor']);
    }
  }

  print_r($produtos);
  • Thanks for the strength! I’ll take a look at that!

  • tranquil! = ) @Camilaf the nice thing about using Inner Join is that you will only make one connection to the database. This generates more optimization to the server.

  • 1

    humm, I’ve heard of it, but I’ve never used it. I’ll run some tests!

0


Database:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

PHP:

 <?php

    $mysqli = new mysqli("localhost", "root", "", "teste");

    $meuArray = array();
    //Add dados:
    $i = 0;
    if ($sth = $mysqli->query("SELECT * FROM Categorias")){
        while ($row = $sth->fetch_assoc()){
    //Posição 0
            $id = $row['id'];
        array_push($meuArray, array(
            "id"=> $row['id'],
            "nome" => $row['nome']));


        if ($st = $mysqli->query("SELECT * FROM `Produtos` WHERE `id_categoria` = '$id'")){
            while ($row2 = $st->fetch_assoc()){

                array_push($meuArray[$i], array("Nome"=> $row2['nome'],
                                          "Valor" => $row2['valor']));

            }
        }

        $i++;
        }
    }

    //Aqui já pegamos todos os campos;
    print_r($meuArray);

Answer:

inserir a descrição da imagem aqui

It was bad for the delay, I was solving some business here first kkkkk

  • From there you make your logic and implement your need there

  • my array is not mounted. that’s just what I don’t know how to join the two tables into one!

  • @Camilaf you want to fetch the mysqli data and with them mount the array?

  • yes! That, I’ve tried I can only get the categories.

  • @Camilaf just query and use a while to fill the fields of the array with the data coming from the server. Just a moment, dx I make an example here

  • @Camilaf, see if this is what you wanted, and dscp for the delay, I was kind of busy here :3

  • Wow it seems you copied my bunch of data, rsrs :p was that right!! thank you!!! s2

Show 2 more comments

Browser other questions tagged

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