mysql - 从 mysql 表中获取 json 格式的所有行

标签 mysql arrays json laravel d3.js

我必须设置我的 json 文件,以便我可以使用 D3 JavaScript 库图形显示这是我的代码:

      <?php                 
          $mns = DB::table('mns')
           ->join('phases','mns.phases_idphase','=','phases.idphase')
           ->join('users','mns.users_iduser','=','users.iduser')
           ->where('projets_idprojet','=',$id)
            ->order_by('mns.created_at','desc')
            ->take(1)
            ->get();
           foreach ($mns as $mn ) 
             { 
                  $documents = DB::table('documents')
                   ->where('mns_idmn','=',$mn->idmn)
                   ->get();

                   $users = User::where('iduser','=',$mn->users_iduser)
                    ->get();

                   $phases = Phase::where('projets_idprojet','=',$id)
                    ->where('idphase','=',$mn->phases_idphase)
                    ->get();

        $nom_mn=$mn->nommn;
        if ($mn->etat=='initié')  $imagemn='http://localhost/batcoop/public/graph/mn_blue.png';
            elseif ($mn->etat=='validé') $imagemn='http://localhost/batcoop/public/graph/mn_vert.png';
            elseif ($mn->etat=='en attent') $imagemn='http://localhost/batcoop/public/graph/mn_gris.png';
        else $imagemn='http://localhost/batcoop/public/graph/mn_rouge.png';


        if(!empty($documents))
        {
 $table_corespond=0;
                foreach ($documents as $document) 
                {
            if ($document->etat=='initié') $imagedo='http://localhost/batcoop/public/graph/document_blue.png';
                    elseif ($document->etat=='validé') $imagedo='http://localhost/batcoop/public/graph/document_vert.png';
                    elseif ($document->etat=='en attent') $imagedo='http://localhost/batcoop/public/graph/document_gris.png';
                    else $imagedo='http://localhost/batcoop/public/graph/document_rouge.png';
          $arraydoc= array('name' =>$document->nomdoc,'image'=>$imagedo,'group'=>3);

                }

         }
        else
        {
        }
                foreach ($phases as $phase) 
        {
                $nom_phase=$phase->titrephase;
                if ($phase->etatphase=='initié') $imageph='http://localhost/batcoop/public/graph/phase_blue.png';
                    elseif ($phase->etatphase=='validé') $imageph='http://localhost/batcoop/public/graph/phase_vert.png';
                    elseif ($phase->etatphase=='en attent') $imageph='http://localhost/batcoop/public/graph/phase_gris.png';
                else $imageph='http://localhost/batcoop/public/graph/phase_rouge.png';

        }


        foreach ($users as $user)
        {
            $imageus="http://localhost/batcoop/public".$user->photousermin;
            $nom_user=$user->nomuser." ".$user->prenomuser;
        }

         $nodes = array(array('name'                              =>$nom_mn,'image'=>$imagemn,'group'=>0),array('name' =>$nom_phase,'image'=>$imageph,'group'=>1),array('name' =>$nom_user,'image'=>$imageus,'group'=>2),$arraydoc);

    $links = array(array("source"=>0,"target"=>1,"distance"=>6),array("source"=>2,"target"=>0,"distance"=>6),array("source"=>3,"target"=>0,"distance"=>6));
    $graph='{ "nodes":'.json_encode($nodes).',"links":'.json_encode($links).'}';
    $my_file='graph.json';
    $handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file); //implicitly creates file
    fwrite($handle, $graph);

问题是我有多个文档,并且在我的 json 文件中仅显示最后一个文档 请任何人帮忙

最佳答案

行前:

foreach($documents 作为 $document)

您应该添加:

$arraydoc = array();

然后更改行:

$arraydoc= array('name' =>$document->nomdoc,'image'=>$imagedo,'group'=>3);

进入:

$arraydoc[] = array('name' =>$document->nomdoc,'image'=>$imagedo,'group'=>3);

关于mysql - 从 mysql 表中获取 json 格式的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013210/

相关文章:

MySQL 提高查询性能

python - 在Python中对多维数组应用Mann Whitney U测试并替换xarray数据数组变量的单个值?

json - NiFi EvaluateJSONPath 循环遍历数组以获得正确的值

python - 如何在python中读取json嵌入ini配置文件

结果中有多个计数的 MySql 查询

Mysql比较逗号分隔的字段与单个字符串

php - 通知: Undefined index ajax

c++ - 将 unicode(?) 字符直接从源代码写入 WriteConsoleOutput

arrays - 根据多个条件进行 Vlookup 以显示最接近的答案

python - Python中的JSON字符串解析错误