所以我在这里遵循了这个教程:http://www.ibm.com/developerworks/opensource/library/os-php-twitter-interface/index.html?ca=dgr-lnxw9d&S_TACT=105AGX59&S_CMP=grsitelnxw9d以了解关于关系数据库的更多信息,但由于某些原因它无法正常工作,我很确定它与最后两个步骤有关。我觉得有更好的PHP知识的人能够看到问题。提前谢谢,
继承了一些代码 - 首先是函数:
function show_posts($userid,$limit=0){
$posts = array();
$user_string = implode(',', $userid);
$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 in ($user_string) order by stamp desc $extra";
$result = mysql_query($sql);
while($data = mysql_fetch_object($result)){
$posts[] = array( 'stamp' => $data->stamp,
'userid' => $data->user_id,
'body' => $data->body
);
}
return $posts;
}
现在实施:
$users = show_users($_SESSION['userid']);
if (count($users)){
$myusers = array_keys($users);
}else{
$myusers = array();
}
$myusers[] = $_SESSION['userid'];
$posts = show_posts($myusers,5);
我的主要问题是
1)show_posts在索引页面上回显一个随机的sql行。 2)我无法在索引页面上找到正确的位置,以便将实现放在最后一步。
继承索引页面以供参考:
<?php
session_start();
include_once('header.php');
include_once('functions.php');
$_SESSION['userid'] = 1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Microblogging Application</title>
</head>
<body>
<?php
if (isset($_SESSION['message'])){
echo "<b>". $_SESSION['message']."</b>";
unset($_SESSION['message']);
}
?>
<form method='post' action='add.php'>
<p>Your status:</p>
<textarea name='body' rows='5' cols='40' wrap=VIRTUAL></textarea>
<p><input type='submit' value='submit'/></p>
</form>
<?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['userid'] ."</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
}
?>
</body>
</html>