php gurus please help
Post Reply
Quote
Re: php gurus please help
Posted by gimpinthesink on Mon Feb 23rd at 9:03pm 2004


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:
//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\">
<strong>$topic_title</strong></a><br>
Created on $topic_create_time by $topic_owner</td>
<td align=center>$num_posts</td>
</tr>";
}

//close up the table
$display_block .= "</table>";

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.

[addsig]




Quote
Re: php gurus please help
Posted by Monqui on Mon Feb 23rd at 10:17pm 2004


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... [addsig]




Quote
Re: php gurus please help
Posted by Leperous on Mon Feb 23rd at 11:04pm 2004


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:
$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><a href="showtopic.php?topic_id='.$topic_info['topic_id'].'">
<b>'.stripslashes($topic_info['topic_title']).'</b></a><br>
Created on '.$topic_info['fmt_topic_create_time'].' by '.stripslashes($topic_info['topic_owner']).'</td>
<td align=center>'.$num_posts.'</td>
</tr>';
}

$display .= '</table>';





Quote
Re: php gurus please help
Posted by gimpinthesink on Mon Feb 23rd at 11:24pm 2004


the $get_topics_res is defined to

Code:
$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) {

a coupple of lines above the last pics of code and yeh the db is set up to use it.

[addsig]




Quote
Re: php gurus please help
Posted by Leperous on Mon Feb 23rd at 11:28pm 2004


Should be a > sign there, I think, otherwise more poor but working code 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)

}





Quote
Re: php gurus please help
Posted by Monqui on Mon Feb 23rd at 11:37pm 2004


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... [addsig]




Quote
Re: php gurus please help
Posted by gimpinthesink on Mon Feb 23rd at 11:55pm 2004


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:
<?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 = "<p><em>No topics exist.</em></p>";
} 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\">
<strong>$topic_title</strong></a><br>
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; ?>
<p>Would you like to <a href="addtopic.html">add a topic</a>?</p>
</body>
</html>

[addsig]




Quote
Re: php gurus please help
Posted by scary_jeff on Mon Feb 23rd at 11:55pm 2004


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]




Quote
Re: php gurus please help
Posted by Leperous on Mon Feb 23rd at 11:57pm 2004


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...



Quote
Re: php gurus please help
Posted by Hornpipe2 on Tue Feb 24th at 1:11am 2004


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. [addsig]




Post Reply