i want to insert some elements in my database, but i want that $pavadinimas and %kaina be in one line, not different. Moreover it will be pretty cool if i could generate my elements in all pages from website, but then I insert more than 2 links i get error from refreshing my web that page could not load. Here is my code. Thanks for help!
<?php // example of how to modify HTML contents
include_once('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('https://www.varle.lt/mobilieji-telefonai/');
foreach($html->find('span[class=inner]') as $pavadinimas) {
$pavadinimas = str_replace("<span class=", " ", $pavadinimas);
$pavadinimas = str_replace("inner>", " ", $pavadinimas);
$pavadinimas = str_replace("<span>", " ", $pavadinimas);
$pavadinimas = str_replace("</span></span>", " ", $pavadinimas);
$pavadinimas = str_replace('"inner"> ', " ", $pavadinimas);
}
foreach($html->find('span[class=price]') as $kaina) {
$kaina = str_replace("Lt", " ", $kaina);
$kaina = str_replace("<span class=", " ", $kaina);
$kaina = str_replace("price", " ", $kaina);
$kaina = str_replace("</span>", " ", $kaina);
$kaina = str_replace(",<sup>99</sup>", " ", $kaina);
$kaina = str_replace(",<sup>99</sup>", " ", $kaina);
$kaina = str_replace(" ", " ", $kaina);
$kaina = str_replace('" ">', " ", $kaina);
$kaina = str_replace(" ", " ", $kaina);
$query = "insert into telefonai (pavadinimas,kaina) VALUES (?,?)";
$this->db->query($query, array($pavadinimas,$kaina));
}
?>
Proceed step by step...
Start by getting all the wanted info from one page (the 1st for example)... The idea is to:
$phones = $html->find('a[data-id]');
Now that you have the code working for one page, let's try to make it work for all pages knowing that:
Next
button, so we'll stop when this link cannot be foundSo here's a code summarizing all what we said above:
Output
Working DEMO
Notice that the code may take a while to parse all the pages, so php may return this error
Fatal error: Maximum execution time of 30 seconds exceeded ...
. Then, simply extend the maximum execution time like this: