我有一个信息系统,更具体地说是一个信息系统,即票务系统.信息系统将包含拥有无限或n个用户的帐户.我希望用户能够看到其他用户在新闻源中的操作或对内容的更改. (就像Facebook).我将使用PHP,MysqL和AJAX(或jQuery)实现新闻源.我将知道如何设置表和查询.
如何使用PHP和AJAX或jQuery提取内容并将其显示在新闻源中(以具有淡入淡出或滚动效果的Facebook新闻源样式显示).
我一直在寻找一个好的教程,但没有找到一个.如果可能的话,我想最好从头开始编写代码.
我仍然有一些问题:这是我的问题:
ajax.PHP
<?PHP require_once('../../Connections/f12_database_connect.PHP'); ?>
<?PHP
if (!function_exists("GetsqlValueString")) {
function GetsqlValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("MysqL_real_escape_string") ? MysqL_real_escape_string($theValue) : MysqL_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
MysqL_select_db($database_f12_database_connect, $f12_database_connect);
$query_newsFeed_a = "SELECT * FROM newsFeed";
$newsFeed_a = MysqL_query($query_newsFeed_a, $f12_database_connect) or die(MysqL_error());
while($row_newsFeed_a = MysqL_fetch_assoc($newsFeed_a))
{
echo("<div class='FeedItem'>");
echo("<div class='title'>" . $FeedItem['title'] . "</div>");
echo("<div class='body'>" . $FeedItem['body'] . "</div>");
echo("</div>");
}
$totalRows_newsFeed_a = MysqL_num_rows($newsFeed_a);
?>
<?PHP
MysqL_free_result($newsFeed_a);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script>
function refreshNews()
{
$("#news").load("ajax.PHP")
}
</script>
</head>
<body>
<div id="news"></div>
</body>
</html>
我究竟做错了什么?
解决方法:
如果您想从头开始编写代码,则基本过程是创建一个PHP脚本来收集数据并将其发送回AJAX请求.通常,我创建一个单独的PHP文件来处理所需的任何操作.
您的PHP脚本正常输出的所有内容都将发送回AJAX请求.因此,任何HTML标记,任何echo / print语句. header()之类的东西也会创建输出,只是一个警告.
根据页面的设计,您可以使用PHP创建HTML,然后根据需要使用jQuery将html放入页面中.另一个选择是使用PHP的json_encode()并将所有数据作为JSON发送回去,并构建HTML结构客户端.
如果每个提要项都具有相同的基本结构,则像在任何常规页面中一样,在PHP中创建HTML服务器端可能是最容易的.您只需要供稿的HTML代码段即可.
最简单的方法是jQuery.load()
http://api.jquery.com/load/
在HTML页面内:
<script>
function refreshNews()
{
$("#news").load("path/to/ajax.PHP")
}
</script>
<div id="news"></div>
在PHP中:
$sql = "sql TO GET NEWS Feed";
$result = MysqL_query($sql);
while($FeedItem = MysqL_fetch_assoc($result))
{
echo("<div class='FeedItem'>");
echo("<div class='title'>" . $FeedItem['title'] . "</div>");
echo("<div class='body'>" . $FeedItem['body'] . "</div>");
echo("</div>");
}
然后,您可以从另一个事件(刷新按钮,定时事件等)中调用refreshNews().
显然,您的html和数据结构可能有所不同.只要确保这是此PHP文件输出的唯一内容即可.这包括标签之外的所有内容.
有更有效的方法来执行此操作,此脚本实际上将在每次调用refreshNews()时重新加载新闻项的整个列表.目前,这是使其工作最简单的方法之一,因此请尝试一下,如有需要,可以使用更有效的方法.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。