[PHP] Counter output

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Okay, misschien niet de meest makkelijke manier, maar zo is nou eenmaal mijn opdracht :(

Ik heb een MySQL database die als counter moet dienen. Hierin worden diverse dingen opgeslagen:
Resolute / IP / DNS / OS / Browser / Exitpage / Date / Time

Het wegschrijven van de info gaat allemaal perfect.
Echter zijn er nu zo'n 2000+ rows in de tabel, mag ook niet veel problemen zijn, aangezien dit er nog wel meer zullen worden :) (Hij draait vanaf afgelopen vrijdag)

Grote probleem nu is echter, dat de baas graag de output hiervan wil zien, met enkele voorwaarden.

- De laatste 1000 rijen moetten te zien zijn
- Het aantal unieke IP adressen moet bepaald worden (geeft op dit moment een timeout)
- Landen moet bepaald worden. + aantal unieke IPs per dat land.
- Aantal bezoekers per dag moet bepaald worden
- Gemiddeld aantal bezoekers per uur moet bepaald worden
-- (dus gemiddeld 500 bezoekers om 5 uur / gemiddeld 100 bezoekers om 9 uur etc.)

Ik heb er nu dus een paar loops in staan, en hierop komt er gewoon een dikke time-out.
Zijn er toevallig MySQL queries die deze loops kunnen vervangen?

Dit is de code die nu actief is, en dus niet lekker werkt.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    for ($count = 0; $row = mysql_fetch_row ($result); ++$count) {
      $sqlB = "SELECT * FROM domeinen WHERE shortname LIKE '" . $row[4] . "'";
      $resultB = mysql_query($sqlB);
      $rowcountB = mysql_num_rows ($resultB);
      $rowB = mysql_fetch_row ($resultB);
      $landnaam = $rowB[2];
      $landimg = "[img]'../landen/"[/img] ";
      if ($rowcountB == 0) {
        unset($landimg);
        $landnaam = "Unknown";
      }
      if ($rowcountB > 1) {
        $landnaam = $landnaam . " - Guessed";
      }
      echo "<TD>$row[0] / $count</TD><TD>" . long2ip($row[2]) . "</TD><TD>$row[3]</TD><TD>$landimg $landnaam</TD><TD>$row[5]</TD><TD>$row[6]</TD><TD>$row[7]</TD><TD>$row[8]</TD><TD>$row[9]</TD><TD>$row[10]</TD><TD>$row[11]</TD></TR>";
      $all_ips[$count] = $row[2];
      $all_ips2[$count] = $row[2];
      $all_countrys[$count] = $row[4];
      $all_countrys2[$count] = $row[4];
      $totalrecords = $count;
    }
    echo "</TABLE>";
    echo "<HR>";
    echo "Total Records Counted: $rowcount<BR>";
    for ($Acount=0; $Acount <= $rowcount; ++$Acount) {
      for ($Bcount=0; $Bcount <= $rowcount; ++$Bcount) {
        if ($Acount != $Bcount) {
          if ($all_ips[$Acount] == $all_ips[$Bcount]) {
//            echo "Removed IP: " . long2ip($all_ips[$Acount]) . "<BR>";
            unset($all_ips[$Bcount]);
          }
        }
      }
    }
    for ($count=0; $count <= $rowcount; ++$count) {
      if ($all_ips[$count]) {
        ++$unique_ip_count;
        for ($Acount=0; $Acount <= $rowcount; ++$Acount) {
          if ($all_ips[$count] == $all_ips2[$Acount]) {
            ++$all_unique_ip_count[$count];
          }
        }
//        echo "Unique IP found: " . long2ip($all_ips[$count]) . " (" . $all_unique_ip_count[$count] . ")<BR>";
      }
    }
    for ($Acount=0; $Acount <= $rowcount; ++$Acount) {
      for ($Bcount=0; $Bcount <= $rowcount; ++$Bcount) {
        if ($Acount != $Bcount) {
          if ($all_countrys[$Acount] == $all_countrys[$Bcount]) {
//            echo "Removed Country: " . $all_countrys[$Bcount] . "<BR>";
            unset($all_countrys[$Bcount]);
          }
        }
      }
    }
    for ($count=0; $count <= $rowcount; ++$count) {
      if ($all_countrys[$count]) {
        if ($landnaam != 'Unknown') {
          ++$unique_countrys_count;
        }
        for ($Acount=0; $Acount <= $rowcount; ++$Acount) {
          if ($all_countrys[$count] == $all_countrys2[$Acount]) {
            ++$all_unique_countrys_count[$count];
          }
        }
        $sqlB = "SELECT * FROM domeinen WHERE shortname LIKE '" . $all_countrys[$count] . "'";
        $resultB = mysql_query($sqlB);
        $rowcountB = mysql_num_rows ($resultB);
        $rowB = mysql_fetch_row ($resultB);
        $landnaam = $rowB[2];
        $landimg = "[img]'../landen/"[/img] ";
        if ($rowcountB == 0) {
          unset($landimg);
          $landnaam = "Unknown";
        }
        if ($rowcountB > 1) {
          $landnaam = $landnaam . " - Guessed";
        }
        if ($landimg) {
          echo "Unique Country found: " . $landimg . " - " . $landnaam . " (" . $all_unique_countrys_count[$count] . " hits)$
        }
        elseif ($landnaam != 'Unknown') {
          echo "Unique Country found: " . $landnaam . " (" . $all_unique_countrys_count[$count] . " hits)<BR>";
        }
      }
    }
    echo "Total Unique IPs: $unique_ip_count<BR>";
    echo "Total Unique Countrys: $unique_countrys_count<BR>";


Dit moet te verbeteren zijn. (hoop ik) :)

Iemand suggesties?

Acties:
  • 0 Henk 'm!

  • Erkens
  • Registratie: December 2001
  • Niet online

Erkens

Fotograaf

kan je niet wat extra tabellen maken?
waarin je bijvoorbeeld unieke ip's bijhoud enzo, scheelt enorm wat load op de sql server lijkt me ;)

Acties:
  • 0 Henk 'm!

  • sjroorda
  • Registratie: December 2001
  • Laatst online: 15:28
Je unieke IP's controleer je nu in een dubbele loop over alle rows, = 2000, dus een loopje dat 4.000.000 wordt doorlopen. Is SELECT UNIQUE(IP) niet een stuk sneller? ;)

LIKE is ook niet de meest snelle syntax; kijk eens naar MATCH ... AGAINST (o.i.d.)

Ik zie meerdere van dat soort dubbele loops in je code zitten. Probeer zo veel mogelijk door MySQL zelf te laten doen, scheelt een hoop ;).

Acties:
  • 0 Henk 'm!

Verwijderd

Misschien kun je iets met 'DISTINCT' doen?
code:
1
SELECT DISCTINCT(ip) bla bla bla

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
SELECT UNIQUE(IP) <== hoe doe ik die query?
ik heb nu:
PHP:
1
$sqlC = "SELECT UNIQUE(ip) FROM tracker";

maar daar krijg ik een dikke error op.

Acties:
  • 0 Henk 'm!

Verwijderd

De juiste syntax is:
SELECT DISTINCT ip FROM .....
of
SELECT DISTINCTROW ip FROM ....

Acties:
  • 0 Henk 'm!

  • Kama
  • Registratie: Mei 2002
  • Laatst online: 30-06 10:52

Kama

Game Coordinator

En om de laatste 1000 te zien kun je LIMIT gebruiken...

Pas ook op met SELECT *... Ga na of je ECHT alle kolommen nodig hebt.

drs. Kama

Pagina: 1