我必须设置我的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 as $document)
您应该添加:
$arraydoc = array();
然后换行:
$arraydoc= array('name' =>$document->nomdoc,'image'=>$imagedo,'group'=>3);
变成:
$arraydoc[] = array('name' =>$document->nomdoc,'image'=>$imagedo,'group'=>3);
关于mysql - 以json格式从mysql表中获取所有行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30013210/