Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 68
0 Users 3 Guests Online

News System

A little bit of credit is due.
Some codes were taken and re-written from the Plus News 2 Script (Flood Time)
First off run these queries in your phpMyAdmin.
Code

CREATE TABLE `comments` (
`id` int(10) NOT NULL auto_increment,
`author` varchar(30) NOT NULL default '',
`content` text NOT NULL,
`postdate` varchar(125) NOT NULL default '',
`nid` varchar(30) NOT NULL default '',
`ip` varchar(30) NOT NULL default ''
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `flood` (
`id` int(10) NOT NULL auto_increment,
`ip` varchar(50) NOT NULL,
`time` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `news` (
`id` int(10) NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`author` varchar(30) NOT NULL default '',
`content` text NOT NULL,
`postdate` varchar(255) NOT NULL default '',
`cat_id` int(2) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `news_cats` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default 'Untitled',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

Thats that these are all the tables you will need to run the news system.

Next in your config.php file please add the following lines of code:
Code

function _Codes_($num){ //declare the function
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"; //available characters
$code = substr(str_shuffle($chars), 0, $num); //create random string
return $code; //return the code
} //then end the function


That is the function we use to create the verification code image ^^

Next you will want to make a file and name it img.php. In this file please type something like the following:
PHP Code
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
<?php
session_start
();
include(
"config.php");
$circles rand(05); //make some random circles
$width '100'//the width
$height '25'//the height you want
$font rand(811); //random font size
    
$string $_GET['text']; //the text to add to the image
$fontwidth ImageFontWidth($font) * strlen($string); //font width
$fontheight ImageFontHeight($font); //font height

$im = @imagecreate($width$height); //create the image
$background_color imagecolorallocate($im255255255); //the background color
$text_color imagecolorallocate($imrand(0100), rand(0100), rand(0100)); //text color, randomness

for ($i 1$i <= $circles$i++) { //loop for circles
    
$randomcolor imagecolorallocate($imrand(100255), rand(100255), rand(100,
        
255)); //create the circles
    
imagefilledellipse($imrand(0$width 10), rand(0$height 3), rand(2070),
        
rand(2070), $randomcolor); //fill elipse with same color
//end the loop
imagerectangle($im00$width 1$height 1$text_color); //create the rectangle shape for the image
imagestring($im$fontrand(3$width $fontwidth 3), rand(2$height $fontheight -
    
3), $string$text_color); //add the text
header("Content-type: image/jpeg"); //creat the haeder of JPG
imagejpeg($im''80); //create the image and shown if displayed somewhere on the server
?>

Next, call this file news.php and make sure you read the comments. there is a crapload to it.
PHP Code
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
session_start
();
include(
"config.php");
if (!
$_GET['page']) { //check for page
    
$page 1//no page so its 1
} else { //there is a page
    
$page = (int)$_GET['page']; //fix the mistakes user made secure and such ^^
//end page check
switch ($_GET['x']) { //start a navigation type thing.
    
default: //default page
        
$max_results 5//max results per page
        
$current = (($page $max_results) - $max_results); //current page
        
$get_news mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT $current$max_results"); //get news
        
$news_total mysql_query("SELECT * FROM `news`"); //total for later
        
$counttotal mysql_num_rows($news_total); //count the total news
        
$total_pages ceil($counttotal $max_results); //total pages
        
if (mysql_num_rows($get_news) == 0) { //if theres no news to show
            
print "<div>
        <h1>Error</h1>
        <p>There is no News in the Database.</p>
        </div>"
//give them an error 
        
} else { //there is some
            
while ($news mysql_fetch_array($get_news)) { //make a loop to show news
                
$total_comments mysql_query("SELECT * FROM `comments` WHERE `nid` = '$news[id]';"); //get total comments
                
print "<div>
            <h1>" 
stripslashes($news[title]) . "</h1>
            <p>Posted on " 
$news[postdate] . " by <a href=\"members.php?user=$news[author]\">" $news[author] .
                    
"</a></p>
                <p>" 
stripslashes($news[content]) .
                    
"</p>
            <p><a href=\"news.php?x=more&id=
$news[id]/\">Comments (" .
                    
mysql_num_rows($total_comments) . ")</a></p>
        </div>"
//clear up the slashes and sow news post ;)
            
//end loop
            
print "<div>
            <h1>Page Navigation</h1>
            <p>"
//Pagination >/
            
if ($page 1) { //page is there and greater then 1
                
$prev = ($page 1); //set page 1 less tghen current
                
print "<a href=\"news.php?page=$prev\">&laquo; Previous</a> "//show link
            
//end check
            
for ($i 1$i <= $total_pages$i++) { //show all pages

                
if ($page == $i) { //if the current is the same as the number
                    
print "<b>" $i "</b> "//dont give link
                
} else { /or maybe it aint
                    
print "<a href=\"news.php?page=$i\">" $i "</a> "//give link
                
//end check
            
//end loop
            
if ($page $total_pages) { //if page is less then
                
$next = ($page 1); //next link increment
                
print "<a href=\"news.php?page=$next\">Next &raquo;</a>"//give link
            
//end check
            
print "</p>
            </div>"
//end paragraph and div entry
        
//end checks
        
break; //end default page >/
    
case 'more'//the comments display page
        
$id = (int)$_GET['id']; //fix up the ID
        
if (!$id) { //no id selected
            
print "<div>
        <h1>Error</h1>
        <p>
        No ID Selected.
        </p>
        </div>"
//give error
        
} else { //there was an ID
            
$check mysql_query("SELECT * FROM `news` WHERE `id` = '$id';"); //check for Item in DB
            
if (mysql_num_rows($check) == 0) { //if there aint
                
print "<div>
        <h1>Error</h1>
        <p>
        Invalid ID Selected.
        </p>
        </div>"
//show error
            
} else { //or its there
                
$array_news_item mysql_fetch_array($check); //array item
                
print "<div>
            <h1>" 
stripslashes($array_news_item[title]) . "</h1>
            <p><small>Posted on " 
$array_news_item[postdate] . " by <a href=\"members.php?user=$news[author]\">" $array_news_item[author] .
                    
"</a></small></p>
                <p>" 
stripslashes($array_news_item[content]) .
                    
"
                    </p>
                </div>"
//print info
                        
$max_com_results 15//max comments per page
                        
$current_page = (($page $max_com_results) - $max_com_results); //current page
                        
$get_comments mysql_query("SELECT * FROM `comments` ORDER BY `id` DESC LIMIT $current_page$max_com_results"); //get comments
                        
$totalcomsnew mysql_num_rows(mysql_query("SELECT * FROM `comments`")); //total
                        
if (mysql_num_rows($get_comments) == 0) { //if there isn't any
                            
print "<div>
        <h1>Error</h1>
        <p>
        There Are Currently No Comments.
        <a href=\"news.php?x=addcomment&id=
$id\">Add Comment</a>
        </p>
        </div>"
//give error
                        
} else { //or there were
                            
print "<div>
                            <h1>Page Navigation</h1>
                            <p>"
//navigation
                            
$total_pages ceil($totalcomsnew $max_com_results); //total pages 
                            
if ($page 1) { //current > 1
                                
$prev = ($page 1); //set to less then curent
                                
print "<a href=\"news.php?x=more&id=$id&page=$prev\">&laquo; Previous</a> "//give link
                            
//end check
                            
for ($i 1$i <= $total_pages$i++) { //make loop

                                
if ($page == $i) { //fi current is same as number
                                    
print "<b>" $i "</b> "//dont give link
                                
} else { //or it aint
                                    
print "<a href=\"news.php?x=more&id=$id&page=$i\">" $i "</a> "//give link
                                
//end page
                            
//end check
                            
if ($page $total_pages) { //next link
                                
$next = ($page 1); //set to one more then current
                                
print "<a href=\"news.php?x=more&id=$id&page=$next\">Next &raquo;</a>"//give link
                            
//end check
                            
print "</p></div>
                            <div>
                            <h1>Comments</h1>"
//end onediv and start new comments one
                            
while ($comments mysql_fetch_array($get_comments)) { //make a loop
                                
print "<table width=\"500\">
                                <tr>
                                <td colspan=\"5\" align=\"left\" valign=\"middle\">
                                <a href=\"news.php?x=addcomment&id=
$id/\">Add New Comment</a>&nbsp;&bull;&nbsp;<b>Posted On</b>: $comments[postdate]
                                </td>
                                </tr>
                                <tr>
                                <td align=\"center\" valign=\"middle\" style=\"width:150px;\">
                                <b>Posted By</b>:
                                " 
stripslashes($comments[author]) . "
                                </td>
                                <td align=\"center\" valign=\"top\" style=\"width:350px;\">
                                " 
stripslashes($comments[content]) .
                                    
"
                                </td>
                                </tr>
                                </table>"
//show comment info
                            
//end loop
                            
print "</div>"//end div
                        
//end comments check
                    
//end news id invalid check
        
//end final id check
        
break; //end display news page.
    
case 'addcomment'//add a new comment to the article
        
$id = (int) $_GET['id']; //fix id
        
if (!$id) { //if no ID 
            
print "<div>
            <h1>Error</h1>
            <p>
            No ID Selected.
            </p>
            </div>"
//give error
        
} else {//or mebbe
            
$check mysql_query("SELECT * FROM `news` WHERE `id` = '$id';"); //check it
            
$arraynews mysql_fetch_array($check);//array the nwes
            
if (mysql_num_rows($check) == 0) { //if not existing
                
print "<div>
                <h1>Error</h1>
                <p>
                Invalid ID Selected.
                </p>
                </div>"
//give error
            
} else { //or its there
                
if (!$_POST['addcomment']) { //if forms not submitted
                           
$_SESSION['XXX_SEC_CODE_SEC_XXX'] = _Codes_(6); //generate a new security code
                            
print "<div>
                            <h1>Add Comment to 
$arraynews[title]</h1>
            <p>
            <form method=\"post\" action=\"news.php?x=addcomment&id=
$id\">
            <fieldset style=\"border:0px solid #000000;\">
            <b>Your Name</b>:
            <input type=\"text\" name=\"name\" size=\"15\" />
            <b>Content</b>: 
            <textarea rows=\"5\" cols=\"25\" name=\"msg\"></textarea>
            <b>Image Verification</b>:
            *Enter the following backwards*
            <img src=\"ver.jpg?text=
$_SESSION[XXX_SEC_CODE_SEC_XXX]\" alt=\"Verification Image\" />
            <input type=\"text\" size=\"15\" name=\"verif\" />
            <input type=\"submit\" name=\"addcomment\" value=\"Post Comment\" />
            </fieldset>
            </form>
            </p>
            </div>"
//print the form and security information to be inputted
                
} else { //forms submitted
                    //all our variables
                    
$name strip_tags(htmlspecialchars(addslashes($_POST['name'])));
                    
$check_name mysql_query("SELECT * FROM `users` WHERE `username` = '$name';");
                    
$msg strip_tags($_POST['msg'], "<a><p><ul><li><ol><em><span><strong>");
                    
$msg addslashes($msg);
                    
$sscode $_POST['verif'];
                    
$time date("YmdHis");
                    
$ftime date("YmdHis"time() + 15);
                    
$postdate date("j F Y");
                    
$errors = array(); //errors array
                    
if (empty($name)) { //no nam,e
                        
$errors[] = "You Must Enter a Name";
                    }
                    if (
mysql_num_rows($check_name) == && !$logged['username']) { //names protected
                        
$errors[] = "Name Protected. Please login to post with this name.";
                    }
                    if (empty(
$msg)) { //empty message

                        
$errors[] = "You Must Enter a Message";
                    }
                    if (empty(
$sscode)) { //empty security code
                        
$errors[] = "You Must Verify The Code.";
                    }
                    if (
$sscode != strrev($_SESSION[XXX_SEC_CODE_SEC_XXX])) { //secuurity inputted doesnt equal the session
                        
$errors[] = "Invalid Verification Code";
                    }
                    if (
count($errors) > 0) { //more then 0 errors
                        
print "<div>
                            <h1>" 
count($errors) . " Error" count($errors) .
                            
"(s) Found</h1>
                            <p>"
;
                        foreach (
$errors as $errs) {
                            print 
"$errs.";
                        }
                        print 
"</p>
                            </div>"
//tell them their errors
                    
} else { //no errors
                        
$getflood mysql_query("SELECT * FROM `flood` WHERE `ip` = '$ip';"); //get the flood time and such :O
                        
$arrflood mysql_fetch_array($getflood); //array the info
                        
if (mysql_num_rows($getflood) == 0) { //if its 0
                            
$insert_comment mysql_query("INSERT INTO `comments` (`author`,`content`,`postdate`,`nid`,`ip`) VALUES ('$name','$msg','$postdate','$id','$ip');"); //insert comment
                            
$insert_flood mysql_query("INSERT INTO `flood` (`ip`,`time`) VALUES ('$ip','$time');"); //and the flood time ;)
                            
print "<div>
                            <h1>Comment Added</h1>
                            <p>Your Comment Has Been Added.</p>
                            </div>"
//tell them comment added
                        
} else { //its found
                            
if ($arrflood['time'] < $time) { //if less then time
                                
$insert_comment mysql_query("INSERT INTO `comments` (`author`,`content`,`postdate`,`nid`,`ip`) VALUES ('$name','$msg','$postdate','$id','$ip');"); //it'll insert comment
                                
$update_flood mysql_query("UPDATE `flood` SET `time` = '$ftime' WHERE `ip` = '$ip';"); //update flood time
                                
print "<div>
                            <h1>Comment Added</h1>
                            <p>Your Comment Has Been Added.</p>
                            </div>"
//comment added ^_^
                            
} else { //or error :O
                                
print "<div>
                            <h1>Error</h1>
                            <p>You need to wait 15 more seconds before posting.</p>
                            </div>"
//haha you gotta wait!
                            
//end flood check
                        
//end flood time check again
                    
//end error check
                
//end form posted check
            
//end ID Check for invalid ID
        
//end check if ID is there or not 
        
break; //end our add comment page.
    
case 'categories'//start our different categories pages
        
if (!$id) { //no ID selected for category :O
            
print "<div>
            <h1>Error</h1>
            <p>
            No Category ID Selected.
            </p>
            </div>"
//give em an error
        
} else { //its there
            
$check mysql_query("SELECT * FROM `news_cats` WHERE `id` = '$id';"); //check if in db
            
if (mysql_num_rows($check) == 0) { //ots not
                
print "<div>
            <h1>Error</h1>
            <p>
            Category Not Found.
            </p>
            </div>"
//give error
            
} else { //or it is
                
$gnic mysql_query("SELECT * FROM `news` WHERE `cat_id` = '$id';"); //get news in category
                
if (mysql_num_rows($gnic) == 0) { //if none
                    
print "<div> 
            <h1>Error</h1>
            <p>
            No News In This Category.
            </p>
            </div>"
//give error
                
} else { //or there are some in there
                    
$max_results 5//max results per page
                    
$current = (($page $max_results) - $max_results); //current page
                    
$get_news mysql_query("SELECT * FROM `news` WHERE `cat_id` = '$id' ORDER BY `id` DESC LIMIT $current$max_results"); //get all news with pagination limits
                    
$news_total mysql_query("SELECT * FROM `news` WHERE `cat_id` = '$id';"); //total news in category
                    
$counttotal mysql_num_rows($news_total); //count the total
                    
$total_pages ceil($counttotal $max_results); //total pages
                        
while ($news mysql_fetch_array($get_news)) { //loop to show news
                            
$total_comments mysql_query("SELECT * FROM `comments` WHERE `nid` = '$news[id]';"); //total comments on article
                            
print "<div>
            <h1>" 
stripslashes($news[title]) . "</h1>
            <p>Posted on " 
$news[postdate] . " by <a href=\"members.php?user=$news[author]\">" $news[author] .
                                
"</a></p>
                <p>" 
stripslashes($news[content]) .
                                
"</p>
            <p><a href=\"news.php?x=more&id=
$news[id]\">Comments (" .
                                
mysql_num_rows($total_comments) . ")</a></p>
        </div>"
;
                        } 
//clean up the things and show the post
                        
print "<div>
            <h1>Page Navigation</h1>
            <p>"
//pagination :D
                        
if ($page 1) { //page is gretaer then one
                            
$prev = ($page 1); //set link less then current
                            
print "<a href=\"news.php?x=categories&page=$prev/\">&laquo; Previous</a> "//show link
                        
//end check
                        
for ($i 1$i <= $total_pages$i++) { //make loop of all pages
                            
if ($page == $i) { //if current is same as i
                                
print "<b>" $i "</b> "//dont give link
                            
} else { //or not
                                
print "<a href=\"news.php?x=categories&page=$i/\">" $i "</a> "//give hte poor man his link :)
                            
//end the page check
                        
//end loop
                        
if ($page $total_pages) { //make next link
                            
$next = ($page 1); //current+1
                            
print "<a href=\"news.php?x=categories&page=$next/\">Next &raquo;</a>"//give next link
                        
//end next link giving thing
                        
print "</p>
            </div>"
//end news post and pagination 
                
//end news there check
            
//end ID Check invalid
        
//End ID Check if in DB
        
break; //end categories page
//end switch($_GET['x']){ function
?> <!-- end this file -->


I did not code all of it as of yet as i've been busy with school exams, and other projects which needed to be finished.
Some things you will need to add are:
- Edit Comments
- Delete Comments
I'll code em furthur on in the future but as for now this script should work fine.

Next, call the following file news_admin.php
PHP Code
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
<?php
session_start
();
include(
"config.php");
if(!
$logged['username'] && !$logged['userlevel']){ //check if logged in
print "<b>Error</b>: Your Not Logged In"//they aint
}elseif($logged['username'] && $logged['userlevel'] != '4' || $logged['userlevel'] != '6'){ //check their level
print "<b>Error</b>: You Do Not Have Access To This Feature."//not admin or mod
}else{ //they are.
switch($_GET['page']){ //navigation thing
default: //default page
print "Welcome $logged[username], Would you like to <a href=\"news_admin.php?page=add\">Add</a> Some News?
<table width=\"450\">
<tr>
<td width=\"100\" align=\"center\" valign=\"middle\">
<b>Title</b>
</td>
<td width=\"50\" align=\"center\" valign=\"middle\">
<b>Author</b>
</td>
<td width=\"50\" align=\"center\" valign=\"middle\">
<b>Category</b>
</td>
<td width=\"150\" align=\"center\" valign=\"middle\">
<b>Postdate</b>
</td>
<td width=\"100\" align=\"center\" valign=\"middle\">
<b>Options</b>
</td>
</tr>"
//table headers and welcome
$get_news mysql_query("SELECT * FROM `news` ORDER BY `id` DESC"); //get news
if(mysql_num_rows($get_news) == 0){ //see if theres any news in the database
print "<tr>
<td width=\"450\" align=\"center\" valign=\"middle\">
No News in the Database
</td>
</tr>"
//give an error
}else{ //theres news so show it
while($news mysql_fetch_array($get_news)){ //make loop to show
$get_comments mysql_query("SELECT * FROM `comments` WHERE `nid` = '$news[id]';"); //get all comments
print "<tr>
<td width=\"100\" align=\"center\" valign=\"middle\">
"
.stripslashes($news[title]).", (<a href=\"news.php?x=more&id=$news[id]\">".mysql_num_rows($get_comments)."</a>) Comments
</td>
<td width=\"50\" align=\"center\" valign=\"middle\">
<a href=\"members.php?user=
$news[author]
</td>
<td width=\"50\" align=\"center\" valign=\"middle\">
$news[cat_id]
</td>
<td width=\"150\" align=\"center\" valign=\"middle\">
$news[postdate]
</td>
<td width=\"100\" align=\"center\" valign=\"middle\">
<a href=\"news_admin.php?page=edit&id=
$news[id]\">Edit</a> || <a href=\"news_admin.php?page=delete&id=$news[id]\">Delete</a>
</td>
</tr>"
//display news data
//end loop
//end news check in db and such
print "</table>"//end table
break; //end page
case 'add'//add news page
if(!$_POST['add']){ //check if forms submitted
print "<form method=\"post\">
<b>Title</b>:
<input type=\"text\" name=\"title\" size=\"15\" />
<b>Category</b>:
<select name=\"category\">"
//echo first bit of form
$get_cats mysql_query("SELECFT * FROM `news_cats` ORDER BY `name` ASC"); //get categories
while($cats mysql_fetch_array($get_cats)){ //looooop them
print "<option value=\"$cats[id]\">$cats[title]</option>\r\n"//print the cat names
//end loop
print "</select>
<b>Content</b>:
<textarea cols=\"60\" rows=\"8\" name=\"content\"></textarea>
<input type=\"submit\" name=\"add\" value=\"Add News\" />
</form>"
//the rest of the form with the submit button
}else{ //it was submitted
//clean our variables
$title addslashes($_POST['title']);
$category = (int) $_POST['category'];
$content addslashes($_POST['content']);
$date date("M-D-Y h:i A");
if(empty(
$title) || empty($category) || empty($content)){ //if any are empty
print "<b>Error</b> You Must Fill Out All The Fields."//tell of error
}else{ //none were blank
$insert mysql_query("INSERT INTO `news` (`title`,`author`,`content`,`postdate`,`cat_id`) VALUES ('$title','$logged[username]','$content','$date','$category');"); //insert
if(!mysql_error()){ //no error
print "News Added!"//it was added
}else{ //or not
print mysql_error(); //tell whats wrong
//end error check
//end empty check
//end form check
break; //end add page
case 'edit'//start edit page
$id = (int) $_GET['id']; //our ID Variable
if(!$id){ //check if ID is there
print "<b>Error</b>: No ID Given."//it aint
}else{ //or is it..
$check mysql_query("SELECT * FROM `news` WHERE `id` = '$id';"); //hmmmm lets see
if(mysql_num_rows($check) == 0){ //nope. its invalid
print "<b>Error</b>: Invalid ID Selected."//tell them its invalid
}else{ //its valid
$news mysql_fetch_array($check); //array our news data
if(!$_POST['edit']){ //form not submitted
print "<form method=\"post\">
<b>Title</b>:
<input type=\"text\" name=\"title\" size=\"15\" value=\""
.stripslashes($news['title'])." />
<b>Category</b>:
<select name=\"category\">"
//first bit of form with title and category start
$get_cats mysql_query("SELECT * FROM `news_cats` ORDER BY `name` ASC"); //get our categories
while($cats mysql_fetch_array($get_cats)){ //make loop for cats
if($news['cat_id'] == $cats['id']){ //if news id is same as cat id its selected.
print "<option value=\"$cats[id]\" selected=\"selected\">$cats[name]</option>\r\n"//selected ^^
}else{ //or it isnt
print "<option value=\"$cats[id]\">$cats[name]</option>\r\n"//echo regular
//end checking
//end loop
print "</select>
<b>Content</b>:
<textarea cols=\"60\" rows=\"8\" name=\"content\">"
.stripslashes($news[content])."</textarea>
<input type=\"submit\" name=\"edit\" value=\"Edit News\" />
</form>"
//last ofthe form all data shown
}else{ //form was submitted
//clean variables
$title addslashes($_POST['title']);
$category = (int) $_POST['category'];
$content addslashes($_POST['content']);
if(empty(
$title) || empty($category) || empty($content)){
print 
"<b>Error</b>: You Must Fill Out All Fields."//if any empty give error
}else{
$update mysql_query("UPDATE `news` SET `title` = '$title', `cat_id` = '$category', `content` = '$content' WHERE `id` = '$id';"); //or not update.
if(!mysql_error()){
print 
"News Updated."//no error found
}else{
print 
mysql_error(); //there was an error
}
}
}
}
}
break;
case 
'delete'//delete page
$id = (int) $_GET['id']; //ID Variable
if(!$id){ //no id
print "<b>Error</b>: No ID Selected."//give error
}else{
$check mysql_query("SELECT * FROM `news` WHERE `id` = '$id';"); //check
if(mysql_num_rows($check) == 0){ //not found
print "<b>Error</b>: Invalid ID Selected.";
}else{
$delete_news mysql_query("DELETE FROM `news WHERE `id` = '$id';"); //delete news
$delete_comments mysql_query("DELETE FROM `comments` WHERE `nid` = '$id';"); //delete comments
if(mysql_error()){
print 
mysql_error(); //theres an error
}else{
print 
"News and Comments Deleted!"//theres no error.
}
}
}
break;
//end pages get
//end level check
?>


Thats it for nopw though theres one more tiny code you may want and that is:
PHP Code
1
2
3
4
5
6
7
8
9
10
<?php
session_start
();
include(
"config.php");
$get_news_cats mysql_query("SELECT * FROM `news_cats` ORDER BY `name` ASC");
while (
$news_cats mysql_fetch_array($get_news_cats)) {
    
$news_in_cat mysql_query("SELECT * FROM `news` WHERE `cat_id` = '$news_cats[id]';");
    print 
"<li><a href=\"news.php?x=categories&id$news_cats[id]\">$news_cats[name]</a></li>\n";
}
?>


If you find any errors please tell me and i'll fix them. For a demo you can see it in action on joe.hrwr.org >.>'
ShadowMage
Author:
Views:
5271
Rating:
Posted on Sunday 10th August 2008 at 07:45 AM
Dalez
Dalez
Nvm, i've done that bit that i wanted to do.

But now, theres a bug in my "edit" bit, the drop down menu is in the title.. and i don't know how to fix it.

Also, the delete doesn't work.
Posted on Saturday 9th August 2008 at 07:53 PM
Diablosblizz
Diablosblizz
So before you read the news, you want them to click a link which then brings them to the news?
Posted on Saturday 9th August 2008 at 04:30 PM
Dalez
Dalez
How would i make it so that little code at the bottom just shows the news titles, so that when you click the news title, it goes to the news?

Thanks
Posted on Sunday 15th June 2008 at 07:40 PM
Adam981
Adam981
Updated codes comming soon, ive sent them to Shadow to edit them :)
Posted on Thursday 29th May 2008 at 04:14 PM
jambomb
jambomb
I get a blank page for the news.php
Posted on Saturday 19th April 2008 at 11:49 AM
Dalez
Dalez
How do i add cats?
As i do not know how to.
Posted on Friday 18th April 2008 at 02:17 PM
schorsch
schorsch
Can someone tell me how to smilies that you can install by mausklick smilies in the news involve?
Posted on Friday 11th April 2008 at 03:16 AM
test
test
Parse error: syntax error, unexpected '/' in news.php line 46 fixed that problem just missing a simple / now im getting


Fatal error: Cannot redeclare _codes_() (previously declared config.php:20) in config.php on line 24

on those lines

function _Codes_($num){ //declare the function
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"; //available characters
$code = substr(str_shuffle($chars), 0, $num); //create random string
return $code; //return the code
} //then end the function





Oh And Dalaz remove $logged['userlevel'] != '4' || from the top of the file will resolve any problems just makes it only site admin can access the acp function
Posted on Thursday 10th April 2008 at 05:40 PM
Dalez
Dalez
atthelibrary, how did you fix it?
Cuz myne is doing that too ;l
Posted on Monday 24th March 2008 at 12:44 PM
Dalez
Dalez
How do i add cats?