Mounting an html frame from a php array

Asked

Viewed 121 times

2

The tables:

  • Ciencias (ex: Mathematics, Biology, Chemistry).
  • Areas (e.g.: Algebra I, Biochemistry, Botany, Chemical Bonding).
  • Exercises (e.g.: Water, Algae, Bryophytes, Progressions, Chemical Bonds).

To use as in this example [ Quadro 01 ] in html:

[ Table 01 ]

Mathematics
- Algebra I(1)
Biology
- Biochemistry(1)
- Botany(2)
Chemistry
- Chemical Bonds(1)

Note: In the table [exercises] there are exercises of all areas, that to be identified in relation to science tables and areas, I created columns such as: exercicioArea, exercicioCiencia.

The 'logic' I thought was to use the Inner Join I found in: http://www.w3schools.com/sql/sql_join_inner.asp to put the tables together like this:

SELECT *
ciencias.ciencia as cienciaNome,
areas.area as materiaNome,
exercicios.exercicio the exercicioNome
FROM science
INNER JOIN areas
ON areas.areaCiencia=ciencia.id
INNER JOIN exercises
ON exercises.exercicioArea = areas.id
ORDER BY scienceName ASC

To know the amount of exercises I found this way in: http://www.w3schools.com/php/func_array_count.asp
echo Count();
That in this case would count the number of elements in the array.

            Array
            (
                [0] => Array
                    (
                        [cienciaNome] => Biologia
                        [materiaNome] => Bioquímica
                        [exercicioNome] => Água
                    )

                [1] => Array
                    (
                        [cienciaNome] => Biologia
                        [materiaNome] => Botânica
                        [exercicioNome] => Algas
                    )

                [2] => Array
                    (
                        [cienciaNome] => Biologia
                        [materiaNome] => Botânica
                        [exercicioNome] => Briófitas
                    )

                [3] => Array
                    (
                        [cienciaNome] => Matemática
                        [materiaNome] => Algebra I
                        [exercicioNome] => Conceito Progressões
                    )

                [4] => Array
                    (
                        [cienciaNome] => Química
                        [materiaNome] => Ligações Químicas
                        [exercicioNome] => Conceito Ligações Químicas
                    )

            )
            Contagem array = 5

However I am not able to understand how to mount [ Quadro 01 ] from the array, as for example there is 3 Biology in the array and in Quadro 01 only needed to put 1 time.

[ Table 01 ]

Mathematics
- Algebra I(1)
Biology
- Biochemistry(1)
- Botany(2)
Chemistry
- Chemical Bonds(1)

  • You only need to mount frame 01 from the mentioned array ?

1 answer

1


If the problem is to assemble Table 01, follow the solution:

    <?php 
         //Criando o array mencionado
         $array_original = array(
                0 => array(
                        'cienciaNome' => 'Biologia',
                        'materiaNome' => 'Bioquímica',
                        'exercicioNome' => 'Água'
                    ),
                1 => array(
                        'cienciaNome' => 'Biologia',
                        'materiaNome' => 'Botânica',
                        'exercicioNome' => 'Algas'
                    ),
                2 => array(
                        'cienciaNome' => 'Biologia',
                        'materiaNome' => 'Botânica',
                        'exercicioNome' => 'Briófitas'
                    ),
                3 => array(
                        'cienciaNome' => 'Matemática',
                        'materiaNome' => 'Algebra I',
                        'exercicioNome' => 'Conceito Progressões'
                    ),
               4 => array(
                        'cienciaNome' => 'Química',
                        'materiaNome' => 'Ligações Químicas',
                        'exercicioNome' => 'Conceito Ligações Químicas'
                    )
    );

    //preparando um array inicial para exibição
    $array_tratado = array();
    foreach ($array_original as $key => $value) {
        foreach ($value as $k => $v) {
            if ($k == 'cienciaNome') {
                $cienciaNome = $v;
            }else if($k == 'materiaNome'){
                $materiaNome = $v;
            }elseif ($k == 'exercicioNome') {
                $array_tratado[$cienciaNome][] = $materiaNome;
                $materiaNome = '';
                $cienciaNome = '';
            }
        }
    }

    //preparando um array final para exibição
    $array_final = array();
    foreach ($array_tratado as $key => $value) {
        foreach ($value as $k => $v) {
          //Nessa linha eu populo o array multidimensional com as chaves correspondentes;
          //O valor que nesse caso é o nome da matéria; 
          //É a mesma lógica do array de cima.
          $array_final[$key][$v][] = $v;
        }
    }

    // exibindo o array
    echo '[ Quadro 01 ]';
    foreach ($array_final as $key => $value) {
        echo "<ul><strong>$key</strong> (" . count($value) . ')';
        foreach ($value as $k => $v) {
            echo "<li>$k (" . count($v) . ')</li> ';
        }
        echo '</ul>';
    }
?>
  • Hello @user31050 I’m glad I helped. The key of an array never repeats on the same level. That’s what happened. If you want to group also the materials just transform them into an array as well.

  • Pole to help us.

  • Ready @user31050 Now it displays the quantity in front.

  • Actually I edited my code. Can you test.

  • Okay, thanks @Ricardo Mota helped a lot if you can comment on this line of code I’m also grateful $array_final[$key][$v][] = $v;

  • I commented @user31050 ;)

Show 1 more comment

Browser other questions tagged

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