MrGohut Posted December 19, 2015 Share Posted December 19, 2015 <?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once ('../includes/connect.php'); try { $filter = $db->query("SELECT * FROM procesory"); $check = $filter->rowCount(); echo $check.'<br />'; while($c = $filter->fetch()){ //echo $c['ID']; $XMLDoc = new DOMDocument(); $source = $c['pURLMorele']; $XMLDoc->loadHTML($source); $xpath = new DOMXPath($XMLDoc); $rows = $xpath->query("//span[@class='price']"); foreach ($rows as $row) { var_dump($row->nodeValue); } } } catch (PDOException $e) { echo 'Something went wrong!<br>'.$e->getMessage(); die(); } ?> I've got database, table "procesory" and there are 4 records. I need to get from each one of them "pURLMorele" column and from URL in this column show on my site text from span.class="price". Now... This code won't work. I only shows "4" from echo. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/299868-domdocument-wont-work/ Share on other sites More sharing options...
ginerjm Posted December 19, 2015 Share Posted December 19, 2015 What the heck is this line: $rows = $xpath->query("//span[@class='price']"); That line has to give you some kind of error. What are you trying to do there? Quote Link to comment https://forums.phpfreaks.com/topic/299868-domdocument-wont-work/#findComment-1528244 Share on other sites More sharing options...
MrGohut Posted December 19, 2015 Author Share Posted December 19, 2015 (edited) Okay so.. 1. I'm getting URL from my database $c['pURLMorele']; 2. i'm entering on that website and getting text in SPAN with class "price" 3. I'm showing that text on my website. I repaired it: <?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once ('../includes/connect.php'); try { $filter = $db->query("SELECT * FROM procesory"); $check = $filter->rowCount(); echo $check.'<br />'; while($c = $filter->fetch()){ //echo $c['ID']; $XMLDoc = new DOMDocument(); $source = file_get_contents($c['pURLMorele']); libxml_use_internal_errors(true); $XMLDoc->loadHTML($source); $xpath = new DOMXPath($XMLDoc); $rows = $xpath->query("//span[@class='price']"); foreach ($rows as $row) { echo $c['ID'].' - '; print_r($row->textContent.' -- OK!<br />'); } } } catch (PDOException $e) { echo 'Something went wrong!<br>'.$e->getMessage(); die(); } ?> But the problem is that this is doing only 3 of 4 rows: http://scr.hu/3tpn/e2vu1 http://scr.hu/3tpn/7ychi I hope you get it now XD I'm not very good at explaining. I used docs about it and some examples from stackoverflow Edited December 19, 2015 by MrGohut Quote Link to comment https://forums.phpfreaks.com/topic/299868-domdocument-wont-work/#findComment-1528245 Share on other sites More sharing options...
MrGohut Posted December 19, 2015 Author Share Posted December 19, 2015 <?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once ('../includes/connect.php'); try { $filter = $db->query("SELECT * FROM procesory"); $check = $filter->rowCount(); echo $check.'<br />'; while($c = $filter->fetch()){ $source = file_get_contents($c['pURLMorele']); $doc = new DomDocument(); $file = @$doc->loadHTML($source); $rows = $doc->getElementsByTagName('span'); foreach ($rows as $row) { if($row->getAttribute('class') == 'price') { echo $c['ID'].' - '; echo $row->nodeValue.' -- OK!<br />'; $id = $c['ID']; $price = $row->nodeValue; $update = $db->prepare("UPDATE procesory SET pPrice=:price WHERE ID=:id"); $update->bindValue(':price', $price); $update->bindValue(':id', $id); $update->execute(); } } } } catch (PDOException $e) { echo 'Something went wrong!<br>'.$e->getMessage(); die(); } ?> I think that's better, but still.. All the time ID from 2 to 4 and i need from 1 to 4 :/ Quote Link to comment https://forums.phpfreaks.com/topic/299868-domdocument-wont-work/#findComment-1528247 Share on other sites More sharing options...
ginerjm Posted December 20, 2015 Share Posted December 20, 2015 Sorry. Dont' know what this last set of code has to do with "1 to 4". Have you tried to show the id values you get in your first query before you continue on with all the rest of the code? What do you see when you do that? BTW - you should prepare the query ONCE - outside the loop - and then just bind the values inside the loop. That's the whole point to having a prepared query. Quote Link to comment https://forums.phpfreaks.com/topic/299868-domdocument-wont-work/#findComment-1528277 Share on other sites More sharing options...
MrGohut Posted December 20, 2015 Author Share Posted December 20, 2015 Hmm okay, i fixed it. Now it works fine. BTW - you should prepare the query ONCE - outside the loop - and then just bind the values inside the loop. That's the whole point to having a prepared query. Thanks I'll do that. Quote Link to comment https://forums.phpfreaks.com/topic/299868-domdocument-wont-work/#findComment-1528308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.