Como ligar um número arbitrário de valores a uma instrução preparada no mysqli?
Eu realmente gostaria que alguém tomasse um tempinho e olhasse meu código. Estou analisando algum conteúdo de notícias e posso inserir a análise inicial em meu banco de dados que contém o URL de notícias e o título. Eu gostaria de expandi-lo ainda mais, passar ao longo de cada link de artigo e analisar o conteúdo do artigo e incluí-lo no meu banco de dados. A análise inicial funciona perfeitamente assim:
<?php
include_once ('connect_to_mysql.php');
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
$main = $html->find('div[class=mainBlock]', 0);
$items = array();
foreach ($main->find('a') as $m){
$items[] = '("'.mysql_real_escape_string($m->plaintext).'",
"'.mysql_real_escape_string($m->href).'")';
}
$reverse = array_reverse($items);
mysql_query ("INSERT IGNORE INTO basket_news (article, link) VALUES
".(implode(',', $reverse))."");
?>
Como você pode ver, estou usandoPHP Parser HTML simples. Para expandir, estou tentando usar a instrução mysqli onde posso vincular os parâmetros para que todas as tags html sejam inseridas em meu banco de dados. Eu fiz isso antes com a análise XML. O problema é que eu não sei como ligar o array, e ver se meu código está correto, se funcionará dessa forma ... Aqui está o código inteiro:
<?php
$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->query("SET NAMES 'utf8'");
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
//find main news
$main = $html->find('div[class=mainBlock]', 0);
$items = array();
foreach ($main->find('a') as $m){
$h = file_get_html('http://www.basket-planet.com'.$m->href.'');
$article = $h->find('div[class=newsItem]');
//convert to string to be able to modify content
$a = str_get_html(implode("\n", (array)$article));
if(isset($a->find('img'))){
foreach ($a->find('img') as $img){
$img->outertext = '';}} //get rid of images
if(isset($a->find('a'))){
foreach ($a->find('a') as $link){
$link->href = 'javascript:;';
$link->target = '';}} //get rid of any javascript
if(isset($a->find('iframe'))){
foreach ($a->find ('iframe') as $frame){
$frame->outertext = '';}} //get rid of iframes
@$a->find('object', 0)->outertext = '';
@$a->find('object', 1)->outertext = '';
//modify some more to retrieve only text content
//put entire content into a div (will if statements work here???)
$text_content = '<div>'.$a.'<br>'.
($a->find('object', 0)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 0)->data.'">Play Video</a> ')
($a->find('object', 1)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 1)->data.'">Play Video</a> ')
($a->find('iframe[src*=youtube]', 0)->src > 0 ? '<a target="_blank" href="'.$a->find('iframe', 0)->src.'">Play Video</a> ')
//couple more checks to see if video links are present
.'</div>';
$items[] = '("'.$m->plaintext.'","'.$m->href.'","'.$text_content.'")';
}
//reverse the array so the latest items have the last id
$reverse = array_reverse($items);
$stmt = $mysqli->prepare ("INSERT IGNORE INTO test_news (article, link, text_cont) VALUES (?,?,?)");
$stmt->bind_param ???; //(implode(',', $reverse));
$stmt->execute();
$stmt->close();
?>
Então a lógica é para cada href de um artigo encontrado, eu estou passando para analisar o conteúdo e estou tentando adicioná-lo ao array. Eu provavelmente tenho uma tonelada de erros, mas eu não posso testá-lo ainda porque eu não sei como ligá-lo para ver se funciona. E eu também não tenho certeza se posso fazer as declarações if dentro de $ text_content div ... significando exibir "Play Video" se elas existirem. Então, por favor, se alguém puder dedicar algum tempo para trabalhar nisso comigo, eu realmente aprecio isso.
UPDATE: alterou as instruções if para operadores de comparação em $ text_content div.