Jump to content

Tutorial System


Wolphie

Recommended Posts

Ok, so i'm writing a tutorial system with comments etc...

But i'm having trouble getting the comments to work, basically what i want to do is:

 

Someone selects a tutorial, and within that same tutorial page it will list the comments assigned to the tutorials ID.

The comment form will be displayed beneath the comments them selves and when a comment is submitted i want

that to go back to the tutorial page which the comment was added to.

 

This is what i have so far:

 

<?php

$host	= 'localhost';
$user	= 'root';
$pass	= '';
$db		= '';

$con		= mysql_connect($host, $user, $pass);

if($con)
{
mysql_select_db($db);
}
else
{
die('Error: ' . mysql_error());
}


$id = $_REQUEST['id'];

if(isset($id))
{
$view_tut 	= 	mysql_query(sprintf("SELECT * FROM `tutorial` WHERE `ID` = '%s'", $id))
						or die('Error: ' . mysql_error());

while($row = mysql_fetch_array($view_tut))
{
	echo '
				<table cellpadding="0" cellspacing="0">
					<tr>	
						<td><a href="view.php?id=' . $id . '">' . $row['title'] . '</a><span style="padding-left: 15px; font-size: 10px;">- ' . $row['author'] . '</span></td>
					</tr>
					<tr>
						<td style="font-size: 10px; font-style: italic;">Posted on: ' . $row['date'] . '</td>
					</tr>
					<tr>
						<td style="padding-top: 10px;">' . $row['content'] . '</td>
					</tr>
				</table>
			';
}

$comments	=	mysql_query(sprintf("SELECT * FROM `comments` WHERE `tut_id` = '%s' ORDER BY `ID` DESC LIMIT 20", $id))
						or die('Error: ' . mysql_error());	

	echo '
				<table cellpadding="0" cellspacing="0" style="margin-top: 50px;" width="200">
					<tr>
						<td style="font-size: 20px; font-style: bold;"><u>Comments</u></td>
					</tr>
				</table>
			';

while($row = mysql_fetch_array($comments))
{		
	echo '
				<table cellpadding="0" cellspacing="0" width="200">
					<tr>
						<td style="padding-top: 15px;">' . $row['title'] . '<span style="padding-left: 15px; font-size: 10px;">- ' . $row['author'] . '</span></td>
					</tr>
					<tr>
						<td style="font-size: 10px; font-style: italic;">Posted on: ' . $row['date'] . '</td>
					</tr>
					<tr>
						<td style="padding-top: 10px;  padding-bottom: 5px; border-bottom: 1px solid #CCCCCC;" width="100%">' . $row['content'] . '</td>
					</tr>
				</table>
			';
}


switch($_GET['do'])
{
	default:
		echo '
					<form action="?do=addcomment" method="post">
					<table cellpadding="0" cellspacing="0" style="margin-top: 50px;" width="200">
						<tr>
							<td style="font-size: 20px; font-style: bold;">Add Comment</td>
						</tr>
						<tr>
							<td style="padding-top: 15px;">Title:</td>
						</tr>
						<tr>
							<td><input type="text" name="title" /></td>
						</tr>
						<tr>
							<td>Author:</td>
						</tr>
						<tr>
							<td><input type="text" name="author" /></td>
						</tr>
						<tr>
							<td>Content:</td>
						</tr>
						<tr>
							<td><textarea cols="50" rows="10" name="content"></textarea></td>
						</tr>
						<tr>
							<td><input type="submit" value="Add" /></td>
						</tr>
					</table>
					</form>
				';
		break;

	case 'addcomment':

		$title			=	mysql_escape_string(htmlspecialchars(stripslashes($_POST['title'])));
		$author		=	mysql_escape_string(htmlspecialchars(stripslashes($_POST['author'])));
		$date 			= 	mysql_escape_string(date('m-d-Y'));
		$content		= 	mysql_escape_string(nl2br(htmlspecialchars(stripslashes($_POST['content']))));
		$tut_id			= mysql_escape_string($id);

		mysql_query(sprintf("INSERT INTO `comments` ( `title`, `author`, `date`, `content`, `tut_id` ) 
		VALUES ( '%s', '%s', '%s', '%s', '%d' )", $title, $author, $date, $content, $tut_id)) 
		or die('Error: ' . mysql_error());

		echo '<head><script type="text/javascript">window.location = "view.php?id=' . $id . '";</script></head>';

		break;
}				
}
else
{
$view		= 	mysql_query(sprintf("SELECT * FROM `tutorial` ORDER BY `ID` DESC"))
					or die('Error: ' . mysql_error());

while($row = mysql_fetch_array($view))
{
	echo '
				<table cellpadding="0" cellspacing="0">
					<tr>
						<td style="padding-top: 15px;"><a href="view.php?id=' . $row['ID'] . '">' . $row['title'] . '</a><span style="padding-left: 15px; font-size: 10px;">- ' . $row['author'] . '</span></td>
					</tr>
					<tr>
						<td style="padding-top: 10px;">' . $row['description'] . '</td>
					</tr>
				</table>
			';
}
}



?>




Link to comment
https://forums.phpfreaks.com/topic/78692-tutorial-system/
Share on other sites

Once the comment has successfully been posted into the database, (you should already have the tutorial id), use the id to display the appropriate page. This can be done by including the tutorial page used to display it, or by redirecting using 'header()', http://uk3.php.net/manual/en/function.header.php

Link to comment
https://forums.phpfreaks.com/topic/78692-tutorial-system/#findComment-398343
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.