php gurus please help

php gurus please help

Re: php gurus please help Posted by gimpinthesink on Mon Feb 23rd 2004 at 9:03pm
gimpinthesink
662 posts
Posted 2004-02-23 9:03pm
662 posts 176 snarkmarks Registered: Apr 21st 2002 Occupation: student Location: Forest Town, Notts
I have decided to invest in a book that teaches PHP, MYSQL and Apache and I am curently working through it but I have come across a pice that don't work properly and I cannot fix itso I was woundering if you could help me

This is the pice of code that I carnt get to work

<!--code-->Code:<TABLE width="95%" align=center><TR><TD>//cerate the display string
$display_block = "
<table cellpadding=3 cellspacing=3 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 = stripslashes($topic_info['topic_title']);
$topic_create_time = $topic_info['fmt_topic_create_time'];
$topic_owner = stripslashes($topic_info['topic_owner']);

//get number of posts
$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)');

//add to display
$display .= "
<tr>
<td><a href"showtopic.php?topic_id=$topic_id">
$topic_title</a>

Created on $topic_create_time by $topic_owner</td>
<td align=center>$num_posts</td>
</tr>";
}

//close up the table
$display_block .= "</table>";
</TD></TR></TABLE><!--/code-->

It should show up a topic with how many posts are in it but it only brings up the TOPIC TITLE and # of POSTS bit which are the titles for the coloms.
Re: php gurus please help Posted by Monqui on Mon Feb 23rd 2004 at 10:17pm
Monqui
743 posts
Posted 2004-02-23 10:17pm
Monqui
member
743 posts 94 snarkmarks Registered: Sep 20th 2002 Occupation: Poor College Student Location: Iowa, USA
Do you actually have the DB set up to use this code?

It also looks like $get_topics_res isn't initialized anywhere here...

I would think that would kick out an error though...

But it seems like nothing within the while is being processed, since the conditional is never true...
Re: php gurus please help Posted by Leperous on Mon Feb 23rd 2004 at 11:04pm
Leperous
3382 posts
Posted 2004-02-23 11:04pm
Leperous
Creator of SnarkPit!
member
3382 posts 1635 snarkmarks Registered: Aug 21st 2001 Occupation: Lazy student Location: UK
Yes, like Monqui said, you need to define $get_topics_res , through a mysql_query() function. I'm guessing such a line would be:

$get_topic_res = mysql_query("SELECT topic_id,topic_title,fmt_topic_create_time,topic_owner FROM topics WHERE forum_id = '$forum' LIMIT $start,$limit");

Where $start is related to which page number you're looking at, and $limit is the number of topics/page you want to display. Otherwise it looks fine, although a much neater way would be to write:

<!--code-->Code:
<TABLE width="95%" align=center>

<TR>
<TD>$display = '
<table cellpadding=3 cellspacing=3 border=1>
<tr>
<th>TOPIC TITLE</th>
<th># of POSTS</th>
</tr>';

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

$num_posts = mysql_result(mysql_query("select count(post_id) from forum_posts where topic_id = $topic_info[topic_id]"),0);
if(!$num_posts) $num_posts = '0';

$display .= '
<tr>
<td>.']
'.stripslashes($topic_info['topic_title']).'


Created on '.$topic_info['fmt_topic_create_time'].' by '.stripslashes($topic_info['topic_owner']).'</td>
<td align=center>'.$num_posts.'</td>
</tr>';
}

$display .= '</table>';


</TD></TR></TABLE><!--/code-->
Re: php gurus please help Posted by gimpinthesink on Mon Feb 23rd 2004 at 11:24pm
gimpinthesink
662 posts
Posted 2004-02-23 11:24pm
662 posts 176 snarkmarks Registered: Apr 21st 2002 Occupation: student Location: Forest Town, Notts
the $get_topics_res is defined to

