generic-table-edit

Gesponsorde koppelingen

PHP script bestanden

  1. generic-table-edit

« Lees de omschrijving en reacties

config.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
<?php
    $config
= array (
        'title' => 'Jan Koehoorn | TableEdit',
        'table' => 'table_edit',
        'list_field' => 'naam',
        'edit_fields' => array (
                        'id' => 'hidden',
                        'naam' => 'text',
                        'adres' => 'text',
                        'woonplaats' => 'text',
                        'beschrijving' => 'textarea'
                        )
        );

    $db = new database ();
?>


business_logic.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
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
<?php
    ini_set ('display_errors', 1);
    error_reporting (E_ALL);
    $fields = array_keys ($config['edit_fields']);
    $scrolltop = (isset ($_GET['scrolltop'])) ? ($_GET['scrolltop']) : (0);
    $id = 0;
    if (isset ($_GET['id']) && ctype_digit ($_GET['id'])) {
            $id = $_GET['id'];
    }

        
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (isset ($_POST['id'])) {
            $sql_update = "
                UPDATE "
. $config['table'] . "
                SET
                "
;
            foreach ($fields as $field) {
                if ($field != 'id') {
                    $sql_update .= PHP_EOL;
                    $sql_update .= $field . ' = \'' . mysql_real_escape_string ($_POST[$field]) . '\',';
                }
            }

            $sql_update = substr ($sql_update, 0, -1);
            $sql_update .= PHP_EOL . 'WHERE id = ' . $_POST['id'];
            if ($db->query ($sql_update)) {
                if ($db->get_affected_rows () == 1) {
                    $db->set_msg ('<p>Record opgeslagen</p>');
                }
            }
        }

        else {
            $sql_insert = "
                INSERT INTO "
. $config['table'] . "
                ("
. implode (', ', array_keys ($_POST)) . ")
                VALUES
                (
                "
;
            foreach ($fields as $field) {
                if ($field != 'id') {
                    $sql_insert .= PHP_EOL;
                    $sql_insert .= '\'' . mysql_real_escape_string ($_POST[$field]) . '\',';
                }
            }

            $sql_insert = substr ($sql_insert, 0, -1);
            $sql_insert .= ')';
            if ($db->query ($sql_insert)) {
                if ($db->get_affected_rows () == 1) {
                    $db->set_msg ('<p>Record opgeslagen</p>');
                }
            }
        }
    }

    $nav_html = '';
    $nav_html .= '<ul>';
    $nav_html .= '<li><a href="?action=new">nieuw</a></li>';
    if ($id) {
        $nav_html .= '<li><a id="delete" href="?action=delete&amp;id=' . $id . '">wissen</a></li>';
    }

    $nav_html .= '</ul>';
    if ($id) {
        if (isset ($_GET['action'])) {
            switch ($_GET['action']) {
                case
'new':
                    break;
                case
'delete':
                    $sql_delete = "
                        DELETE FROM "
. $config['table'] . "
                        WHERE id = "
. $id . "
                        LIMIT 1
                        "
;
                    if ($db->query ($sql_delete)) {
                        if ($db->get_affected_rows () == 1) {
                            $db->set_msg ('<p>Record gewist</p>');
                        }
                    }

                    break;
            }
        }
    }

    
    $list_html = '';
    $sql_select = "
        SELECT id, "
. $config['list_field'] . "
        FROM "
. $config['table'] . "
        ORDER BY "
. $config['list_field'] . " ASC
        "
;
    if ($db->query ($sql_select)) {
        $list_html .= PHP_EOL . '<ul>';
        while ($row = $db->fetch ()) {
            $current = ($row['id'] == $id) ? (' class="current"') : ('');
            $list_html .= '<li><a' . $current . ' href="?action=edit&amp;id=' . $row['id'] . '">' . $row[$config['list_field']] . '</a></li>';
        }

        $list_html .= PHP_EOL . '</ul>';
    }

    $edit_html = '';
    if ($id) {
        $sql_edit = "
            SELECT "
. implode (', ', $fields) . "
            FROM "
. $config['table'] . "
            WHERE id = "
. $id . "
            LIMIT 1
            "
;
        if ($db->query ($sql_edit)) {
            if ($row = $db->fetch ()) {
                $edit_html .= '<form method="post" action="' . $_SERVER['REQUEST_URI'] . '">';
                $edit_html .= '<p>';
                $edit_html .= '<input type="submit" value="opslaan" />';
                $edit_html .= '</p>';
                foreach ($fields as $field) {
                    $edit_html .= '<p>';
                    switch ($config['edit_fields'][$field]) {
                        case
'hidden':
                            $edit_html .= '<input id="' . $field . '" name="' . $field . '" type="hidden" value="' . $row[$field] . '">';
                            break;
                        case
'text':
                            $edit_html .= '<label class="floated" for="' . $field . '">' . $field . ':</label>';
                            $edit_html .= '<input class="medium" id="' . $field . '" name="' . $field . '" type="text" value="' . $row[$field] . '">';
                            break;
                        case
'textarea':
                            $edit_html .= '<label class="floated" for="' . $field . '">' . $field . ':</label>';
                            $edit_html .= '<textarea class="medium" id="' . $field . '" name="' . $field . '">' . $row[$field] . '</textarea>';
                            break;
                    }

                    $edit_html .= '</p>';
                }

                $edit_html .= '</form>';
            }
        }
    }

    else {
        $edit_html .= '<form method="post" action="' . $_SERVER['REQUEST_URI'] . '">';
        $edit_html .= '<p>';
        $edit_html .= '<input type="submit" value="opslaan" />';
        $edit_html .= '</p>';
        foreach ($fields as $field) {
            $edit_html .= '<p>';
            switch ($config['edit_fields'][$field]) {
                case
'text':
                    $edit_html .= '<label class="floated" for="' . $field . '">' . $field . ':</label>';
                    $edit_html .= '<input class="medium" id="' . $field . '" name="' . $field . '" type="text" value="' . $row[$field] . '">';
                    break;
                case
'textarea':
                    $edit_html .= '<label class="floated" for="' . $field . '">' . $field . ':</label>';
                    $edit_html .= '<textarea class="medium" id="' . $field . '" name="' . $field . '">' . $row[$field] . '</textarea>';
                    break;
            }

            $edit_html .= '</p>';
        }

        $edit_html .= '</form>';
    }

?>


index.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
<?php
    ini_set ('display_errors', 1);
    error_reporting (E_ALL);
    require '../classes/class.core.php';
    require '../classes/class.database.php';
    require 'config.php';
    require 'business_logic.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $config['title']; ?></title>
    
    <script type="text/javascript" src="../mootools/mootools.js"></script>
    <script type="text/javascript" src="table_edit.js"></script>
    <script type="text/javascript">
        window.addEvent ('domready', function () {
            $('list').scrollTop = <?php echo $scrolltop; ?>;
        });
    </script>
    
    <link rel="stylesheet" type="text/css" media="screen" href="reset.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="table_edit.css" />
</head>

<body>
    <div id="container">
        <div id="header">
            <?php echo '<h1>Tabel: ' . $config['table'] . '</h1>'; ?>
        </div>
        <div id="nav">
            <?php echo $nav_html; ?>
        </div>
        <?php
            echo $db->get_msgs_html ();
            echo $db->get_errs_html ();
        ?>

        <div id="content-container">
            <div id="list">
            <?php echo $list_html; ?>
            </div>
            <div id="record">
            <?php echo $edit_html; ?>
            </div>
        </div>
    </div>
</body>
</html>


reset.css
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
    
    html                                { font-size: 100%; }
    body                                { font: 62.5%/1em Verdana, Geneva, Arial, Helvetica, sans-serif; }

    html, body, div,
    dl, dt, dd, ul, ol, li,
    h1, h2, h3, h4, h5, h6, pre,
    form, fieldset, input, select, option, textarea,
    p, blockquote, th, td                { margin: 0; padding: 0; }
    table                                { border-collapse: collapse; border-spacing: 0; }
    fieldset, img                        { border: 0; }
    address, caption, cite, code,
    dfn, em, strong, th, var            { font-style: normal; font-weight: normal; }
    ol, ul                                { list-style: none; }
    caption, th                            { text-align: left; }
    h1, h2, h3, h4, h5, h6                { font-weight: normal; }
    q:before, q:after                    { content: '"'; }
    abbr, acronym                        { border: 0; }
    :focus                                { outline: 0; }
    


table_edit.css
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
    div#container                    {width: 800px; margin: 0 auto; border: 1px solid #ccc; font: 1.2em Verdana, Arial, Helvetica, sans-serif;}

    div#header                        {height: 50px; padding: 10px;}
    div#header h1                    {font: bold 2em Arial, Helvetica, sans-serif; color: #900; margin-left: 200px;}
    div#nav                            {height: 20px; background: #ccc;}
    div#nav ul                        {overflow: hidden; width: 300px; margin-left: 200px;}
    div#nav ul li                    {float: left;}
    div#nav ul li a                    {text-decoration: none; display: block; padding: 3px 10px; color: #000;}
    div#nav ul li a:link,
    div#nav ul li a:visited            {}
    div#nav ul li a:hover,
    div#nav ul li a:active            {background: #ff0;}
        
    div#content-container            {overflow: hidden; width: 100%;}
    div#list                        {overflow: auto; float: left; width: 200px; height: 400px;}
    div#list ul                        {}
    div#list ul li                    {}
    div#list ul li a                {color: #000; text-decoration: none; display: block; padding: 5px;}
    div#list ul li a.current        {color: #fff; background: #900;}
    div#list ul li a:link,
    div#list ul li a:visited        {}
    div#list ul li a:hover,
    div#list ul li a:active            {color: #000; background: #ff0;}
    
    div#record                        {margin: 0 0 0 200px;}
    
    form p                            {margin: 10px;}
    label.floated                    {float: left; width: 180px;}
    .medium                            {width: 380px;}
    input, textarea                    {font: 12px Verdana, Arial, Helvetica, sans-serif;}
    
    div.errs            { background: #fcc url(bull_exclamation.gif) 5px 5px no-repeat; border: 2px solid #f99; margin: 10px; padding: 20px;}
    div.msgs            { background: #ccf url(bull_msg.gif) 5px 5px no-repeat; border: 2px solid #99f; margin: 10px; padding: 20px; }


table_edit.js
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
    window.addEvent ('domready', function () {
        $$('#list a').each (function (item) {
            item.addEvent ('click', function (e) {
                e =new Event (e).preventDefault ();
                window.location = this.href + '&scrolltop=' + $('list').scrollTop;
            });
        });
        $('delete').addEvent ('click', function (e) {
            if (!confirm ('Record wissen. Zeker weten?')) {
                e =new Event (e).preventDefault ();
            }
        });
    });


Tot slot nog een link naar de plaatjes:

Afbeelding
Afbeelding

 
 

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.