Charvusta Posted October 1, 2017 Share Posted October 1, 2017 I'm trying to output only the first column in this table, do I have to add another getElementsByTagName(); this is my code, <?php function tdrows($elements) { $str = null; foreach ($elements as $element) { $str .= $element->nodeValue . ", "; } return $str; } function getdata() { $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>"; $DOM = new DOMDocument; $DOM->loadHTML($contents); $items = $DOM->getElementsByTagName('tr'); foreach ($items as $node) { echo tdrows($node->childNodes) . "<br />"; } } //tdrows(); getdata(); ?> The output is Cheers Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/ Share on other sites More sharing options...
ginerjm Posted October 1, 2017 Share Posted October 1, 2017 I"m confused. You ask how to only show the first column, but then you show an example with two columns. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552199 Share on other sites More sharing options...
Charvusta Posted October 1, 2017 Author Share Posted October 1, 2017 Lol, yeah, i only want to show column 1, does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552201 Share on other sites More sharing options...
Charvusta Posted October 1, 2017 Author Share Posted October 1, 2017 (edited) $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>"; Is this possible? Have a little bit of regex experience from another language. Cheers Edited October 1, 2017 by requinix merged Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552206 Share on other sites More sharing options...
requinix Posted October 1, 2017 Share Posted October 1, 2017 (edited) With difficulty, sure. A better approach is to load the string into DOM and locate the column with its tools. $dom = new DOMDocument(1.0, "utf-8"); $dom->loadHTML($contents); // if $contents only has the one <table> and you want the first <td> from the first/only <tr> $td = $dom->getElementsByTagName("td"); if ($td->length) { $value = $td[0]->textContent; } else { // error } Edited October 1, 2017 by requinix merged 1 Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552207 Share on other sites More sharing options...
Charvusta Posted October 1, 2017 Author Share Posted October 1, 2017 I'm playing around with this similar example i found Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552208 Share on other sites More sharing options...
requinix Posted October 1, 2017 Share Posted October 1, 2017 One thread at a time, please. Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552209 Share on other sites More sharing options...
Charvusta Posted October 2, 2017 Author Share Posted October 2, 2017 With difficulty, sure. A better approach is to load the string into DOM and locate the column with its tools. $dom = new DOMDocument(1.0, "utf-8"); $dom->loadHTML($contents); // if $contents only has the one <table> and you want the first <td> from the first/only <tr> $td = $dom->getElementsByTagName("td"); if ($td->length) { $value = $td[0]->textContent; } else { // error } Wow thats a great way using if length, thanks Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552227 Share on other sites More sharing options...
Charvusta Posted October 2, 2017 Author Share Posted October 2, 2017 This is what i done to my code to get it working <?php function getdata() { $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>"; $DOM = new DOMDocument; $DOM->loadHTML($contents); $table = $DOM->getElementsByTagName('table'); $rows = $DOM->getElementsByTagName('tr'); foreach ($rows as $row) { $cols = $row->getElementsByTagName('td'); echo $cols->item(0)->nodeValue.'<br />'; } } getdata(); ?> output is now Quote Link to comment https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/#findComment-1552230 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.