<!--code-->Code:<TABLE width="95%" align=center><TR><TD>$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) {
</TD></TR></TABLE><!--/code-->

a coupple of lines above the last pics of code and yeh the db is set up to use it.
Re: php gurus please help Posted by Leperous on Mon Feb 23rd 2004 at 11:28pm
Leperous
3382 posts
Posted 2004-02-23 11:28pm
Leperous
Creator of SnarkPit!
member
3382 posts 1635 snarkmarks Registered: Aug 21st 2001 Occupation: Lazy student Location: UK
Should be a > sign there, I think, otherwise more poor but working code :argh: Again, I'd personally leave that last line out (mysql_num_rows), it's a bit pointless. Also make sure you have some topics in your database..!

These forums simply work off the following code (the last reply time is stored in the topic table, making it possible to sort the topics by when it was last replied to, along with the number of replies to that topic so you don't need to manually count how many replies there are each time!):

$tpp = 20; //topics per page
if(!$page) $start = 0; else $start = ($page-1)*$tpp;

$sql = mysql_query("SELECT * FROM topics WHERE forum_id = '$forum' ORDER BY topic_time DESC LIMIT $start, $tpp");
while($array = mysql_fetch_array($sql)) {

(blah blah blah)

}
Re: php gurus please help Posted by Monqui on Mon Feb 23rd 2004 at 11:37pm
Monqui
743 posts
Posted 2004-02-23 11:37pm
Monqui
member
743 posts 94 snarkmarks Registered: Sep 20th 2002 Occupation: Poor College Student Location: Iowa, USA
To be honest, it's a bit hard to find out what's wrong now since we don't have the entire script...

Now, another question- is there actually an entry in the DB for this?

If nothing is there, obviously nothing can get spat out at you...

Don't be offended if these sound condescending, I'm just trying to think of little things that can go wrong...
Re: php gurus please help Posted by gimpinthesink on Mon Feb 23rd 2004 at 11:55pm
gimpinthesink
662 posts
Posted 2004-02-23 11:55pm
662 posts 176 snarkmarks Registered: Apr 21st 2002 Occupation: student Location: Forest Town, Notts
I thourght that you may need to see the whole script to find out whats going wrong.

I have put a entery in to the db I did that with a script that i wrote and then checked that it was there in phpmyadmin.

Heres the entire script

<!--code-->Code:<TABLE width="95%" align=center><TR><TD><?php
//connect to the sever and select databace
$conn = mysql_connect("localhost", "username", "password")
or die(mysql_error());
mysql_select_db("database",$conn) or die(mysql_error());

//gather the topics
$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) {
//there are no topics, so say so
$display_block = "No topics exist.

";
} else {
//cerate the display string
$display_block = "
<table cellpadding=3 cellspacing=3 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 = stripslashes($topic_info['topic_title']);
$topic_create_time = $topic_info['fmt_topic_create_time'];
$topic_owner = stripslashes($topic_info['topic_owner']);

//get number of posts
$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)');

//add to display
$display .= "
<tr>
<td><a href"showtopic.php?topic_id=$topic_id">
$topic_title</a>

Created on $topic_create_time by $topic_owner</td>
<td align=center>$num_posts</td>
</tr>";
}

//close up the table
$display_block .= "</table>";
}
?>
<html>
<head>
<title>Meryleliza Crafts.com</title>
</head>
<body>
<h1>Topics in My Forum</h1>
<?php echo $display_block; ?>
Would you like to add a topic?

</body>
</html>
</TD></TR></TABLE><!--/code-->
Re: php gurus please help Posted by scary_jeff on Mon Feb 23rd 2004 at 11:55pm
scary_jeff
1614 posts
Posted 2004-02-23 11:55pm
1614 posts 191 snarkmarks Registered: Aug 22nd 2001
The best thing you can do to find out why your code isn't working is to get it to echo variables as it goes along, then see where something isn't waht it should be. I suggest echoing the exact SQL it is using, then trying that SQL in PHPMyAdmin, to see if you get the correct results there.

[edit] you may want to use some indentation system in your code to make it easier to follow [/edit]
Re: php gurus please help Posted by Leperous on Mon Feb 23rd 2004 at 11:57pm
Leperous
3382 posts
Posted 2004-02-23 11:57pm
Leperous
Creator of SnarkPit!
member
3382 posts 1635 snarkmarks Registered: Aug 21st 2001 Occupation: Lazy student Location: UK
Like I said, get rid of the line containing mysql_num_rows (along with the corresponding, closing } at the end) and it should work. Or at least change it to greater than, >, because right now it's only doing that loop if you don't have any topics to display...
Re: php gurus please help Posted by Hornpipe2 on Tue Feb 24th 2004 at 1:11am
Hornpipe2
636 posts
Posted 2004-02-24 1:11am
636 posts 123 snarkmarks Registered: Sep 7th 2003 Occupation: Programmer Location: Conway, AR, USA
Edit php.ini and enable the little flag that shows errors in the webpage instead of just a blank page. I don't remember what it's called now, but it's in there somewhere.