Jump to content

Recommended Posts

How do I know which query hits my HTML DOM?

Or how can I separate the results to each query?

$xpath = new DomXPath($dom);

$nodes = $xpath->query("//tr/td/div[@class='category']/a[1]/text() | 
                        //tr/td[@id='data']/div[@class='category_data']/text() | 
                        //tr/td[@id='data']/div[@class='category']/text() | 
                        //tr/td[@id='data']/div[@class='category']/span[@class='category_data']/text()");

header("Content-type: text/plain");

foreach ($nodes as $i => $node) {
    if (  ) // Query 1: //tr/td/div[@class='category']/a[1]/text()
    { output the content here } 
    if (  ) // Query 2: //tr/td[@id='data']/div[@class='category_data']/text()
    ... and so on
    
    echo "Node($i): " . ltrim(rtrim($node->nodeValue)) . "<br> --> " . $node->nodeName . "\n";
}

$node->nodeName seems to be wrong.

I also tried other option from PHP.net but maybe it isn't possible.

Edited by Tipo
Link to comment
https://forums.phpfreaks.com/topic/285930-how-to-split-results-from-xpath-query/
Share on other sites

Do separate queries. Because the alternative is looking at each node and figuring out which of the patterns it matches. And that's just silly since you had DOMXPath do all that work for you already only a second earlier.

Ok, but how can I run several queries at once?

My problem is that I have to combine the results.

For example the first pattern shows me the "headline", while the rest fetch the content related to that "headline".

So when I create the output I would like to know:

--> this is my headline 1 --> and this my content to this headline

--> this is my headline 2 --> and so on

 

In simple steps:

1. Find headline x

2. Find content for headline x

3. return to step 1 and find next headline

 

With my 4 patterns I will get every information I need but now I can't separate them.

 

Do you have an example how to write more queries and run them while the first query is active?

Is it possible to run subqueries? That would solve my problem immediately. :happy-04:

You keep trying this idea of yours of executing multiple queries at once, realizing it's not working, and then insisting that you have to do it with multiple queries. You don't, if for the simple reason that it won't work.

 

Run a query to get the "root" headline elements. Loop over that. Inside the loop you can get the individual bits of data you want.

foreach headline as $headline {
	$category = get the category from the $headline
	$text = get the text from the $headline
	output whatever you want
}
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.