Page 1 of 1

news post error

Posted: Tue Nov 29, 2005 11:36 am
by gaogier
i have created a scrip that will show the first post of a news forum, but it wont show the first post untill someone replies to it, eg, look here, http://gamingvortex.org/index.php , here is my script

Code: Select all

<?php
# News Db script - index.php

function displayNews($all = 0) {
global $sitestyle;
	require_once ('../mysql_connect1.php');//connect to db
    if ($all == 0) {   
    		$topic_query = "SELECT * FROM `phpbb_topics` WHERE `forum_id`= '1' ORDER BY `topic_time` DESC LIMIT 7"; // do topic query
    }else{
			$topic_query = "SELECT * FROM `phpbb_topics` WHERE `forum_id`= '1' ORDER BY `topic_time` DESC"; // do topic query
	}
		$topic_result = mysql_query ($topic_query);
			while ($topic_row = mysql_fetch_assoc($topic_result)) {
				$tt = $topic_row['topic_time'];
				$topic = $topic_row['topic_id'];
		  		$poster = $topic_row['topic_poster'];
				$first = $topic_row['topic_first_post_id'];
				$title = $topic_row['topic_title'];
				$topic_replies = $topic_row['topic_replies'];
				$topic_runescape = $topic_row['topic_runescape'];
				$post_query = "SELECT * FROM `phpbb_posts` WHERE  `topic_id` = '$topic' AND `poster_id` = '$poster' AND `post_id` = '$first'"; // do post subject query
				$post_result = mysql_query($post_query);
				while ($post_row = mysql_fetch_assoc($post_result)){
					$post_id = $post_row['post_id'];
				}
				$use_query = "SELECT `username`, `user_avatar`, `user_email`, `user_rank` FROM `phpbb_users` WHERE `user_id` = '$poster'"; // do user query

$use_result = mysql_query($use_query);
				while ($use_row = mysql_fetch_assoc($use_result)){
					 $user = $use_row['username'];
					 $user_avatar = $use_row['user_avatar'];
					 $user_email = $use_row['user_email'];
					 $rank = $use_row['user_rank'];					
				}
				$news_query = "SELECT `post_text` FROM `phpbb_posts_text` WHERE `post_id`='$post_id'";
				$news_result = mysql_query($news_query);
				while ($news_row = mysql_fetch_assoc($news_result)){
					$news = $news_row['post_text'];
				}
				$rank_result = mysql_query("SELECT `rank_title` FROM `phpbb_ranks` WHERE `rank_id`='$rank'");
				while($rank_row = mysql_fetch_assoc($rank_result)){
					$ranks = $rank_row['rank_title'];
				}
				$date = date("D M jS, Y g:i a", $tt);
				if ($topic_replies == 0){
				$reply = '<a href="http://www.gamingvortex.org/forums/posting.php?mode=reply&t='.$topic.'">Nobody has commented on this piece of news yet. Be the first!</a>';
				$post_new ="";
				}else{
				$reply =  'comments(<a href="http://www.gamingvortex.org/forums/viewtopic.php?t='.$topic.'">'.$topic_replies.'</a>) |';
				$post_new = '<a href="http://www.gamingvortex.org/forums/posting.php?mode=reply&t='.$topic.'">Post a comment</a>';
				
					$news1 = $news;
				}
				// Replace newlines //
				$news2 = str_replace("\n", "\n<br />\n", $news1);
		?>
	
		 <table border="0" cellpadding="0" cellspacing="0" width="100%" class="news">
        <tr>
          <td></td>
          <td><div align="center"><p class="small2"><a href="http://www.gamingvortex.org/forums/viewtopic.php?t=<?php echo $topic; ?>" class="news"><?php echo $title; ?></a> posted by <a href="http://www.gamingvortex.org/forums/profile.php?mode=viewprofile&u=<?php echo $poster; ?>"><?php echo $user; ?></a> on <?php echo $date;?></p></font></div></td>
          <td></td>
           </tr>
      </table>

             <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td></td>
         <td align="center" border="0" cellpadding="0" cellspacing="0"> 
		  
   			<img src="<?php echo $user_avatar; ?>" ><br /><center><font class="small2"><b><?php echo $ranks; ?></b></font></center></td>
		    <td width="429"> <p><font class="small2">
      <p><?php echo $news2; ?></p><br>
	  <br /><font class="small2"><?php echo $reply. ' '. $post_new;?>
<td></td>
          </tr>
          </table>
      
      <table border="0" cellpadding="0" cellspacing="0" width="100%">

       <tr>
                          <td width="16"></td>
                          <td></td>
                          <td width="16"></td>
        </tr>
      </table></div><br />
	<?php
    }
    
    /* if we aren't displaying all news, 
     * then give a link to do so */
    if ($all == 0) {
        echo "<center><a href=\"{$_SERVER['PHP_SELF']}" .
             "?action=all\">View all news</a></center>\n";
    }else{
		echo '<center><a href="'.$_SERVER['PHP_SELF'].'">View recent news only</a></center>';	
	}

}
echo "<CENTER>\n";
switch($_GET['action']) {
    case 'all':
        displayNews(1);
        break;
    default:
        displayNews();
}
echo "</CENTER>\n";
?>
if anyone can see why this is not working please let me know

