<?php
function statystyka()
{
  if(!$link = mysql_connect("localhost", "php", "test")){
    //echo("Problem z połączeniem z bazą danych.");
    return;
  }
  if(!mysql_select_db("testphp")){
    //echo("Problem z wyborem bazy testphp.");
    @mysql_close($link);
    return;
  }

  $arr_systemy = array('Windows', 'Linux');
  $arr_systemy_res = array();
  $arr_browsers = array('chrome', 'firefox', 'msie', 'opera');
  $arr_browsers_res = array();

  foreach($arr_systemy as $val){
    $query = "SELECT COUNT(*) FROM Stats WHERE agent LIKE '%$val%'";

    if(!$result = mysql_query($query)){
      //echo("Problem z baz&plusmn; danych: odrzucone zapytanie");
      $arr_systemy_res[] = 0;
    }
    else{
      if(!$row = mysql_fetch_row($result)){
        $arr_systemy_res[] = 0;
      }
      else{
        $arr_systemy_res[] = $row[0];
      }
    }
  }

  foreach($arr_browsers as $val){
    $query = "SELECT COUNT(*) FROM Stats WHERE agent LIKE '%$val%'";
    if(!$result = mysql_query($query)){
      //echo("Problem z baz&plusmn; danych: odrzucone zapytanie");
      $arr_browsers_res[] = 0;
    }
    else{
      if(!$row = mysql_fetch_row($result)){
        $arr_browsers_res[] = 0;
      }
      else{
        $arr_browsers_res[] = $row[0];
      }
    }
  }

  echo "Windows  {$arr_systemy_res[0]}<br />";
  echo "Linuks  {$arr_systemy_res[1]}<br />";
  echo "Razem ", $arr_systemy_res[0] + $arr_systemy_res[1];
  echo "<br /><br />";

  $count = 0;
  foreach($arr_browsers_res as $key => $val){
    echo $arr_browsers[$key] . " " . $val . "<br />";
    $count += $val;
  }

  echo "Razem ", $count;

  @mysql_close($link);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<title>Moja strona WWW</title>
</head>
<body>
<div>
<?php
statystyka();
?>
</div>
</body>
</html>
