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
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 12:56 am
Ok, so I have this code that will display the forum topics and then when a user clicks the topic it will take them to a new page and display the topic but when it does this it displays all the topics (5 in the SQL database). I cant figure out why it is display all five topics at once. It should only display the topic that the user clicked.
Link to the page (cuss words will display (f word) was mad that It was not working so I inputted cuss words into database
so dont click if you dont want to see the f word as topic headings lol)
http://matthew-ketcham.com/militia/ceoDisplayTopics.php
Just click a link and you will see what I mean.
Now here is the code put in seperate replays so its not one giant thing:
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 12:57 am
This displays the forum topics:
Code: Select all
<?php
include("dbConnect.php");
dbConnect();
$get_topics_sql = mysql_query("SELECT topic_id, topic_title, topic_owner FROM ceoForum_topics");
$get_topics_res = mysql_query("SELECT * FROM ceoForum_topics");
if(mysql_num_rows($get_topics_sql) < 1)
{
echo "Nope";
}else {
$display_block = "
<table cellpadding=\"3\" cellspacing=\"1\" border=\"1\">
<tr>
<th>TOPIC TITLE</th>
<th># of POSTS</th>
</tr>";
while($topic_info = mysql_fetch_array($get_topics_res))
{
$topic_id = $topic_info['topic_id'];
$topic_title = $topic_info['topic_title'];
$topic_owner = $topic_info['topic_owner'];
$get_num_posts_sql = mysql_query("SELECT COUNT(post_id) AS post_count FROM ceoForum_posts WHERE topic_id = '".$topic_id."'");
$get_num_posts_res = mysql_query($mysql, $get_num_posts_sql);
while($posts_info = mysql_fetch_array($get_num_posts_res))
{
$num_posts = $posts_info['post_count'];
}
$display_block .="
<tr>
<td><a href=\"ceoShowTopic.php?topic_id=".$topic_id."\"><strong>".$topic_title."</strong></a></br>Created by ".$topic_owner."</td><td
align=center>".$num_posts."</td>
</tr>";
}
mysql_free_result($get_topics_res);
mysql_free_result($get_num_posts_res);
mysql_close($mysql);
$display_block .= "</table>";
}
?>
<html><head><title></title></head>
<body>
<?php
echo $display_block;
?>
</body>
</html>
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 12:58 am
This displays the topic body, the whole post:
Code: Select all
<?php
include("dbConnect.php");
dbConnect();
if(!isset($_GET["topic_id"]))
{
header("Location: ceoDisplayTopics.php");
exit;
}
$verify_topic_sql = mysql_query("SELECT topic_title FROM ceoForum_topics WHERE topic_id = '".$_GET["topic_id"]."'");
$verify_topic_res = mysql_query("SELECT * FROM ceoForum_topics");
if(mysql_num_rows($verify_topic_res) < 1)
{
$display_block = "<p><em>Invalid topic:<br>
Please <a href=\"ceoDisplayTopics.php\">try again</a>.</em></p>";
}else {
while($topic_info = mysql_fetch_array($verify_topic_res))
{
$topic_title = $topic_info['topic_title'];
}
$get_posts_sql = mysql_query("SELECT post_id, post_text, post_owner FROM ceoForum_posts WHERE topic_id = '".$_GET["topic_id"]."'");
$get_posts_res = mysql_query("SELECT * FROM ceoForum_posts");
$display_block .="
<p>Showing posts for the <strong>".$topic_title."</strong> topic:</p>
<table width=\"100\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\">
<tr>
<th>AUTHOR</th>
<th>POST</th>
</tr>";
while($post_info = mysql_fetch_array($get_posts_res))
{
$post_id = $post_info['post_id'];
$post_text = $post_info['post_text'];
$posts_owner = $post_info['post_owner'];
$display_block .= "
<tr>
<td width=\"35%\" valign=\"top\">".$post_owner."<br></td>
<td width=\"65%\" valign=\"top\">".$post_text."<br><br>
<a href=\replytopost.php?post_id=".$post_id."\">
<strong>Reply to post?</strong></a></td>
</tr>";
}
$display_block .= "</table>";
}
?>
<html><head>
<title></title>
</head>
<body>
<?php
echo $display_block;
?>
</body>
</html>
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 12:58 am
I've been banging my head
on this for the past few hours cant find the error in it, thanks for any help
pcoder
Forum Contributor
Posts: 230 Joined: Fri Nov 03, 2006 5:19 am
Post
by pcoder » Wed Oct 15, 2008 1:35 am
Check it:
Code: Select all
$get_posts_res = mysql_query("SELECT * FROM ceoForum_posts");
You didn't append the topic id here.
The $get_posts_sql is correct, but you have not used it anywhere.???
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 1:50 am
pcoder wrote: Check it:
Code: Select all
$get_posts_res = mysql_query("SELECT * FROM ceoForum_posts");
You didn't append the topic id here.
The $get_posts_sql is correct, but you have not used it anywhere.???
would it be something like this then?
Code: Select all
$get_posts_res = mysql_query($mysql, $get_posts_sql);
If so I tried that and it did not work. Well when I do that only the html code shows and nothing from the database.
pcoder
Forum Contributor
Posts: 230 Joined: Fri Nov 03, 2006 5:19 am
Post
by pcoder » Wed Oct 15, 2008 2:00 am
I think your $post_info should be:
Code: Select all
while($post_info = mysql_fetch_array($get_posts_sql))
{
//and the rest of the code
}
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 2:22 am
pcoder wrote: I think your $post_info should be:
Code: Select all
while($post_info = mysql_fetch_array($get_posts_sql))
{
//and the rest of the code
}
rest of what code? its not clicking
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 2:27 am
oh think i get what you are saying, i hope its not that small little mistake going to try it, just got back from gym, heads really not thinking now lol
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 2:32 am
Nope that did not work, I changed it to
Code: Select all
$get_posts_sql = mysql_query("SELECT post_id, post_text, post_owner FROM ceoForum_posts WHERE topic_id = '".$_GET["topic_id"]."'");
$get_posts_res = mysql_query("SELECT * FROM ceoForum_posts");
//mysqli_query($mysqli, $get_posts_sql);
$display_block .="
<p>Showing posts for the <strong>".$topic_title."</strong> topic:</p>
<table width=\"100\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\">
<tr>
<th>AUTHOR</th>
<th>POST</th>
</tr>";
while($post_info = mysql_fetch_array($get_posts_sql))
smog890
Forum Newbie
Posts: 9 Joined: Wed Oct 15, 2008 12:52 am
Post
by smog890 » Wed Oct 15, 2008 2:41 am
wait its working 80% now, just got to figure out one last bug, think i can make it work, thanks for help.