R
R
ronni102018-02-04 23:32:36
PHP
ronni10, 2018-02-04 23:32:36

How to correctly select and display an associative array from related tables?

5a776b9c06e35570911670.png5a776ba856b59438646470.png
Related tables by skills_id, you need to display an associative array in php, I do this:

$db = Db::getconnection();

      $skillslist = [];

      $result = $db->query('SELECT * FROM skills LEFT OUTER JOIN about ON skills.skills_id = about.skills_id');

      $result -> execute();

      while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $skillslist[$row['skill_name']][] =$row;				

        
      }
      //$row = $result->FETCHALL(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
      
      return $skillslist;

output is this:
array(5) {
["frontend"]=>
array(4) {
[0]=>
array(5) {
["skills_id"]=>
string(1) "1"
["skill_name"]= >
string(8) "frontend"
["about_id"]=>
string(1) "1"
["about_name"]=>
string(10) "html & css"
["about_val"]=>
string(3) " 100"
}
[1]=>
array(5) {
["skills_id"]=>
string(1) "1"
["skill_name"]=>
string(8) "frontend"
["about_id"]=>
string(1) "3"
["about_name"]=>
string(11) "js & jquery"
["about_val"]=>
string(2) "20"
}
[2]=>
array(5) {
["skills_id"]=>
string(1) "1"
[" skill_name"]=>
string(8) "frontend"
["about_id"]=>
string(1) "4"
["about_name"]=>
string(4) "sass"
["about_val"]=>
string(2 ) "20"
}
[3]=>
array(5) {
["skills_id"]=>
string(1) "1"
["skill_name"]=>
string(8) "frontend"
["about_id"]=>
string(1) "5"
["about_name"]=>
string(9) "bootstrap"
["about_val"]=>
string(2) "18"
}
}
["backend"]=>
array(4) {
[0]=>
array(5) {
["skills_id"] =>
string(1) "2"
["skill_name"]=>
string(7) "backend"
["about_id"]=>
string(1) "6"
["about_name"]=>
string(3) "php "
["about_val"]=>
string(2) "20"
}
[1]=>
array(5) {
["skills_id"]=>
string(1) "2"
["skill_name"]=>
string(7) "backend"
["about_id"]=>
string(1) "7"
["about_name"]=>
string(5) "mysql"
["about_val"]=>
string(2) "25"
}
[2]=>
array(5) {
["skills_id" ]=>
string(1) "2"
["skill_name"]=>
string(7) "backend"
["about_id"]=>
string(1) "8"
["about_name"]=>
string(8) " composer"
["about_val"]=>
string(2) "20"
}
[3]=>
array(5) {
["skills_id"]=>
string(1) "2"
["skill_name"]=>
string(7) "backend"
["about_id"]=>
string(1) "9"
["about_name"]=>
string(7) "laravel"
["about_val"]=>
string(2) "20"
}
}
["workflow"]=>
array(4) {
[ 0]=>
array(5) {
["skills_id"]=>
string(1) "3"
["skill_name"]=>
string(8) "workflow"
["about_id"]=>
string(2) "10 "
["about_name"]=>
string(3) "git"
["about_val"]=>
string(2) "20"
}
[1]=>
array(5) {
["skills_id"]=>
string(1) "3"
["skill_name"]=>
string(8) "workflow"
["about_id"]=>
string(2) "11"
["about_name"]=>
string(4) "gulp"
["about_val"]=>
string(2) "20"
}
[2]=>
array(5) {
["skills_id"]=>
string(1) "3"
["skill_name"]=>
string(8) "workflow"
["about_id"]=>
string(2) " 12"
["about_name"]=>
string(5) "bower"
["about_val"]=>
string(2) "20"
}
[3]=>
array(5) {
["skills_id"]=>
string(1) "3"
["skill_name"]=>
string(8) "workflow"
["about_id"]=>
string(2) "13"
["about_name"]=>
string(3) "ssh"
["about_val"]=>
string(2) "20"
}
}
}
It just seems to me that something is not right, because some difficulties arise

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-02-04
@SmoKE_xD

Everything is much easier - PDO fetchAll

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question