Tipo Posted February 4, 2014 Share Posted February 4, 2014 (edited) 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 February 4, 2014 by Tipo Quote Link to comment Share on other sites More sharing options...
requinix Posted February 4, 2014 Share Posted February 4, 2014 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. Quote Link to comment Share on other sites More sharing options...
Tipo Posted February 4, 2014 Author Share Posted February 4, 2014 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 4, 2014 Share Posted February 4, 2014 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 } Quote Link to comment 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.