Bonjour,

Je débute en POO et j'aimerai être aidé su une fonction.
Je crée une class qui me permet d'enregistrer des statistiques.

Voici le code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
 
<?php
class Stats{
    public $tablet_browser;
    public $mobile_browser;
    public $current_date;
    public $ip;
    public $host;
    public $referer;
    public $current_page;
    public $user_agent;
    public $browser;
    public $mobile_ua;
    public $mobile_agents;
    public $stock_ua;
    public $support;
    public $os_platform;
    public $os_array;
 
 
    public function statsFront(){
        $this->current_date = date("Y-m-d H:i:s");
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $this->ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
            $this->ip  = $_SERVER['HTTP_CLIENT_IP'];
        }else{
            $this->ip = $_SERVER['REMOTE_ADDR'];
        }
        $this->host = gethostbyaddr($this->ip);
        if(isset($_SERVER['HTTP_REFERER'])){
            $this->referer = $_SERVER['HTTP_REFERER'];
        }else{
            $this->referer = 'Direct';
        }
 
        // Récupération du nom de la page courante ainsi que ses arguments
        if($_SERVER['QUERY_STRING'] == ''){
            $this->current_page = $_SERVER['PHP_SELF'];
        }else{
            $this->current_page = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        }
 
        //Browser
        $this->user_agent = $_SERVER['HTTP_USER_AGENT'];
        $this->browser = 'Unknown Browser';
        if(preg_match('/MSIE/i',$this->user_agent) && !preg_match('/Opera/i',$this->user_agent)){
            $this->browser = 'Explorer'; 
        }elseif(preg_match('/Firefox/i',$this->user_agent)){
            $this->browser = 'Firefox'; 
        }elseif(preg_match('/Chrome/i',$this->user_agent)){
            $this->browser = 'Chrome';
        }elseif(preg_match('/Safari/i',$this->user_agent)){
            $this->browser = 'Safari';
        }elseif(preg_match('/Opera/i',$this->user_agent)){
            $this->browser = 'Opera';
        }elseif(preg_match('/Netscape/i',$this->user_agent)){
            $this->browser = 'Netscape';
        }else{
            $this->browser = 'Unknown Browser';
        }
 
        //TABLET, PC OR MOBILE
        if(preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i',strtolower($_SERVER['HTTP_USER_AGENT']))){
            $this->tablet_browser++;
        }
        if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i',strtolower($_SERVER['HTTP_USER_AGENT']))){
            $this->mobile_browser++;
        }
        if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
            $this->mobile_browser++;
        }
        $this->mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
        $this->mobile_agents = array(
            'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
            'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
            'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
            'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
            'newt','noki','palm','pana','pant','phil','play','port','prox',
            'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
            'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
            'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
            'wapr','webc','winw','winw','xda ','xda-');
        if(in_array($this->mobile_ua,$this->mobile_agents)){
            $this->mobile_browser++;
        }
        if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
            $this->mobile_browser++;
            //Check for tablets on opera mini alternative headers
            $this->stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
            if(preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $this->stock_ua)){
                $this->tablet_browser++;
            }
        }
        if(isset($this->tablet_browser) > 0){
           $this->support = 'Tablet';
        }elseif(isset($this->mobile_browser) > 0){
           $this->support = 'Mobile';
        }else{
           $this->support = 'Desktop';
        } 
 
        // OS SYSTEM
        $this->os_platform = 'Unknown OS Platform';
        $this->os_array = array(
            '/windows nt 6.2/i'     => 'Windows 8',
            '/windows nt 6.1/i'     => 'Windows 7',
            '/windows nt 6.0/i'     => 'Windows Vista',
            '/windows nt 5.2/i'     => 'Windows Server 2003/XP x64',
            '/windows nt 5.1/i'     => 'Windows XP',
            '/windows xp/i'         => 'Windows XP',
            '/windows nt 5.0/i'     => 'Windows 2000',
            '/windows me/i'         => 'Windows ME',
            '/win98/i'              => 'Windows 98',
            '/win95/i'              => 'Windows 95',
            '/win16/i'              => 'Windows 3.11',
            '/macintosh|mac os x/i' => 'Mac OS X',
            '/mac_powerpc/i'        => 'Mac OS 9',
            '/linux/i'              => 'Linux',
            '/ubuntu/i'             => 'Ubuntu',
            '/iphone/i'             => 'iPhone',
            '/ipod/i'               => 'iPod',
            '/ipad/i'               => 'iPad',
            '/android/i'            => 'Android',
            '/blackberry/i'         => 'BlackBerry',
            '/webos/i'              => 'Mobile'
        );
        foreach($this->os_array as $regex => $value){ 
            if(preg_match($regex, $this->user_agent)){
                $this->os_platform = $value;
            } 
        } 
        $sql = Db::getInstance()->query("INSERT INTO stats (s_id_stat,s_date,s_year,s_month,s_day,s_hour,s_page,s_address_ip,s_os,s_host,s_browser,s_support,s_lang,s_referer)
              VALUES 
              ('','$this->current_date','".date("Y")."','".date("n")."','".date("j")."','".date("G")."','$this->current_page','$this->ip','$this->os_platform','$this->host','$this->browser','$this->support','".substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)."','$this->referer')");
    }
 
    /**
     * Permet de connaitre le nombre de jour dans un mois
     * @param int $month
     * @param int $year
     */
    public function nbDayMonth($month,$year){ 
        $month = ($month) ? $month : date('m'); 
        $year = ($year) ? $year : date('Y'); 
        return intval(date('t',strtotime("$year-$month-01"))); 
    }
 
 
    public function getHours(){
        for($h = 0;$h < 24;$h++){
            if($h <= 9){
                $h = '0'.$h;
            }
            echo "'".$h." H',";
        }   
    }
 
    /**
     * Permet d'avoir les statistiques par heure sur le jour courant
     */
    public function setStatsHour(){
        for($h = 0;$h < 24;$h++){
            $sql = Db::getInstance()->query("SELECT DISTINCT(s_address_ip) FROM stats WHERE s_year=".date('Y')." AND s_month=".date('n')." AND s_day=".date('j')." AND s_hour=".$h);
            $resultday[] = $sql->num_rows;
        }
    }
    public function getStatsHour(){
        $this->setStatsHour();
        foreach ($resultday as $result_day){
            print_r($result_day.',');
        }
    }
    /**
     * Permet d'avoir les visites uniques par mois
     */
    public function getStatsMonth(){
        for($i = 1;$i <= $this->nbDayMonth((date('n')),date('Y'));$i++){
            $sql = Db::getInstance()->query("SELECT DISTINCT(s_address_ip) FROM stats WHERE s_year=".date('Y')." AND s_month=".date('n')." AND s_day=".$i);
            $result[] = $sql->num_rows;
            echo "'".$i." / ".date('m')."',<br>";
        }
        $i = 0;
        foreach ($result as $result_month){
            echo $result_month.',';
        }
    }
}
?>
J'aimerai ajouter un fonction comme ceci afin d'avoir tout dans ma classe:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
for($h = 0;$h < 24;$h++){
    $sql = Db::getInstance()->query("SELECT DISTINCT(s_address_ip) FROM stats WHERE s_year=".date('Y')." AND s_month=".date('n')." AND s_day=".date('j')." AND s_hour=".$h);
    $resultday[] = $sql->num_rows;
}
foreach ($resultday as $result_day){
     echo $result_day.',';
}
Est ce que quelqu'un pourrait m'expliquer comment m'y prendre ?

Si je fais mon code en dehors de ma classe cela fonctionne, mais je trouve que cela fait "brouillon"

Merci d'avance