Creating multidimensional array from query to BD Mysql

Asked

Viewed 862 times

1

I have the following problem:

I have a table in a Mysql database and in a code snippet I need to make the content recovered by a query made to this database be stored in a multidimensional array/array in php. The values of the two consulted columns are related and I can’t imagine any other way to keep the relationship between the items in a practical way without them being stored in a structure variable similar to the database table (in this case, the multidimensional array).

TL;DR; I want to store the contents of two columns of an SQL table in a multidimensional array so that I can access the array with

'array["valor1_da_tabela"]= "valor1,valor2,valor3,..."
 array["valor2_da_tabela"]= "valor1,valor2,valor3,..."
 array["valor3_da_tabela"]= "valor1,valor2,valor3,..." 
'

Detail: solution suggestions should be made using PDO. All this is so you can do a cascading javascript combobox/dropdown scheme on a portal page. And the items in these combobox/dropdown are all values stored in this SQL table. The first one works smoothly. The second that depends on what was selected in the first is not leaving because of the order of execution of the client and server side codes. I need to store these two entire columns with string values to facilitate the task of implementing dynamic combobox/dropdown cascades. If you have any suggestions on that, I’d also appreciate it.

Thank you for your attention.

EDIT: This is an excerpt from a part of the project that does something similar only for one column. What I tried to do was adapt this snippet to suit my need to create a multidimensional array instead of a common array.

<?php 
    ini_set('default_charset','UTF-8');
    require("../dbconnect.inc.php");
    $vet = array();
    $assunto = array();
    $jsArray = "";
    $sql = "SELECT distinct assunto
                FROM arquivos";

    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $vet = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach($vet as $vet_assunto){
    array_push($assunto, $vet_assunto['assunto']);
}       
    for($i=0;$i<sizeof($assunto);$i++){
    $jsArray = $jsArray . $assunto[$i] . ",";
    }
    $jsArray = chop($jsArray, ","); 
    require("../dbdisconnect.inc.php");     
?>

<script type="text/javascript" language="javascript">

    var jsStrArray = "<?php echo $jsArray; ?>";
    var listaAs = jsStrArray.split(",");

        $(document).ready(function() {     

            $.each(listaAs, function(i, val){
            var content = val;          
            $('#painel').append('<a href="aulaView.php?assunto='
                                + content   
                                + '"><div class="tile"><p class="asTitle"><b>' 
                                + content 
                                +'</b></p></div></a>');
            });

           $(".tile").hover( function(){
                $(this).css({'border':'3px solid #007CFF','box-shadow':'2px 2px 7px #00B2FF'});
           }, 
            function(){
                $(this).css({'border':'3px solid #151515','box-shadow':'2px 2px 7px #000000'});
            });
        });
</script>

This stretch is one of the attempts:

        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $vet = $stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach($vet as $vet_assunto){
        array_push($assunto['assunto'], $vet_assunto['assunto']);   
        array_push($assunto['assunto']['aula'], $vet_assunto['aula']);  
    }

        require("../dbdisconnect.inc.php");     
    ?>

Example of the two columns in question in the database table:

Assunto|aula

teste1 | 1
teste1 | 2
teste2 | 1
teste2 | 2
teste2 | 3

And I would like the array to be organized as follows:

                  Output
   array[teste1]= {1,2}
   array[teste2]= {1,2,3}

array[teste1][1]= {2}
array[teste2][2]= {3}

  • 1

    What have you ever tried to do?

  • Basically, I tried to pass the content using array_push() and foreach. I’m probably using the syntax wrong. I don’t have much experience with php yet. If you want I put one of my code attempts here, but it’s all wrong. No.

  • 1

    It is always important that you post what you have already done, because it shows that you have somehow tried but failed to solve, and do not want the entire code ready. Post there, even if you are wrong, the crowd will show where you should change and what to do to make it work. :)

  • 1

    Your question does not make very clear the format of the data you need. There are many ways to solve this problem, but it depends on the content of the table. Apart from the code, it would be cool for a real example of the type of data you will store. One possibility is to save the data of each line in a text field like this: dado1|dado2|dado3 and retrieve each line with explode. If you can by real examples of what will be stored, increases the chance of having good answers.

  • It became a little clearer?

  • Well... I’ll see if I can solve using explodes later... for now I’m bypassing using text fields, but it’s not ideal. Thanks for the tip, Bacco.

Show 1 more comment
No answers

Browser other questions tagged

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