how to lock a topic in a forum?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to lock a topic in a forum?

Post by Celauran »

SELECT the sticky column, ORDER BY sticky DESC. As you're iterating over the returned rows, if $row['sticky'] == 1, then label it as sticky.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: how to lock a topic in a forum?

Post by beginner123 »

oh I think I understand now. So I'm just putting all the topics in order so if one topic has sticky = 1 then it will always stay at the top?
but what about locking the forum to stop people posting in it?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to lock a topic in a forum?

Post by Celauran »

You'd use basically the same logic for displaying whether a topic is locked or not. In the thread view, just remove the reply button if locked = 1
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: how to lock a topic in a forum?

Post by beginner123 »

thanks for all the help. sorry it took so long to explain :lol:
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: how to lock a topic in a forum?

Post by beginner123 »

I changed the value of sticky in a category to 1 but when I put in ORDER BY sticky DESC I just get errrors
heres the home page where I want to put the code:

Code: Select all

<?php
//index.php
include 'connect.php';
include 'header.php';

$sql = "SELECT
		categories.categoryID,
		categories.categoryName,
		categories.categoryDescription,
		COUNT(topics.topicID) AS topics
	FROM
		categories
              //I tried putting ORDER BY sticky here
	LEFT JOIN
		topics
	ON
		topics.topicID = categories.categoryID
	GROUP BY
		categories.categoryName, categories.categoryDescription, categories.categoryID";
		

$result = mysql_query($sql);

if(!$result)
{
	echo 'The categories could not be displayed, please try again later.';
}

else
{
	if(mysql_num_rows($result) == 0)
	{
		//no categories have been made yet
		echo 'No categories defined yet.';
	}
	else
	{
		//make the table
		echo '<table border="1">
			  <tr>
				<th>Category</th>
				<th>Last topic</th>
			  </tr>';	
			
		while($row = mysql_fetch_assoc($result))
		{				
			echo '<tr>';
			echo '<td class="leftpart">';
			 echo '<h3><a href="category.php?id=' . $row['categoryID'] . '">' . $row['categoryName'] . '</a></h3>' . $row['categoryDescription'];
			echo '</td>';
			echo '<td class="rightpart">';
				
				//fetch last topic for each category
					$topicsql = "SELECT
								topicID,
								topicSubject,
								topicDate,
								topicCategory
							FROM
								topics
							WHERE
								topicCategory = " . $row['categoryID'] . "
						                ORDER BY
								topicDate
							DESC
							LIMIT
								1";
								
					$topicsresult = mysql_query($topicsql);
				
					if(!$topicsresult)
					{
					//error
						echo 'Last topic could not be displayed.';
					}
					else
					{
					if(mysql_num_rows($topicsresult) == 0)
					{
						echo 'no topics';
					}
					else
					{
					while($topicrow = mysql_fetch_assoc($topicsresult))
					echo '<a href="topic.php?id=' . $topicrow['topicID'] . '">' . $topicrow['topicSubject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topicDate']));
					}
					}
			echo '</td>';
			echo '</tr>';
		}
	}
}

include 'footer.php';
?>

if i add the code it skips to

Code: Select all

if(!$result)
{
	echo 'The categories could not be displayed, please try again later.';
}
and doesn't display the categories
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to lock a topic in a forum?

Post by Celauran »

ORDER BY must come after GROUP BY
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: how to lock a topic in a forum?

Post by beginner123 »

thanks :D
Post Reply