Posted: Tue Nov 29, 2005 1:16 pm
by gaogier
someone please help

Posted: Tue Nov 29, 2005 2:38 pm
by Charles256
dude..you bumped within twenty four hours...big no no...either way.probably something in your sql queries..echo em and check em out..your logic probably went bad somewhere:-D

Posted: Tue Nov 29, 2005 2:44 pm
by gaogier
yea im sorry, but im really impactient, i have no idea what i have to do!

what is the problem?

Posted: Tue Nov 29, 2005 2:53 pm
by BDKR
gaogier wrote:yea im sorry, but im really impactient, i have no idea what i have to do!

what is the problem?
Did you try Charles suggestions? You can be a programmer if you can't debug your own stuff.

Posted: Tue Nov 29, 2005 3:00 pm
by Charles256
yeah,problem is do what I told you first:-D debugging is the funnest part of programming, don't deprive yourself of that joy:-d

Posted: Wed Nov 30, 2005 4:31 pm
by gaogier
the same code is working on another site, but not this one, why?

Posted: Wed Nov 30, 2005 6:04 pm
by John Cartwright
Have you even made an attempt at debugging the code?

I've taken the liberty to indent the code so we can actually read it (at the end of the post I posted it). I cannot stress how important it is to indent code properly. Should be the first thing users learn when entering the world of programming. Note: I have no changed any of the code, and won't make any attempt to unless you can show that you have tried to debug.

Now, when debugging you generally want to have all your queries set to variables so you can check what was actually set against what you were expecting to be set. In most instances you have done this, but not all of them. Prior to the mysql_query() call, echo out each query's variable to see if it what you were expected. For instance in one part of your code

Code: Select all

echo 'Debug $topic_query: '. $topic_query;
$topic_result = mysql_query ($topic_query);
Next, you are not checking for any mysql_error()'s. Modify your mysql_query() calls to omit an error if one is produced.
This will stop the script and output what has gone wrong. As it is setup, your script will continue to proceed even though your query failed. This is most likely the culperate of your problem (atleast this will tell us what went wrong)

Code: Select all

$result = mysql_query($sql) or die(mysql_error());


Next, I've noticed you are looping through rows and overwritting variables, I do not know if this was an accident or what. Your code:

Code: Select all

while ($post_row = mysql_fetch_assoc($post_result)){
			$post_id = $post_row['post_id'];
		}
If you only want to retrieve one row, simply do

Code: Select all

$post_row = mysql_fetch_assoc($post_result);
$post_id = $post_row['post_id'];
Now your HTML is a whole other issue, I recommend you run your site against http://validator.w3.org/ to see your issues ;)

Other than that ... I don't see anything apparantly wrong and can no longer help you unless you fixed what I mentioned above.

Code: Select all

<?php
# News Db script - index.php

