我想使用phpQuery解析DOM元素。这是我要使用PHP递归函数导入到MySQL数据库中的the HTML tree。
将树存储为类别的MySQL数据库(带有sub,sub,..,category)
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() );
}
}
谢谢您的帮助
最佳答案
我实际上做到了:
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>";
}
关于php - HTML ul/li树到使用phpQuery的PHP数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13527604/