basis-nieuwsscript

Gesponsorde koppelingen

PHP script bestanden

  1. basis-nieuwsscript

« Lees de omschrijving en reacties

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
152
153
154
155
156
157
158
159
160
<?php
$max_items
= 5; //maximaal aantal te tonen news items

//het maken van een mysql connectie

$db = mysql_connect ('localhost','root','');
mysql_select_db ('test',$db);

//Hieronder niks veranderen als je niks van php weet.
function displayNews($all = 0) {
    //het samevoegen van de twee varibalen $db en $max_items
    global $db, $max_items;
    
    if ($all == 0) {
        //Het weergeven volgens het $max_items
        $query = "SELECT id,title,newstext," .
                 "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
                 "FROM news ORDER BY postdate DESC LIMIT $max_items";
    }
else {
        //Weergeven van al het nieuws als $max_items niet is ingevuld
        $query = "SELECT id,title,newstext," .
                 "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
                 "FROM news ORDER BY postdate DESC";
    }

    $result = mysql_query ($query);
    while ($row = mysql_fetch_assoc ($result)) {
        //Nieuws weergevn via een zeer simpele table constructie
        echo "<TABLE border=\"1\" width=\"300\">\n";

        //het verwijderen van de html tags
        $date = $row['date'];        
        $title = htmlentities ($row['title']);
        $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
        
        //Het nieuws outputten
        echo "<TR><TD><b>$title</b> posted on $date</TD></TR>\n";
        echo "<TR><TD>$news</TD></TR>\n";
        
        //Het aantal commentaren tellen
        $comment_query = "SELECT count(*) FROM news_comments " .
                         "WHERE news_id={$row['id']}";
        $comment_result = mysql_query ($comment_query);
        $comment_row = mysql_fetch_row($comment_result);
        
        //Het weergeven vaan een commentaar link
        echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}" .
             "?action=show&id={$row['id']}\">Comments</a>" .
             "($comment_row[0]}</TD></TR>\n";
        
        //Tabel afsluiten
        echo "</TABLE>\n";
        echo "<BR>\n";
    }

    
    //Link weergeven als niet al het nieuws te zien is
    if ($all == 0) {
        echo "<a href=\"{$_SERVER['PHP_SELF']}" .
             "?action=all\">View all news</a>\n";
    }
}

function
displayOneItem($id) {
    global $db;
    
    //Weergeven van een bepaalde item
    $query = "SELECT * FROM news WHERE id=$id";
    $result = mysql_query ($query);
    
    //Als geen resultaat dan:
    if (mysql_num_rows ($result) == 0) {
        echo "Bad news id\n";
        return;
    }

    $row = mysql_fetch_assoc($result);
    echo "<TABLE border=\"1\" width=\"300\">\n";

    //html troep eruit filteren
    $title = htmlentities ($row['title']);
    $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
    
    //items weergeven
    echo "<TR><TD><b>$title</b></TD></TR>\n";
    echo "<TR><TD>$news</TD></TR>\n";
    
    echo "</TABLE>\n";
    echo "<BR>\n";
    
    //commentaar tonen
    displayComments($id);
}

function
displayComments($id) {
    global $db;
    
    //query voor commentaar
    $query = "SELECT * FROM news_comments WHERE news_id=$id";
    $result = mysql_query ($query);
    echo "Comments:<BR><HR width=\"300\">\n";
    
    //al het commentaar weergeven
    while ($row = mysql_fetch_assoc ($result)) {
        echo "<TABLE border=\"1\" width=\"300\">\n";
        
        $name = htmlentities ($row['name']);
        echo "<TR><TD><b>by: $name</b></TD></TR>\n";
    
        $comment = strip_tags ($row['comment'], '<a><b><i><u>');
        $comment = nl2br ($comment);
        echo "<TR><TD>$comment</TD></TR>\n";
    
        echo "</TABLE>\n";
        echo "<BR>\n";
    }

    
    //nieuw commentaar formulier invoegen
    echo "<HR width=\"300\">";
    echo "<FORM action=\"{$_SERVER['PHP_SELF']}" .
         "?action=addcomment&id=$id\" method=POST>\n";
    echo "Name: <input type=\"text\" " .
         "width=\"30\" name=\"name\"><BR>\n";
    echo "<TEXTAREA cols=\"40\" rows=\"5\" " .
         "name=\"comment\"></TEXTAREA><BR>\n";
    echo "<input type=\"submit\" name=\"submit\" " .
         "value=\"Add Comment\"\n";
    echo "</FORM>\n";
    
}

function
addComment($id) {
    global $db;
    
    //commentaar invoegen
    $query = "INSERT INTO news_comments " .
             "VALUES('',$id,'{$_POST['name']}'," .
             "'{$_POST['comment']}')";
    mysql_query($query);
    
    echo "Comment entered. Thanks!<BR>\n";
    echo "<a href=\"{$_SERVER['PHP_SELF']}" .
         "?action=show&id=$id\">Back</a>\n";
}


//de output

echo "<CENTER>\n";
switch($_GET['action']) {
    
    case
'show':
        displayOneItem($_GET['id']);
        break;
    case
'all':
        displayNews(1);
        break;
    case
'addcomment':
        addComment($_GET['id']);
        break;
    default:

        displayNews();
}

echo "</CENTER>\n";
?>

 
 

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.