Perad Posted October 25, 2006 Share Posted October 25, 2006 I found this script a while back and have used a modified version of this is used across my new site. The problem i am having is that i need to add another layer to it and don't understand how the switch statement that controls it works. Here's the script[code]<?php/* user config variables */$max_items = 5; /* max number of news items to show *//* make database connection */$db = mysql_connect ('localhost','root','Berskin99');mysql_select_db ('test',$db);function displayNews($all = 0) { /* bring in two variables * $db is our database connection * $max_items is the maximum number * of news items we want to display */ global $db, $max_items; /* query for news items */ if ($all == 0) { /* this query is for up to $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 { /* this query will get all news */ $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)) { /* display news in a simple table */ echo "<TABLE border=\"1\" width=\"300\">\n"; /* place table row data in * easier to use variables. * Here we also make sure no * HTML tags, other than the * ones we want are displayed */ $date = $row['date']; $title = htmlentities ($row['title']); $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>')); /* display the data */ echo "<TR><TD><b>$title</b> posted on $date</TD></TR>\n"; echo "<TR><TD>$news</TD></TR>\n"; /* get number of comments */ $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); /* display number of comments with link */ echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id={$row['id']}\">Comments</a>" . "($comment_row[0]}</TD></TR>\n"; /* finish up table*/ echo "</TABLE>\n"; echo "<BR>\n"; } /* if we aren't displaying all news, * then give a link to do so */ if ($all == 0) { echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">View all news</a>\n"; }}function displayOneItem($id) { global $db; /* query for item */ $query = "SELECT * FROM news WHERE id=$id"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad news id\n"; return; } $row = mysql_fetch_assoc($result); echo "<TABLE border=\"1\" width=\"300\">\n"; /* easier to read variables and * striping out tags */ $title = htmlentities ($row['title']); $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>')); /* display the items */ echo "<TR><TD><b>$title</b></TD></TR>\n"; echo "<TR><TD>$news</TD></TR>\n"; echo "</TABLE>\n"; echo "<BR>\n"; /* now show the comments */ displayComments($id);}function displayComments($id) { /* bring db connection variable into scope */ global $db; /* query for comments */ $query = "SELECT * FROM news_comments WHERE news_id=$id"; $result = mysql_query ($query); echo "Comments:<BR><HR width=\"300\">\n"; /* display the all the comments */ 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"; } /* add a form where users can enter new comments */ 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; /* insert the comment */ $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";}/* this is where the script decides what do do */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";?> [/code]I have written what i want to add which you can see below. Each news article has a category. I want the user to be able to select a category which will lead them onto the news in that category. How would i modify the switch statement to allow this?[code]<? function displayArticles() { global $dbc, $max_items, $username; $query = "SELECT * FROM site_categories"; $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { /* display categories in a simple table */ echo "<TABLE border=\"1\" width=\"580\">\n"; $title = htmlentities ($row['cat_name']); echo "<TR><TD width=\"300\"><b>$title</b></TD>"; /* get number of articles */ $comment_query = "SELECT count(*) FROM downloads WHERE cat_id={$row['cat_id']}"; $comment_result = mysql_query ($comment_query); $comment_row = mysql_fetch_row($comment_result); /* display number of articls with link */ echo "<TD><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&cat_id={$row['cat_id']}\">Articles</a>" . "($comment_row[0])</TD></TR>\n"; /* finish up table*/ echo "</TABLE>\n"; echo "<BR>\n"; } }?>[/code] Link to comment https://forums.phpfreaks.com/topic/25079-adding-another-layer-to-this-script/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.