message board problem

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

Post Reply
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

message board problem

Post by kristie380 »

Ok I know I'm missing something small in here but I just can't figure out what it is. I am trying to dispaly the topics posted in my message board but I'm not getting anything. Here is my php code:

Code: Select all

<? php
$conn = mysql_connect("mysql", "*****", "*****") or die(mysql_error());
mysql_select_db("alumni",$conn) or die(mysql_error());

$get_topics = "SELECT `topic_id`, `topic_title`, date_format(topic_create_time, '%b %e %Y at %r') as fmt_topic_create_time, `topic_owner` FROM `forum_topics` ORDER BY `topic_create_time` DESC";

$get_topics_res = mysql_query($get_topics,$conn) or die(mysql_error());

if (mysql_num_rows($get_topics_res) < 1) {
$display_block = "<table width=\"790px\"><tr><td align=\"center\"><p><font size=\"4\">No topics exist</font></p></td></tr></table>";

} else {

$display_block = "<table width=\"790px\" cellpadding=\"3\" cellspacing=\"1\"><tr><td width=\"80%\"><p><font size=\"4\">TOPIC TITLE</td><td width=\"20%\"># OF POSTS</td></tr>";

while ($topic_info = mysql_fetch_array($get_topics_res)) {

$topic_id = $topic_info['topic_id'];
$topic_title = stripslashes($topic_info['topic_title']);
$topic_create_time = $topic_info['fmt_topic_create_time'];
$topic_owner = stripslashes($topic_info['topic_owner']);

$get_num_posts = "SELECT count(post_id) FROM `forum_posts` WHERE `topic_id` = $topic_id";
$get_num_posts_res = mysql_query($get_num_posts,$conn) or die(mysql_error());
$num_posts = mysql_result($get_num_posts_res,0,'count(post_id)');

$display_block .= "<tr><td width=\"80%\"><a href=\"showtopic.php?topic_id=$topic_id\"><b>$topic_title</b></a><br>Created on $topic_create_time by $topic_owner</td><td width=\"20%\" align=\"center\">$num_posts</td></tr>";
}
$display_block .= "</table>";
}

?>
<html>
<head>
        
 <title>Untitled</title>

</head>
<body text="#006633">

<?php echo "$display_block"; ?>

<p><a href="addtopic.htm">Add A Topic</a></p>
</body>
</html>

Can someone see what I'm missing?



Hawleyjr Edit: Username/Password removed.
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

I think you need to echo stuff as you loop through it. The db results are only being assigned to variables.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

ambivalent, he echo's his $display_block variable at the end, there.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

No, he is echoing it.

Remove the space between < and ?php (this should be causing the script to echo your source). Give us more information on what the script outputs too.
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

Ok I re-organized it like this and it is still not working:

Code: Select all

<? php
$conn = mysql_connect("mysql", "*******", "*******") or die(mysql_error());
mysql_select_db("alumni",$conn) or die(mysql_error());

$get_topics = "SELECT `topic_id`, `topic_title`, `date_format(topic_create_time, '%b %e %Y at %r') as fmt_topic_create_time`, `topic_owner` FROM `forum_topics` ORDER BY `topic_create_time` DESC";

$get_topics_res = mysql_query($get_topics,$conn) or die(mysql_error());

while ($topic_info = mysql_fetch_array($get_topics_res)) {

$topic_id = $topic_info['topic_id'];
$topic_title = stripslashes($topic_info['topic_title']);
$topic_create_time = $topic_info['fmt_topic_create_time'];
$topic_owner = stripslashes($topic_info['topic_owner']);

$get_num_posts = "SELECT `count(post_id)` FROM `forum_posts` WHERE `topic_id` = $topic_id";
$get_num_posts_res = mysql_query($get_num_posts,$conn) or die(mysql_error());
$num_posts = mysql_result($get_num_posts_res,0,'count(post_id)');

if (mysql_num_rows($get_topics_res) < 1) {
echo "<table width=\"790px\"><tr><td align=\"center\"><p><font size=\"4\">No topics exist</font></p></td></tr></table>";

} else {

echo "<table width=\"790px\" cellpadding=\"3\" cellspacing=\"1\"><tr><td width=\"80%\"><p><font size=\"4\">TOPIC TITLE</td><td width=\"20%\"># OF POSTS</td></tr>";

echo "<tr><td width=\"80%\"><a href=\"showtopic.php?topic_id=$topic_id\"><b>$topic_title</b></a><br>Created on $topic_create_time by $topic_owner</td><td width=\"20%\" align=\"center\">$num_posts</td></tr>";
}
echo "</table>";
}

?>
oh and by the way...I'm a she :)


Username & Password removed...
ody
Forum Contributor
Posts: 147
Joined: Sat Mar 27, 2004 4:42 am
Location: ManchesterUK

Post by ody »

try changing:

while ($topic_info = mysql_fetch_array($get_topics_res))

to use mysql_fetch_assoc as you are accessing $topic_info in an associative mannner.
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

still nothing - i'm just getting no output at all...a blank screen
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Sorry, should have read your sn.

You still haven't fixed the space between < and ?php

Code: Select all

<?php
$conn = mysql_connect("mysql", "****", "****") or die(mysql_error());
mysql_select_db("alumni",$conn) or die(mysql_error());
Edit - Ack, and stop posting your user/pass!
Last edited by Ambush Commander on Mon Nov 14, 2005 2:59 pm, edited 1 time in total.
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Nathaniel wrote:ambivalent, he echo's his $display_block variable at the end, there.
woops....missed that
ody
Forum Contributor
Posts: 147
Joined: Sat Mar 27, 2004 4:42 am
Location: ManchesterUK

Post by ody »

You have back ticks around your entire column definition, you should only have the ticks around the column name `fmt_topic_create_time`.

copy and paste this to see if it works.

$get_topics = "SELECT `topic_id`, `topic_title`, date_format(topic_create_time, '%b %e %Y at %r') as `fmt_topic_create_time`, `topic_owner` FROM `forum_topics` ORDER BY `topic_create_time` DESC";
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

problem solved...thanks!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Now, if you had just posted that there was an SQL error in your script... :roll:
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

The odd thing is, he should have gotten the error output with his die(mysql_error()) call, instead of a blank screen.
Post Reply