generic-table-generator

Gesponsorde koppelingen

PHP script bestanden

  1. generic-table-generator

« Lees de omschrijving en reacties

generic_table_generator.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
    ini_set ('display_errors', 1);
    error_reporting (E_ALL | E_STRICT);

    require 'classes/class.core.php';
    require 'classes/class.database.php';
    
    $db = new database ();
    // vul hier je eigen tabelnaam in
    $tabelnaam = 'ckv_instellingen';
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Generic Table Generator</title>
    <style type="text/css">
        @import 'oop.css';
    </style>
</head>

<body>
    <div id="container">
    <div class="wrapper">
    <?php
        // vul hier de velden in die je wilt hebben
        $velden = array ('naam', 'website', 'postcode', 'plaats');
        // deze is voor in de querystring
        $sql_str = implode (', ', $velden);
        // kijken of $_GET['sort_by'] geset is. Zoniet: eerste arrayelement van $velden pakken
        $sort_by = (isset ($_GET['sort_by'])) ? ($_GET['sort_by']) : ($velden[0]);
        // beveiligingscheck: hij moet wel in de array $velden voorkomen
        if (!in_array ($sort_by, $velden)) {
            $sort_by = $velden[0];
        }

        // kijken of $_GET['sort_order'] geset is. Zoniet: 'ASC' pakken
        $sort_order = (isset ($_GET['sort_order'])) ? ($_GET['sort_order']) : ('ASC');
        // beveiligingscheck: het mag alleen ASC of DESC zijn
        if ($sort_order != 'ASC' && $sort_order != 'DESC') {
            $sort_order = 'ASC';
        }

        // querystring in elkaar sleutelen
        $sql = "
            SELECT "
. $sql_str . "
            FROM "
. $tabelnaam . "
            ORDER BY "
. $sort_by . " " . $sort_order . "
            "
;
        if ($db->query ($sql)) {
            echo '<h1>' . $tabelnaam . '</h1>';
            echo PHP_EOL;
            echo '<table>';
            echo PHP_EOL;
            $tbl_str = '<tr>';
            // kolomkoppen in elkaar sleutelen
            foreach ($velden as $veld) {
                if ($veld == $sort_by) {
                    // na het uitvoeren van de query draaien we $sort_order om
                    // als er op dezelfde kolomkop geklikt wordt tenminste

                    $sort_order = ($sort_order == 'ASC') ? ('DESC') : ('ASC');
                    $tbl_str .= '<th><a href="?sort_by=' . $veld . '&amp;sort_order=' . $sort_order . '" title="">' . $veld . '</a></th>';
                }

                else {
                    $tbl_str .= '<th><a href="?sort_by=' . $veld . '&amp;sort_order=ASC" title="">' . $veld . '</a></th>';
                }
            }

            $tbl_str .= '</tr>';
            echo $tbl_str;
            // voor de achtergrondkleur van de rijen
            $color = 0;
            while ($row = $db->fetch ()) {
                echo PHP_EOL;
                // rijen om en om kleuren; de SNELSTE manier!!! :-))))
                echo '<tr class="bg' . ($color++) % 2 . '"><td>';
                echo str_replace ('&', '&amp;', implode ('</td><td>', $row));
                echo '</td></tr>';
            }

            echo '</table>';
        }

        // als er errors zijn, laten zien
        if ($db->has_errs ()) {
            // in een div zetten, zodat je je eigen CSS kan maken
            echo '<div class="errs">';
            foreach ($db->get_errs () as $err) {
                echo $err;
            }

            echo '</div>';
        }

    ?>
    
    </div>
    </div>
</body>
</html>


Het OOP gedeelte:
(deze staan bij mij in de map 'classes')
class.core.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
    class core {
        protected $errs;
        protected $msgs;
        
        /*
        * constructor
        */

        public function __construct () {
            $this->errs = array ();
            $this->msgs = array ();
        }

        
        /*
        * setters
        */

        public function set_err ($err) {
            array_push ($this->errs, $err);
        }


        public function set_msg ($msg) {
            array_push ($this->msgs, $msg);
        }

        
        /*
        * getters
        */

        public function get_errs () {
            return $this->errs;
        }


        public function get_msgs () {
            return $this->msgs;
        }

        
        /*
        * return the number of messages, if any
        * @return int
        */

        public function has_msgs () {
            return count ($this->msgss);
        }

        
        /*
        * return the number of errors, if any
        * @return int
        */

        public function has_errs () {
            return count ($this->errs);
        }
    }

?>

class.database.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
    class database extends core {
        private $db_host;
        private $db_user;
        private $db_pass;
        private $db_name;
        
        protected $res;
        protected $affected_rows;
        protected $num_rows;
        
        public function __construct () {
            parent::__construct ();
            $this->db_host = '*****';
            $this->db_user = '*****';
            $this->db_pass = '*****';
            $this->db_name = '*****';
            if (!mysql_connect ($this->db_host, $this->db_user, $this->db_pass)) {
                array_push ($this->errs, '<p>Connectie met DB is mislukt.</p>');
            }

            if (!mysql_select_db ($this->db_name)) {
                array_push ($this->errs, '<p>Fout bij het selecteren van de database</p>');
            }
        }

        
        public function query ($sql) {
            if (!$this->res = mysql_query ($sql)) {
                array_push ($this->errs, '<p>' . mysql_errno () . ': ' . mysql_error () . '</p>');
                array_push ($this->errs, '<pre>' . htmlentities (str_replace ("\t", '', $sql)) . '</pre>');
                return false;
            }

            else {
                return true;
            }
        }

        
        public function fetch () {
            return mysql_fetch_assoc ($this->res);
        }

        
        public function get_affected_rows () {
            return $this->affected_rows;
        }

        
        public function get_num_rows () {
            return mysql_num_rows ($this->res);
        }

        
        public function get_ip () {
            if (@getenv ($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = @getenv ($_SERVER['HTTP_X_FORWARDED_FOR']);
            }

            elseif (@getenv ($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = @getenv ($_SERVER['HTTP_CLIENT_IP']);
            }

            else {
                $ip = $_SERVER['REMOTE_ADDR'];
            }

            return $ip;
        }
    }

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.