Se connecter avec
S'enregistrer | Connectez-vous

Probleme de array en PHP

Dernière réponse : dans Programmation
Expert Programmation

D'abord voici mon code assez long ! :

  1. //TOTAL
  2. $sql = 'SELECT count(*) FROM stat1';
  3. $req = mysql_query($sql) or die('ERREUR SQL !'.mysql_error());
  4. $data = mysql_fetch_array($req);
  5. $total = $data[0];
  6.  
  7. //FIREFOX
  8. $sql0 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%firefox%"';
  9. $req0 = mysql_query($sql0) or die('ERREUR SQL'.mysql_error());
  10. $data0 = mysql_fetch_array($req0);
  11. $firefox = $data0[0];
  12. $pourcent_firefox = $firefox / $total * 100;
  13. $barre_firefox = round($pourcent_firefox / 100 * 300);
  14. $sond_titre_firefox = '<tr><td><b>Firefox : </b></td>';
  15. $sond_barre_firefox = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_firefox.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  16. $sond_pourc_firefox = '<td align="center"><b> '.substr($pourcent_firefox,0,5).'% </b></td></tr>';
  17. $array_firefox = array("0" => $pourcent_firefox, "1" => $sond_titre_firefox, "2" => $sond_titre_firefox, "3" => $sond_titre_firefox);
  18.  
  19. //OPERA
  20. $sql1 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%opera%"';
  21. $req1 = mysql_query($sql1) or die('ERREUR SQL'.mysql_error());
  22. $data1 = mysql_fetch_array($req1);
  23. $opera = $data1[0];
  24. $pourcent_opera = $opera / $total * 100;
  25. $barre_opera = round($pourcent_opera / 100 * 300);
  26. $sond_titre_opera = '<tr><td><b>Opera : </b></td>';
  27. $sond_barre_opera = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_opera.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  28. $sond_pourc_opera = '<td align="center"><b> '.substr($pourcent_opera,0,5).'% </b></td></tr>';
  29. $array_opera = array("0" => $pourcent_opera, "1" => $sond_titre_opera, "2" => $sond_titre_opera, "3" => $sond_titre_opera);
  30.  
  31. //INTERNET EXPORER DE MERDE !
  32. $sql2 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%msie%"';
  33. $req2 = mysql_query($sql2) or die('ERREUR SQL !'.mysql_error());
  34. $data2 = mysql_fetch_array($req2);
  35. $msie = $data2[0];
  36. $pourcent_msie = $msie / $total * 100;
  37. $barre_msie = round($pourcent_msie / 100 * 300);
  38. $sond_titre_msie = '<tr><td><b>Internet Explorer : </b></td>';
  39. $sond_barre_msie = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_msie.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  40. $sond_pourc_msie = '<td align="center"><b> '.substr($pourcent_msie,0,5).'% </b></td></tr>';
  41. $array_msie = array("0" => $pourcent_msie, "1" => $sond_titre_msie, "2" => $sond_titre_msie, "3" => $sond_titre_msie);
  42.  
  43. //NETSCAPE
  44. $sql3 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%netscape%"';
  45. $req3 = mysql_query($sql3) or die('ERREUR SQL !'.mysql_error());
  46. $data3 = mysql_fetch_array($req3);
  47. $netscape = $data3[0];
  48. $pourcent_netscape = $netscape / $total * 100;
  49. $barre_netscape = round($pourcent_netscape / 100 * 300);
  50. $sond_titre_netscape = '<tr><td><b>Netscape : </b></td>';
  51. $sond_barre_netscape = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_netscape.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  52. $sond_pourc_netscape = '<td align="center"><b> '.substr($pourcent_netscape,0,5).'% </b></td></tr>';
  53. $array_netscape = array("0" => $pourcent_netscape, "1" => $sond_titre_netscape, "2" => $sond_titre_netscape, "3" => $sond_titre_netscape);
  54.  
  55. //SAFARI
  56. $sql4 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%safari%"';
  57. $req4 = mysql_query($sql4) or die('ERREUR SQL !'.mysql_error());
  58. $data4 = mysql_fetch_array($req4);
  59. $safari = $data4[0];
  60. $pourcent_safari = $safari / $total * 100;
  61. $barre_safari = round($pourcent_opera / 100 * 300);
  62. $sond_titre_safari = '<tr><td><b>Safari : </b></td>';
  63. $sond_barre_safari = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_safari.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  64. $sond_pourc_safari = '<td align="center"><b> '.substr($pourcent_safari,0,5).'% </b></td></tr>';
  65. $array_safari = array("0" => $pourcent_safari, "1" => $sond_titre_safari, "2" => $sond_titre_safari, "3" => $sond_titre_safari);
  66.  
  67. //KONQUEROR
  68. $sql5 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%konqueror%"';
  69. $req5 = mysql_query($sql5) or die('ERREUR SQL !'.mysql_error());
  70. $data5 = mysql_fetch_array($req5);
  71. $konqueror = $data5[0];
  72. $pourcent_konqueror = $konqueror / $total * 100;
  73. $barre_konqueror = round($pourcent_konqueror / 100 * 300);
  74. $sond_titre_konqueror = '<tr><td><b>Konqueror : </b></td>';
  75. $sond_barre_konqueror = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_konqueror.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  76. $sond_pourc_konqueror = '<td align="center"><b> '.substr($pourcent_konqueror,0,5).'% </b></td></tr>';
  77. $array_konqueror = array("0" => $pourcent_konqueror, "1" => $sond_titre_konqueror, "2" => $sond_titre_konqueror, "3" => $sond_titre_konqueror);
  78.  
  79. //FIREBIRD
  80. $sql6 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%firebird%"';
  81. $req6 = mysql_query($sql6) or die('ERREUR SQL !'.mysql_error());
  82. $data6 = mysql_fetch_array($req6);
  83. $firebird = $data6[0];
  84. $pourcent_firebird = $firebird / $total * 100;
  85. $barre_firebird = round($pourcent_firebird / 100 * 300);
  86. $sond_titre_firebird = '<tr><td><b>Firebird : </b></td>';
  87. $sond_barre_firebird = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_firebird.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  88. $sond_pourc_firebird = '<td align="center"><b> '.substr($pourcent_firebird,0,5).'% </b></td></tr>';
  89. $array_firebird = array("0" => $pourcent_firebird, "1" => $sond_titre_firebird, "2" => $sond_titre_firebird, "3" => $sond_titre_firebird);
  90.  
  91. //SEAMONKEY
  92. $sql7 = 'SELECT count(*) FROM stat1 WHERE http_user_agent LIKE "%seamonkey%"';
  93. $req7 = mysql_query($sql7) or die('ERREUR SQL !'.mysql_error());
  94. $data7 = mysql_fetch_array($req7);
  95. $seamonkey = $data7[0];
  96. $pourcent_seamonkey = $seamonkey / $total * 100;
  97. $barre_seamonkey = round($pourcent_seamonkey / 100 * 300);
  98. $sond_titre_seamonkey = '<tr><td><b>SeaMonkey : </b></td>';
  99. $sond_barre_seamonkey = '<td><table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="images/debsond.gif" width="4" alt="" height="12" /></td><td><img src="images/sond.gif" width="'.$barre_seamonkey.'" height="12" alt="100%" /></td><td><img src="images/finsond.gif" width="4" alt="" height="12" /></td></tr></table></td>';
  100. $sond_pourc_seamonkey = '<td align="center"><b> '.substr($pourcent_seamonkey,0,5).'% </b></td></tr>';
  101. $array_seamonkey = array("0" => $pourcent_seamonkey, "1" => $sond_titre_seamonkey, "2" => $sond_titre_seamonkey, "3" => $sond_titre_seamonkey);



