вот скрипт:
Код:
class Category
{
var $categoryInfo = array(
"id"=>0,
"parent_id"=>0,
"level"=>0,
"title"=>"",
"desc"=>"",
"creation_date"=>"",
"modify_date"=>"",
"photo"=>"");
var $categoryInfoCollection = array();
function Category()
{
//default constructor
}
................
function GetCategoryByParentID($categoryParentID)
{
$db = new MySql(null,"mport3",null,null);
$db->MySql_Connect();
$db->MySql_SelectDb();
$selectQuery = "select * from category where cat_PARENTID = ".
$categoryParentID;
$db->MySql_QueryDb($selectQuery);
//echo $selectQuery;
if ($db->dbResult != null)
{
while($row = mysql_fetch_assoc($db->dbResult))
{
$this->categoryInfo['id'] = $row['cat_ID'];
$this->categoryInfo['parent_id'] = $row['cat_PARENTID'];
$this->categoryInfo['level'] = $row['cat_NODELEVEL'];
$this->categoryInfo['title'] = $row['cat_TITLE'];
$this->categoryInfo['desc'] = $row['cat_DESC'];
$this->categoryInfo['creation_date'] = $row['cat_CREATION_DATE'];
$this->categoryInfo['modify_date'] = $row['cat_MODIFY_DATE'];
$this->categoryInfo['photo'] = $row['cat_PHOTO_URL'];
$this->categoryInfoCollection[] = $this->categoryInfo;
}
return 0;
}
return -1;
}
................
}
"дерево":
Код:
function Tree($categoryParentID)
{
$category = new Category();
if ($category->GetCategoryByParentID($categoryParentID) == 0)
{
if (!empty($category->categoryInfoCollection))
{
foreach($category->categoryInfoCollection as $categoryInfoItem)
{
echo $categoryInfoItem["title"] . " " . $categoryInfoItem["parent_id"] . "<BR>\r\n";
Tree($categoryInfoItem["id"]);
}
}
}
}
mysql helper class:
Код:
class MySql
{
var $dbServer = "";
var $dbName = "";
var $dbUser = "";
var $dbPass = "";
var $dbConn;
var $dbResult;
var $dbErrorNumber = 0;
var $dbErrorMessage = "";
var $debugFromHanlder = "";
/*function MySql()
{
//default constructor
}*/
function MySql($dbServer = null, $dbName = null, $dbUser = null, $dbPass = null)
{
//default constructor
if ($dbServer != null)
{
$this->dbServer = $dbServer;
}
if ($dbName != null)
{
$this->dbName = $dbName;
}
if ($dbUser != null)
{
$this->dbUser = $dbUser;
}
if ($dbPass != null)
{
$this->dbPass = $dbPass;
}
}
function MySql_Connect()
{
if (!isset($this->dbConn) || $this->dbConn == null)
{
$this->dbConn = @mysql_connect(
$this->dbServer,
$this->dbUser,
$this->dbPass);
$this->MySql_ErrorHanlder("connect");
}
}
function MySql_SelectDb()
{
if (isset($this->dbName) && $this->dbName != null)
{
@mysql_select_db($this->dbName,$this->dbConn);
$this->MySql_ErrorHanlder("select");
}
}
function MySql_QueryDb($dbQuery)
{
if (isset($dbQuery) && $dbQuery != null)
{
$this->dbResult = @mysql_query($dbQuery,$this->dbConn);
//echo $dbQuery;
$this->MySql_ErrorHanlder("query");
if (@mysql_num_rows($this->dbResult) == 0)
{
$this->dbResult=null;
}
}
}
function MySql_Close()
{
if (isset($this->dbConn) || $this->dbConn != null)
{
@mysql_close($this->dbConn);
$this->MySql_ErrorHanlder("close");
}
}
function MySql_Free()
{
@mysql_free($this->dbResult);
$this->MySql_ErrorHanlder("free");
}
function MySql_ErrorHanlder($debugFromHanlder)
{
$this->dbErrorNumber = mysql_errno();
$this->dbErrorMessage = mysql_error();
if ($this->dbErrorNumber != 0)
{
echo $this->dbErrorNumber . " " . $this->dbErrorMessage;
echo " " . $debugFromHanlder . "<BR>\r\n";
}
$this->dbErrorNumber = 0;
$this->dbErrorMessage = "";
}
}