1

Here is my code:

$url = "https://www.leaseweb.com/dedicated-servers/single-processor";

libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$n = $xpath->query('//td[@data-column-name="Model"]');
$r = $xpath->query('//td[@data-column-name="RAM"]');

foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $RAM  = $r->nodeValue;
    echo " $Name - RAM: $RAM<br>";
}

I successfully echo $Name but i can not get the value for $RAM because i do not have it in my array. My question is how i can add $r into this foreach loop and make it work ?

Thanks in advance!

8
  • foreach ($n as $key => $entry) { $Name = $entry->nodeValue; $RAM = $r[$key]->nodeValue; echo " $Name - RAM: $RAM<br>"; } Commented Aug 4, 2015 at 10:44
  • I don't get it. Can you make it as an answer ? Commented Aug 4, 2015 at 10:47
  • 1
    can you please tell me how data of $r is related to data of $n then i can help you. Commented Aug 4, 2015 at 11:02
  • Are you shure that $n->length == $r->length? Commented Aug 4, 2015 at 11:03
  • Yes that is for sure! Commented Aug 4, 2015 at 11:03

4 Answers 4

1

PHP Documentation is kind of ambiguous at this point. DOMXPath::query returns a DOMNodeList which isn't really an Iterator nor does it seems to implement ArrayAccess but rather implements Traversable which doesn't have a particular signature that's helpful to traverse the object.

Here's something that comes to mind:

$item = 0;
foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $RAM  = $r->item($item)->nodeValue;
    echo " $Name - RAM: $RAM<br>";
    $item++;
}

Something else that might work:

$iterator = new IteratorIterator($r);
$iterator->rewind();
foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $node = $iterator->current();
    $RAM  = $node->nodeValue;
    $iterator->next();
    echo " $Name - RAM: $RAM<br>";
}

IteratorIterator class:

This iterator wrapper allows the conversion of anything that is Traversable into an Iterator. It is important to understand that most classes that do not implement Iterators have reasons as most likely they do not allow the full Iterator feature set. If so, techniques should be provided to prevent misuse, otherwise expect exceptions or fatal errors.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the only one working answer so far! Thank you so much!
0

use $key variable inside loop

   foreach ($n as $key => $entry) {
        $Name = $entry->nodeValue;
        $RAM  = $r[$key]->nodeValue;
        echo " $Name - RAM: $RAM<br>";
    }

1 Comment

Your blank screen could be a result of an error. Please check the errorlog
0

You could try with current and next array functions (php doc)

libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$n = $xpath->query('//td[@data-column-name="Model"]');
$r = $xpath->query('//td[@data-column-name="RAM"]');
//Reset pointer to first element and assign to currR
$currR = reset($r);

foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $RAM  = $currR->nodeValue;
    echo " $Name - RAM: $RAM<br>";
    //Move pointer to next element and assign this to currR
    $currR = next($r);
}

1 Comment

It is not working. The complete code is posted above in the edited question. You can now test it.
0
$i=0;
foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $RAM  = $r[$i]->nodeValue;
    echo " $Name - RAM: $RAM<br>";
    $i++;
}

1 Comment

if you can post print_r($r) then i can do some help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.