0
$title = $xpath->query("span[@class='item_list']",$item)[0];

I can't figure out why it only works when I omit this line on my web host. localhost seems normal and functional as intended. This is used on a template in Wordpress 4.9.4. I have no idea what the PHP version of the host is. This line is inside a loop of another xpath query which works with the page if that line above is cut out:

foreach( $xpath->query("//div[@class='product_list']") as $item) {    }

Could there be a limitation set in php on my web host that prevented me to do more than one query? or nested query perhaps?

typescript
  • 131
  • 1
  • 2
  • 12
  • 1
    Crashes how? Does it give any error messages? You may need to look at the server error logs for any information. – aynber Mar 01 '18 at 18:34
  • What happens if you do `$matches = $xpath->query("span[@class='item_list']",$item); $title = $matches[0];` – Patrick Q Mar 01 '18 at 18:38
  • @aynber It displays blank content. The header is left out, probably because get_header() is at the top. Do I have access to the server error logs in wordpress? – typescript Mar 01 '18 at 18:40
  • @PatrickQ It still shows no result – typescript Mar 01 '18 at 18:42
  • It could be that your first query doesn't match anything - so when you try and use [0] it will fail with no element. You should check if it returns anything before assuming it has even 1 element. – Nigel Ren Mar 01 '18 at 18:42
  • @NigelRen The first query should have results considering my localhost has shown the result and also by omitting this line, it displays the said result – typescript Mar 01 '18 at 18:46
  • The error logs would be on the server somewhere, but not accessible by Wordpress. You can look in the Wordpress directory for an `error.log` file, or you can check wherever the host puts the error logs. – aynber Mar 01 '18 at 19:09
  • @aynber Thanks. I managed to get the error using this [function](https://stackoverflow.com/a/21429652/4188998) inserted on the script – typescript Mar 01 '18 at 23:29

2 Answers2

1

Node lists can not be treated as arrays in older PHP versions (< 5.6.3).

Here are several approaches to avoid the problem.

Use the ->item() method

$titleNode = $xpath->query("span[@class='item_list']",$item)->item(0);

Use a limit in Xpath

foreach ($xpath->query("span[@class='item_list'][1]",$item) as $titleNode) {
  //...
}

This limits the nodes in the node list to the first one. The foreach() can be executed once for the first node or not if the Xpath expression fails to match a node. It is a nice shortcut to validate if here is a title node to handle.

Fetch the text content directly

$titleString = $xpath->evaluate("string(span[@class='item_list'])",$item);

Xpath expressions can return the result a scalar variable (string, number, boolean). This works only with DOMxpath::evaluate(). string() casts the first matched node into a string (returns its text content). It returns an empty string if no node was matched.

ThW
  • 19,120
  • 3
  • 22
  • 44
0

This answer solved my issue. The culprit came out when I turned on error reporting on the script and showed me this:

Fatal error: Cannot use object of type DOMNodeList as array

I referenced the error and learned that it was caused by the different PHP versions between my localhost and web host as I initially suspected.

typescript
  • 131
  • 1
  • 2
  • 12