Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 08:06 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,409
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Please wrap your code in php tags not code tags

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,409
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Ah nevermind.

    If you wish to suggest sticking with code tags then thats fine. I won't help in this topic.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •