Jump to content

Recommended Posts

<?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?

Link to comment
https://forums.phpfreaks.com/topic/299868-domdocument-wont-work/
Share on other sites

:D

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:

 
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 by MrGohut
<?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 :/

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.