et voici pour l'instant mon affichage :

  1. echo $sond_titre_firefox.$sond_barre_firefox.$sond_pourc_firefox;
  2. echo $sond_titre_opera.$sond_barre_opera.$sond_pourc_opera;
  3. echo $sond_titre_msie.$sond_barre_msie.$sond_pourc_msie;
  4. echo $sond_titre_netscape.$sond_barre_netscape.$sond_pourc_netscape;
  5. echo $sond_titre_safari.$sond_barre_safari.$sond_pourc_safari;
  6. echo $sond_titre_konqueror.$sond_barre_konqueror.$sond_pourc_konqueror;
  7. echo $sond_titre_firebird.$sond_barre_firebird.$sond_pourc_firebird;
  8. echo $sond_titre_seamonkey.$sond_barre_seamonkey.$sond_pourc_seamonkey;



Mais voila il y a un probleme ! cela affiche bien les statistique mais bon c'est mieu d'avoir le plus grand pourcentage en haut et puis par ordre decroissant hors ce n'est pas le cas !

Apparament minarray que je fait sous chaque navigateur ne marche pas (enfin je crois) et de plus ensuite si même cela marchait cela ne serait pas classé !

Résultat je suis un peu pas mal bloqué !

Sinon au pire je ferai une table qui met dans l'ordre les navigateur de temps en temps par exemple juste quant moi je me connecte pour ne pas etre gourmand pour tout le monde mais je suis sur que ici il y a une solution. Seulement il faut bien savoir manipuler les array !



