1

I try to pass my array in my php code javscript . I want to get the result of my foreach . The problem is that I only get the last array. How to get each arrays in the console.log() ? PHP CODE :

function users_nb_match($value){
  $tab_nb_match_par_user = array();
  $mois_choisi = "";
  $match_ou_catego = "matching";//"categorisation";
  if(strlen($mois_choisi)==0){
      $res = sql_dico("SELECT MAX(mois) AS zemois FROM stats_".$match_ou_catego."_univers WHERE LENGTH(mois)=7");//mois de janvier
      if($res && $rez = mysql_fetch_object($res))
          $mois_choisi = $rez->zemois;
  }
  //echo formate_pieChart("Column",$tab_values,$_GET['univers']);
  $zedate = "";
  $res = sql_dico("SELECT mois, login, nb_match FROM   stats_".$match_ou_catego."_univers 
  WHERE login != '' ".((strlen($value)>0)?"
  AND univers = '".$value."'":"")." ".((strlen($mois_choisi)>0)?"
  AND mois IN ('".$mois_choisi."')":"")."
  AND LENGTH(mois)=7 ORDER BY nb_match DESC");

  //tab_value_user c'est le nb matching par utilisateurs
  while($res && $rez = mysql_fetch_object($res)){
      if(isset($tab_nb_match_par_user[$rez->login]))
          $tab_nb_match_par_user[$rez->login] += $rez->nb_match;
      else
          $tab_nb_match_par_user[$rez->login] = $rez->nb_match;
      $zedate = $rez->mois;
  }
  arsort($tab_nb_match_par_user);   
  return $tab_nb_match_par_user;}

  foreach($tab_bases as $key => $values){
      $tab_nb_match_par_user = users_nb_match($values);
  }

Javascript (on the same page)

<script type="text/javascript">
  var tab_nb_match_par_user = <?php   print_r(json_encode($tab_nb_match_par_user)); ?>;
  console.log(tab_nb_match_par_user);</script>

$tab_base contains database_name, so for each database name there are one query, and with this query I build an array

EDIT POST : on this platform when I select " Tous les univers " I get only the last array . I do not dynamically retrieves my others javascript array . I generate my graph like this :

function formate_pieChart(){
 $PieChart = "<div id='chart1' style='width:400px;height:400px'></div>";
 return  $PieChart;}

   function formate_barChart(){
  $BarChart = "<div id='chart2' style= 'width:60%;height:400px'></div>";
  return $BarChart;}

function formate_imageChart(){
$imgMi = "<div id='chart3' style='width:100%;height:200px;margin-right:10%'></div>";
return $imgMi;}


function genere_code($choix){
switch($choix){
    case 0:{
        return formate_barChart();
        break;
    }
    case 1:{
        return formate_pieChart();
        break;
    }
    case 2:{
        return formate_imageChart();
        break;
    }
    default:{
        return formate_barChart();
        break;
    }
  }}
if($univers=="tous") {//la variable $bdd est fixe à cet endroit
  echo genere_code(1);
}
foreach($tab_bases as $key => $values){
    $tab_nb_match_par_user = users_nb_match($values);
    $tab_mois_nb_match = mois_nb_match($values);
    echo genere_code(1);

}

If I click on ec_be or ect the charts loading but if I click on "tous les univers" only the last chart is running... note : the value of the is "tous" for "tous les univers"

4
  • What does the console says? Commented Jun 18, 2015 at 9:55
  • the console gives me one array Commented Jun 18, 2015 at 9:58
  • why dont you just use json_encode? Commented Jun 18, 2015 at 14:53
  • If you have the solution I will past a great night :) Commented Jun 18, 2015 at 15:03

1 Answer 1

2

I want to get the result of my foreach . The problem is that I only get the last array. How to get every arrays in the console.log() ?

Change

foreach($tab_bases as $key => $values)
{
      $tab_nb_match_par_user = users_nb_match($values);
}

to

foreach($tab_bases as $key => $values)
{
      $tab_nb_match_par_user[] = users_nb_match($values);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.