我想使用phpQuery来解析DOM Elements。这是the HTML tree我想使用PHP递归函数导入MySQL数据库。
将数据库存储为类别(包含sub,sub,..,类别)的MySQL数据库
create table categories
(
categorie_id int(11) primary key auto_increment,
categorie_id_parent int(11) not null,
categorie_nom varchar(255) not null
)
这是我的伪代码:
function getCategories( $pq_object, $last_generated_mysql_id )
{
// for every element of $pq_object on same lvl
{
// insert into mysql $pq_object->a->name
// and with categorie_id_parent as $last_generated_mysql_id if has parent
// going deeper for every child
getCategories( $pq_object->children(), mysql_insert_id() );
}
}
感谢您的帮助
答案 0 :(得分:1)
我实际上是这样做的:
function getCats( $categorie_id_parent, $cats )
{
echo "<ul>";
foreach ( $cats as $ch )
{
$categorie_nom = mysql_real_escape_string( pq($ch)->find("a:first")->text() );
// Inserting categories
$query_categorie = "SELECT categorie_id FROM categories WHERE categorie_id_parent='".$categorie_id_parent."' AND categorie_nom LIKE '".$categorie_nom."' LIMIT 1";
$result_categorie = mysql_query( $query_categorie ) or die ( mysql_error() );
$categorie_id = 0;
if ( mysql_num_rows( $result_categorie ) )
{
$row_categorie = mysql_fetch_assoc( $result_categorie );
$categorie_id = $row_categorie['categorie_id'];
}
else
{
$query_insert = "INSERT INTO categories (categorie_id_parent, categorie_nom) VALUES ('".$categorie_id_parent."', '".$categorie_nom."')";
mysql_query( $query_insert ) or die ( mysql_error() );
$categorie_id = mysql_insert_id();
}
// if we have any subcategories
if ( pq($ch)->children()->children()->length )
{
echo "<li>".pq($ch)->find("a:first")->text()."</li>";
// going deeper
getCats( $categorie_id, pq($ch)->children()->children() );
}
// If no subcategories, show the last link name and href
else
echo "<li>".pq($ch)->find('a:first')->text()." ".pq($ch)->find('a:first')->attr('href')."</li>";
}
echo "</ul>";
}