Tom's Guide > Forum > Programmation > Probleme de array en PHP

Probleme de array en PHP

Forum Programmation : Probleme de array en PHP

TomsGuide.com : 800 000 inscrits répondent à toutes vos questions high-tech et informatique. Pour obtenir de l'aide, inscrivez-vous gratuitement !
Mot :    Pseudo :           
 

D'abord voici mon code assez long ! :

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

Code :
  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

Liens sponsorisés
Inscrivez-vous ou connectez-vous pour masquer ceci.

est-ce que tu sais creer une fonction en PHP ? on dirait pas...

Pour tes problemes, le mieux serait de stocker les pourcentages pour tous les navigateurs dans un tableau, et de le trier avant l'affichage.

Répondre à bluedylc

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 :

Code :
  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. $array['opera'][0] = $pourcent_opera;
  7. $array['opera'][1] = $sond_titre_opera;
  8. $array['opera'][2] = $sond_barre_opera;
  9. $array['opera'][3] = $sond_pourc_opera;
  10. $array['msie'][0] = $pourcent_msie;
  11. $array['msie'][1] = $sond_titre_msie;
  12. $array['msie'][2] = $sond_barre_msie;
  13. $array['msie'][3] = $sond_pourc_msie;
  14. $array['safari'][0] = $pourcent_safari;
  15. $array['safari'][1] = $sond_titre_safari;
  16. $array['safari'][2] = $sond_barre_safari;
  17. $array['safari'][3] = $sond_pourc_safari;
  18. $array['konqueror'][0] = $pourcent_konqueror;
  19. $array['konqueror'][1] = $sond_titre_konqueror;
  20. $array['konqueror'][2] = $sond_barre_konqueror;
  21. $array['konqueror'][3] = $sond_pourc_konqueror;
  22. $array['firebird'][0] = $pourcent_firebird;
  23. $array['firebird'][1] = $sond_titre_firebird;
  24. $array['firebird'][2] = $sond_barre_firebird;
  25. $array['firebird'][3] = $sond_pourc_firebird;
  26. $array['seamonkey'][0] = $pourcent_seamonkey;
  27. $array['seamonkey'][1] = $sond_titre_seamonkey;
  28. $array['seamonkey'][2] = $sond_barre_seamonkey;
  29. $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

Répondre à SiM07

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 ?

Répondre à SiM07
Tom's Guide > Forum > Programmation > Probleme de array en PHP
Aller à :

Il y a 415 utilisateurs connus et inconnus. Pour voir la liste des connectés connus, cliquez ici.

Attention

Vous allez répondre sur un sujet resté inactif pendant plus de 6 mois.
Assurez-vous d'apporter des éléments nouveaux à la discussion avant de poursuivre.

Répondre Annuler
Liens