Jump to content

Echo Just The First Column in Table


Charvusta

Recommended Posts

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

 

07dfdbdd30066bf4d9ae3b31d3714568.png

 

Cheers

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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
}
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 

 

da0a370b4e1a4c031a38d89ce268f345.png

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.