Merci

Autres pages sur : probleme array php

Lassé par la pub ? Créez un compte
Expert Programmation

oui je sais faire des fonctions (simple) (et c'est vrai que j'aurai du en faire !). Seulement avant de faire ce script j'ai recolter deja pas mal de user_agent et c'est aussi pour etre moins gourmand en serveur sinon dans chanque page ce serait gourmand alors que sinon c juste cet page qui est lourde en requete, sinon j'ai fait juste 2 ou 3 pour les stats dans les autres page.


Bon sinon j'ai trouvé 2 fonctions (;-) ) usort et natsort qui devrai normalement purvoir resoudre mon probleme j'ai donc dressé le bon tableau :

  1. $array = array();
  2. $array['firefox'][0] = $pourcent_firefox;
  3. $array['firefox'][1] = $sond_titre_firefox;
  4. $array['firefox'][2] = $sond_barre_firefox;
  5. $array['firefox'][3] = $sond_pourc_firefox;
  6.  
  7. $array['opera'][0] = $pourcent_opera;
  8. $array['opera'][1] = $sond_titre_opera;
  9. $array['opera'][2] = $sond_barre_opera;
  10. $array['opera'][3] = $sond_pourc_opera;
  11.  
  12. $array['msie'][0] = $pourcent_msie;
  13. $array['msie'][1] = $sond_titre_msie;
  14. $array['msie'][2] = $sond_barre_msie;
  15. $array['msie'][3] = $sond_pourc_msie;
  16.  
  17. $array['safari'][0] = $pourcent_safari;
  18. $array['safari'][1] = $sond_titre_safari;
  19. $array['safari'][2] = $sond_barre_safari;
  20. $array['safari'][3] = $sond_pourc_safari;
  21.  
  22. $array['konqueror'][0] = $pourcent_konqueror;
  23. $array['konqueror'][1] = $sond_titre_konqueror;
  24. $array['konqueror'][2] = $sond_barre_konqueror;
  25. $array['konqueror'][3] = $sond_pourc_konqueror;
  26.  
  27. $array['firebird'][0] = $pourcent_firebird;
  28. $array['firebird'][1] = $sond_titre_firebird;
  29. $array['firebird'][2] = $sond_barre_firebird;
  30. $array['firebird'][3] = $sond_pourc_firebird;
  31.  
  32. $array['seamonkey'][0] = $pourcent_seamonkey;
  33. $array['seamonkey'][1] = $sond_titre_seamonkey;
  34. $array['seamonkey'][2] = $sond_barre_seamonkey;
  35. $array['seamonkey'][3] = $sond_pourc_seamonkey;


Sachant que dans $array['navigateur'][0] il y a le pourcentage.


Normalement avec cela et la fonction usort je devrai pouvoir m'en sortir surement même mais j'arrive pas trop a faire la bonne fonction
Expert Programmation

et voila j'ai presque la reponde en 2 ligne !

array_multisort($array, SORT_DESC);
print_r($array);


faut juste que je suprime l'affichage de ceci :

Array ( [0] => Array ( [0] => 55.38 [1] => [2] => [3] => ) [1] => Array ( [0] => 24.61 [1] => [2] => [3] => ) [2] => Array ( [0] => 1.538 [1] => [2] => [3] => ) [3] => Array ( [0] => 0 [1] => [2] => [3] => ) [4] => Array ( [0] => 0 [1] => [2] => [3] => ) [5] => Array ( [0] => 0 [1] => [2] => [3] => ) [6] => Array ( [0] => 0 [1] => [2] => [3] => ) )

Mais existe t-il une fonction ou je suis obligé de faire une boucle ?
Lassé par la pub ? Créez un compte
Tom's guide dans le monde