Jump to content

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
https://forums.phpfreaks.com/topic/305169-echo-just-the-first-column-in-table/
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

Edited by requinix
merged

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 by requinix
merged
  • Like 1

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

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

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.