I want to use phpQuery for parsing DOM Elements. Here is the HTML tree that i want to import into MySQL db using a PHP recursive function.
The MySQL db that will store the tree as categories (with sub, sub, .., categories)
create table categories
(
categorie_id int(11) primary key auto_increment,
categorie_id_parent int(11) not null,
categorie_nom varchar(255) not null
)
Here is my pseudocode :
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() );
}
}
Thank you for your help
I actually did it :
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>";
}