【php开源代码栏目提醒】:网学会员为需要php开源代码的朋友们搜集整理了articlelib.php相关资料,希望对各位网友有所帮助!
<?php
//articlelib.php 在article子目录下
require "../lib.php";
$db = db_connect();
//返回论坛中所有文章的数目
function readnum()
{
global $db;
$sql = "select id from article where id>0";
$result = mysql_query($sql, $db);
$total = mysql_num_rows($result);
return $total;
}
//返回论坛中所有parent字段值为0的记录(一级父)
function readborder_parent()
{
global $db;
$sql = "select id,title,importtime,writer,click from article where parent='0' order by id desc";
$result = mysql_query($sql, $db) or db_error();
if ($result)
{
return $result;
}
else
{
return "";
}
}
//这里用到了两个数组,$infocontent_child, $infofamily_child
//$infocontent_child用来存储对应于一个一级父下的所有子文章
//infofamily_child用来存储这些文章之间的关系,如0表示一级父、1表示二级父、2表示三级父
//依次类推。以方便显示文章之间的关系
//返回对应于一级父下的所有子文章和这些文章的内在联系
$infocontent_child;
$infofamily_child;
function readborder_child($parent, $arragement)
{
global $db;
global $infocontent_child;
global $infofamily_child;
$sql = "select id,title,importtime,writer,click from article where parent='$parent' order by id desc";
$result = mysql_query($sql, $db) or db_error();
if ($result)
{
$i = 0;
while ($resarticle=mysql_fetch_array($result))
{
$nums = count($infocontent_child);
$infocontent_child[$nums] = $resarticle;
$nums = count($infofamily_child);
$infofamily_child[$nums] = $arragement;
if ($resarticle["id"] > 0)
{
$i++;
$parent = $resarticle["id"];
readborder_child($parent, $arragement+$i);
//递归调用
}
}
}
}
//这里用到了两个数组:$info_content, $info_family
//$info_content用来存储论坛所有的文章信息
//$info_family用来存储文章之间的关系
//该函数返回论坛中所有的文章,以及这些文章之间的关系
$info_content;
$info_family;
function readborder_all()
{
global $infocontent_child;
global $infofamily_child;
global $info_content;
global $info_family;
global $begin;
if (empty($begin))
{
$begin = 0;
}
$result_parent = readborder_parent();
//读出所有的一级父
if (!empty($result_parent))
{
while ($resarticle_parent = mysql_fetch_array($result_parent))
{
if ($resarticle_parent["id"] > 0)
{
//找出每一个一级父,再找出其所有与之关联的实体
$info_content[] = $resarticle_parent;
//一级父的info_family为0
$info_family[] = 0;
readborder_child($resarticle_parent["id"], 1);
if (is_array($infocontent_child) && is_array($infofamily_child))
{
$nums = count($infocontent_child);
for ($i=$begin; $i<$nums; $i++)
{
$info_content[] = $infocontent_child[$i];
$info_family[] = $infofamily_child[$i];
}
$begin = $nums;
}
}
}
}
}
//该函数返回某篇文章的相关信息
function readfamily($id, $obj)
{
global $db;
if (!empty($id))
{
//根据$obj的不同,返回自己的详细信息($obj == "self_content")或简略信息
//($obj=="self")、其父文章($obj=="parent")以及其一级子文章($obj=="child")
if ($obj == "self_content")
{
$sql = "select title,content,importtime,click,writer from article where id='$id'";
}
if ($obj == "self")
{
$sql = "select title,importtime,click,writer from article where id='$id'";
}
elseif ($obj == "parent")
{
$sql = "select parent from article where id='$id'";
$result = mysql_query($sql, $db) or db_error();
if ($result)
{
list($parent) = mysql_fetch_row($result);
if ($parent > 0)
{
$sql = "select id from article where id='$parent'";
}
}
}
elseif ($obj == "child")
{
$sql = "select id from article where parent='$id' order by id desc";
}
if (!empty($sql))
{
$result =
mysql_query($sql, $db) or db_error();
if ($result)
{
return $result;
}
}
}
}
//通过调用函数readfamily()、read_three()将返回某篇文章的所有相关主体以及
//它们之间的内在联系
function read_three($id, $need)
{
$res_parent = readfamily($id, "parent");
$level = 0;
if ($info_parent = mysql_fetch_array($res_parent))
{
if ($info_parent["id"]>0)
{
$id_three[] = $info_parent["id"];
$family_three[] = $level;
$level = 1;
if ($info_parent["id"] > 0)
{
$res_same = readfamily($info_parent["id"], "child");
while ($info_same = mysql_fetch_array($res_same))
{
if ($info_same["id"] != $id)
{
$id_three[] = $info_same["id"];
$family_three[] = $level