function displayNews($all = 0) 
{
	global $sitestyle;
	require_once ('../mysql_connect1.php');//connect to db

	if ($all == 0) {   
		$topic_query = "SELECT * FROM `phpbb_topics` WHERE `forum_id`= '1' ORDER BY `topic_time` DESC LIMIT 7"; // do topic query
	}
	else {
		$topic_query = "SELECT * FROM `phpbb_topics` WHERE `forum_id`= '1' ORDER BY `topic_time` DESC"; // do topic query
	}

	$topic_result = mysql_query ($topic_query);

	while ($topic_row = mysql_fetch_assoc($topic_result)) {
		$tt = $topic_row['topic_time'];
		$topic = $topic_row['topic_id'];
		$poster = $topic_row['topic_poster'];
		$first = $topic_row['topic_first_post_id'];
		$title = $topic_row['topic_title'];
		$topic_replies = $topic_row['topic_replies'];
		$topic_runescape = $topic_row['topic_runescape'];
		
		$post_query = "SELECT * FROM `phpbb_posts` WHERE  `topic_id` = '$topic' AND `poster_id` = '$poster' AND `post_id` = '$first'"; // do post subject query
		$post_result = mysql_query($post_query);

		while ($post_row = mysql_fetch_assoc($post_result)){
			$post_id = $post_row['post_id'];
		}

		$use_query = "SELECT `username`, `user_avatar`, `user_email`, `user_rank` FROM `phpbb_users` WHERE `user_id` = '$poster'"; // do user query
		$use_result = mysql_query($use_query);
		
		while ($use_row = mysql_fetch_assoc($use_result)){
			$user = $use_row['username'];
			$user_avatar = $use_row['user_avatar'];
			$user_email = $use_row['user_email'];
			$rank = $use_row['user_rank'];                    
		}

		$news_query = "SELECT `post_text` FROM `phpbb_posts_text` WHERE `post_id`='$post_id'";
		$news_result = mysql_query($news_query);

		while ($news_row = mysql_fetch_assoc($news_result)){
			$news = $news_row['post_text'];
		}
		
		$rank_result = mysql_query("SELECT `rank_title` FROM `phpbb_ranks` WHERE `rank_id`='$rank'");

		while($rank_row = mysql_fetch_assoc($rank_result)){
			$ranks = $rank_row['rank_title'];
		}

		$date = date("D M jS, Y g:i a", $tt);
		
		if ($topic_replies == 0){
			$reply = '<a href="http://www.gamingvortex.org/forums/posting.php?mode=reply&t='.$topic.'">Nobody has commented on this piece of news yet. Be the first!</a>';
			$post_new ="";
		}
		else{
			$reply =  'comments(<a href="http://www.gamingvortex.org/forums/viewtopic.php?t='.$topic.'">'.$topic_replies.'</a>) |';
			$post_new = '<a href="http://www.gamingvortex.org/forums/posting.php?mode=reply&t='.$topic.'">Post a comment</a>';
			$news1 = $news;
		}
		// Replace newlines //
		$news2 = str_replace("\n", "\n<br />\n", $news1);
?>

		<table border="0" cellpadding="0" cellspacing="0" width="100%" class="news">
			<tr>
				<td></td>
				<td><div align="center"><p class="small2">
						<a href="http://www.gamingvortex.org/forums/viewtopic.php?t=<?php echo $topic; ?>" class="news"><?php echo $title; ?></a> 
						posted by 
						<a href="http://www.gamingvortex.org/forums/profile.php?mode=viewprofile&u=<?php echo $poster; ?>"><?php echo $user; ?></a> 
						on 
						<?php echo $date;?>
					</p></div></td>
				<td></td>
			</tr>
		</table>

		<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td></td>
				<td align="center" border="0" cellpadding="0" cellspacing="0">
					<img src="<?php echo $user_avatar; ?>" ><br /><font class="small2"><b><?php echo $ranks; ?></b></font></td>
				<td width="429"><p><font class="small2">
					<p><?php echo $news2; ?></p><br>
					<br /><font class="small2"><?php echo $reply. ' '. $post_new;?>
				<td></td>
			</tr>
		</table>

		<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td width="16"></td>
				<td></td>
				<td width="16"></td>
			</tr>
		</table></div><br />

<?php
	}

/* if we aren't displaying all news,
* then give a link to do so */
	if ($all == 0) {
		echo "<center><a href=\"{$_SERVER['PHP_SELF']}?action=all\">View all news</a></center>\n";
	}
	else{
		echo '<center><a href="'.$_SERVER['PHP_SELF'].'">View recent news only</a></center>';    
	}
}

	echo "<CENTER>\n";
	switch($_GET['action']) {
		case 'all':
			displayNews(1);
			break;
		default:
			displayNews();
		}
	echo "</CENTER>\n";
?> 


?>