Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-12-2011, 07:02 AM   PM User | #1
truong50
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
truong50 is an unknown quantity at this point
Help to Debug my twitter-like code (from a tutorial) desperate!

Hi all
This is my first post and I know most users hate to help with debugging but I am trying to implement a twitter system into my website as it would be the main platform. I am currently following this tutorial which is very helpful but am having trouble finishing this baby up.

http://www.ibm.com/developerworks/op...html#icomments

Right now, I have everything working properly but my code is not able to retrieve the posts back from the database. "posts" are being saved to the database but not being able to be retrieved. I feel that this has a lot to do with the function show_post(). Perhaps it has something to do with implode as well? I say this because I know that implode() has to accept an array and I realize that some of the code in the tutorial is incorrect.

Somethign important that I need to mention is that I did change some values in the file. One of them is in implode() since I know that it only accepts arrays. I changed $userid to $posts.

Also, there are no error messages. Something is just not working correctly since the immediate message I receive is that "You haven't posted anything yet!" Obviously my code is not grabbing the values from the database correctly.

Here is the function on my end.

Code:
//selects the posts from the tables and fetches the results
function show_posts($userid,$limit=0){
	$posts = array();

	$user_string ="'" .implode(",", $posts). "'";
	$extra =  " and id in ($user_string)";

	if ($limit > 0){
		$extra = "limit $limit";
	}else{
		$extra = '';	
	}

	$sql = "select user_id, body, stamp from posts 
		where user_id='$user_string'
		order by stamp desc $extra";
	//echo $sql;
	$result = mysql_query($sql);

	while($data = mysql_fetch_object($result)){
		$posts[] = array( 	'stamp' => $data->stamp, 
							'user_id' => $data->user_id, 
							'body' => $data->body
					);
	}
	return $posts;

}
And here is the main index\profile.php page:
Code:
<!-- Shows posts on page-->
<?php
$posts = show_posts($_SESSION['userid']);

if (count($posts)){
?>
<table border='1' cellspacing='0' cellpadding='5' width='500'>
<?php
foreach ($posts as $key => $list){
	echo "<tr valign='top'>\n";
	echo "<td>".$list['user_id'] ."</td>\n";
	echo "<td>".$list['body'] ."<br/>\n";
	echo "<small>".$list['stamp'] ."</small></td>\n";
	echo "</tr>\n";
}
?>
</table>
<?php
}else{
?>
<p><b>You haven't posted anything yet!</b></p>
<?php
}
?>
Thanks so much, I really appreciate it, and I'm glad I made my first post here! Awaiting your replies!

Last edited by truong50; 03-12-2011 at 07:06 AM..
truong50 is offline   Reply With Quote
Old 03-12-2011, 01:32 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Please wrap your code in php tags not code tags
tangoforce is online now   Reply With Quote
Old 03-12-2011, 04:13 PM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by tangoforce View Post
Please wrap your code in php tags not code tags
code tags rulz coz i can use icode,

try to see $data using var_dump:
Code:
//selects the posts from the tables and fetches the results
function show_posts($userid,$limit=0){
	$posts = array();

	$user_string ="'" .implode(",", $posts). "'";
	$extra =  " and id in ($user_string)";

	if ($limit > 0){
		$extra = "limit $limit";
	}else{
		$extra = '';	
	}

	$sql = "select user_id, body, stamp from posts 
		where user_id='$user_string'
		order by stamp desc $extra";
	//echo $sql;
	$result = mysql_query($sql);

	while($data = mysql_fetch_object($result)){
                var_dump($data);
		$posts[] = array( 	'stamp' => $data->stamp, 
							'user_id' => $data->user_id, 
							'body' => $data->body
					);
	}
	return $posts;

}
best regards
oesxyl is offline   Reply With Quote
Old 03-12-2011, 04:38 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Ah nevermind.

If you wish to suggest sticking with code tags then thats fine. I won't help in this topic.
tangoforce is online now   Reply With Quote
Reply

Bookmarks

Tags
mysql, php, twitter

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:48 PM.


Advertisement
Log in to turn off these ads.