Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I have created a page that executes 3 very similar queries in order to retrieve the threads (in a discussion board) that belong to a certain topic.

In my database, I have a column called threadTopic that specifies which topic the thread in the table belongs to. So when I for example, SELECT * FROM threads WHERE threadTopic=generalDiscussion, it will only retrieve the threads related to the generalDiscussion topic; which can then be displayed in their own seperate table.

However, I have to do this process every time for every threadTopic option (there will be 5 altogether).

My question isnt looking for correction on any code I have done as it works fine, but more a question of best practice as the way I have done it feels messy, inefficient, time-consuming and cheap.

Is there a technique where I can use a foreach to get each threadTopic and then display all the threads in the Topic so that I will end with 5 tables from one query?

This is the query I am repeating, with a different SQL query to match each threadTopic:

PHP
$query = 'SELECT * FROM threads WHERE threadTopic="general"';
				$output = $db->query($query);
				$results = $output->fetchAll(PDO::FETCH_ASSOC);
				if (count($results) > 0) {
					echo "<table>\n";
					echo "<tr>
					<th>General Discussion</th>
					<th>Replies</th>
					<th>Last activity</th></tr>\n";
					foreach($results as $result){
					//example replies amount generator
                                        $rand = rand(000, 999);
						echo "<tr>\n";
						echo "<td>$result[threadName]</td>";
						echo "<td>$rand</td>";
						echo "<td >$result[lastActive]<br>By: <a href=\"#\">$result[threadBy]</a></td>";
						}
						echo "</tr>\n";
						echo "</table>";
					}
Posted

1 solution

Put your code into a function with an argument called say $topic

PHP
function getThread($topic){

// your sql and echo code here

}
// call the function and pass the topic as argument
$topic = "general";
getThread($topic);

Read more: PHP Function[^]
However, you should use parameterized query instead of concatenating parameters in sql to prevent sql injection. Refer: How can I prevent SQL-injection in PHP?[^]
 
Share this answer
 
v2
Comments
jba1991 16-Mar-15 10:40am    
Thanks I'll definately try this. I've used functions before but they always slip my mind.
jba1991 16-Mar-15 15:51pm    
This is what I currently have now after creating a function. However, I am getting an error of an undefined index for the lines $name, $last and $by.



$query = 'SELECT * FROM threads WHERE threadTopic=:topic';
$output = $db->prepare($query);
$results = $output->execute(array(':topic' => $topic));
if (count($results) > 0) {
echo "<table>\n";
echo "<tr><th>$topic</th><th>Replies</th><th>Last activity</th></tr>\n";
if ($results) {
$row = $output -> fetchAll();
$name = $row['threadName'];
$last = $row['lastActive'];
$by = $row['threadBy'];
$rand = rand(000, 999);
echo "<tr>\n";
echo "<td>$name</td>";
echo "<td class=\"text-center\">$rand</td>";
echo "<td class=\"text-center\">$last<br>By: $by</td>";
}
echo "</tr>\n";
echo "</table>";
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900