Function and Code are not passing through variables properly. Somewhat of a debugging request in a twitter-like system code
For some reason the form data is not being passed through pr开发者_JAVA百科operly in a function that I have used from http://www.ibm.com/developerworks/opensource/library/os-php-twitter-interface/index.html#author1
I am not sure if I am using implode correctly. If you notice though, I did change some values in the tutorial because I just felt that they seemed incorrect. Just go ahead and let me know what I should change. Right now, profile.php comes up and no posts show up. However, the databases are getting all of the posts perfectly.
There are no error messages... Something just isn't being passed through properly.
I am at the last part of the tutorial. My Code that is on index\profile.php:
<!-- 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
}
?>
And this is the code in functions.php:
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;
}
One obvious error I saw:
function show_posts($userid,$limit=0){
$posts = array();
$user_string ="'" .implode("','", $posts). "'";
$posts is defined just before you implode it, so it will always be empty. Did you change that line? Shouldn't it be:
implode("','", $userid);
??
But honestly. This is more of a "request to debug my code" than an actual question.
Final created mysql query is wrong at all.
echo $sql;
and you will see how the query is wrong you are adding condition part after order by which should be with the where clause.
and $user_string
is blank at all.
It looks like you have an extra set of single quotes in your implode, should'nt it be...
$user_string ="'" .implode(",", $posts). "'";
精彩评论