Jump to content

Need help grabbing table data?


AquariaXI

Recommended Posts

So I'm trying to grab values from a specific table id in html in PHP possibly using `DOMElement` or something else? I want to add my index.php page to a variable, search for a certain table id  ( in this case, "shades" ), then grab JUST the values from inside that table & print it out in an echo.

Here's what i have. for some reason I'm getting

    Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity
<?php
    $HTML = [ ];
    $stream = fopen ( "index.php", "r" );
    $string = stream_get_contents ( $stream );
    HTML [ 0 ] = $string;
    // Our HTML goes here
    $innerHTML = implode ( ',', $HTML );
    // Create a new DOMDocument based on our HTML
    $document = new DOMDocument;
    $document -> loadHTML ( $innerHTML );
    // Get a NodeList of any table "id",  "shades"
    $cells = $document -> getElementsByTagName ( "id" );
    // Cycle over each <td>, adding its HTML to $innerHTML
    foreach ( $cells as $cell ) {
        $innerHTML = $document -> saveHTML ( $cell );
    }
    // Output our glorious HTML
    echo $innerHTML;
    fclose ( $stream );
?>



Any help is absolutely appreciated!
 

Edited by AquariaXI
Link to comment
Share on other sites

Yes & ONLY grab the value{s} between "<td id=\"shades\">DATA HERE</td>" - It is to grab the color hexes from my jquery hex array & echo them out in php. and NO I cannot build the table in PHP. the hex values are generated from jquery ONLY & are put inside the table with the id, "shades"

Edited by AquariaXI
Link to comment
Share on other sites

You're trying to grab data from your own site.

You have some PHP code responsible for outputting data into the table, right? Don't you think it would be easier to mimic that code that gets the data rather than do this complicated thing of executing the PHP, parsing the HTML, locating the correct table somewhere inside it, and extracting parts from it?

Link to comment
Share on other sites

i dont understand... T_T

I said the jquery is serving up the tds

$ ( "#shades" ).append (
    '<tr>' + 
        '<td class="hexcolor">' + 
            '<a style="background : ' + shadeHex + '"></a>' + 
        '</td>' + 
        '<td class="hexcode">' + shadeHex + '</td>' + 
    '</tr>'
);

Edited by AquariaXI
Link to comment
Share on other sites

function hex2rgb ( hex ) {
    hex = hex.replace ( '#', '' );
	r = parseInt ( hex.substring ( 0, 2 ), 16 );
    g = parseInt ( hex.substring ( 2, 4 ), 16 );
    b = parseInt ( hex.substring ( 4, 6 ), 16 );
	result = 'rgb(' + (
        r + ', ' + 
        g + ', ' + 
        b + ')'
    );
	return result;
}

function rgb2hex ( rgb ) {

    rgb = rgb.match (
        /^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i
    );

    return ( rgb && rgb.length === 4 ) ? "#" + 
        ( "0" + parseInt ( rgb [ 1 ], 10 ).toString ( 16 ) ).slice ( -2 ) + 
        ( "0" + parseInt ( rgb [ 2 ], 10 ).toString ( 16 ) ).slice ( -2 ) + 
        ( "0" + parseInt ( rgb [ 3 ], 10 ).toString ( 16 ) ).slice ( -2 ) : ""
	}

    for ( var i = 0; i < 10; i++ ) {
        var shadeHex = rgb2hex ( String (
             'rgb(' + 
                shades [ 9 - i ].rgb + 
            ')'
        ) );

	    var tintHex  = rgb2hex ( String (
            'rgb(' + 
                tints [ i ].rgb + 
            ')'
        ) );

        $ ( "#shades" ).append (
            '<tr>' + 
                '<td class="hexcolor">' + 
                    '<a style="background : ' + shadeHex + '"></a>' + 
                '</td>' + 
                '<td class="hexcode">' + shadeHex + '</td>' + 
            '</tr>'
        );

        $ ( "#tints"  ).append (
            '<tr>' + 
                '<td class="hexcolor">' + 
                    '<a style="background : ' + tintHex + '"></a>' + 
                '</td>' + 
                '<td class="hexcode">' + tintHex + '</td>' + 
            '</tr>'
        );

    }


this is my whole jquery code

Edited by AquariaXI
Link to comment
Share on other sites

There has to be more than that - where are "shades" and "tints" coming from?

And is there a particular reason this is all being done with Javascript? Is the user interacting with the page to change what values are being shown in the table?

Link to comment
Share on other sites

ding ding ding! CORRECT! :) The user DOES interact with the page to change what values are being shown in the table.

anyway, this is the table

<table class="color_frame">
    <tr>
        <td class="picker_box">
            <div class="color_wrapper">
                <div id="colorpickerHolder"></div>
                <div class="top_bar">
                    <div class="color_panel">
                        <!--Color Panel-->
                    </div>
                </div>
                <input type="button" value="&nbsp;" class="update_btn" />
            </div>
        </td>
        <td class="color_map"><img src="images/color_map.gif" alt="Shade and Tint map"  /></td>
        <td id="colors" class="shade_box">
            <table id="shades"></table>
            <table id="tints"></table>
        </td>
    </tr>
</table>

 

Edited by AquariaXI
Link to comment
Share on other sites

Okay... so if the table changes according to how the user interacts with the page, and you first said that you wanted PHP to get those values from the table, then how is PHP supposed to know how the user interacted with it?

Is your question more about how to send the values from the table to PHP? Maybe the user did something and now you want the results of whatever to be handled by some PHP code?
If that's the case then exactly what is the information you want? What is the user doing, anyway?

And one more thing. If you haven't noticed, posting little bits here and there just means I have to keep asking more and more questions...

Link to comment
Share on other sites

Correct. I just want to echo out the chosen hex values from within the "shades" td & "tints" td id tags

When the user interacts with the "Update" button, when they hit it, it changes the tints / shades hex values that are held within the "shades" id & "tints" id td tags.

Edited by AquariaXI
Link to comment
Share on other sites

Yeah, okay, that's the exact opposite of what I thought you wanted to do.

Put the table into a <form> and use hidden inputs to identify each value.

$ ( "#shades" ).append (
    '<tr>' + 
        '<td class="hexcolor">' + 
            '<a style="background : ' + shadeHex + '"></a>' + 
        '</td>' + 
        '<td class="hexcode">' +
            '<input type="hidden" name="shade[]" value="' + shadeHex or whatever + '">' +
            shadeHex +
        '</td>' + 
    '</tr>'
);

Submit the form normally or with AJAX and you'll have an array of shadeHex (or whatever) values.

Link to comment
Share on other sites

You have two options:

1. Use hidden inputs, like I said to do, so that the user can do whatever they want. I don't know what else they can do with the table. If anything - every single bit of information you've decided to reveal so far I've had to forcibly pry out of you and I'm not going to do that anymore. They do whatever with the table and cells, you use AJAX to send the current state of the form's "data" to PHP.
2. Don't bother with the hidden inputs because the user can't change anything once they hit whatever button. You have code that builds the table cells so it would be easy to (for example) insert those shadeHex values into an array as you go, then when the table is done you can AJAX that array to PHP.

Either way, the point is you are sending data to PHP rather than having it try to go out and get the data on its own (which isn't actually possible to do in this case anyways).

Link to comment
Share on other sites

Isn't there a way to use

	<?php

    // Create a new DOM Document
	$dom = new DOMDocument('1.0', 'iso-8859-1');
	  
	// Enable validate on parse
	$dom->validateOnParse = true;

    // Create a div element
	$element = $dom->appendChild(new DOMElement('div',
	   'Hey, this is the text content of the div element.'));
	  
	// Create a id attribute to div
	$attr = $element->setAttributeNode(
	          new DOMAttr('id', 'my_id'));
	  
	// Set that attribute as id
	$element->setIDAttribute('id', true);
	  
	// Get the tag content
	$tagcontent = $dom->getElementById('my_id')->textContent;
	  
	echo $tagcontent;

?>
	

? except i don't want a div, i want the td's "shades" & "tints"

Edited by AquariaXI
Link to comment
Share on other sites

No, no, the point of what I said not about me doing the work. The part of that post you skipped over was "for the same reason", meaning I was trying to demonstrate the "that will not work" point using an example.

I'll try some different examples, okay?

Having PHP access the user's current version of the webpage at all - forget DOMDocument, that has nothing to do with any of this - will not work for the same reason as why you cannot
1. Fill your car's gas tank while you sit at home (because the gas is at the gas station and you are not)
2. Pull a turkey out of the oven while you are filling up your car's gas tank (because the the oven at home and you are not)
3. Climb Mt. Everest with a hot turkey you just pulled out of your oven (because Mt. Everest is in a remote location in Asia and your oven is not)

Do you see the pattern here? The HTML table you want to access is located inside your user's browser while the PHP code is running on the server. To exchange data you have to exchange the data, meaning the client's browser (where the data currently is) has to be sent the information about the table/hex colors to the PHP server (where you want the data to be).

Sending information from the browser to the server is done by either (a) using HTML forms, which you said you don't want to do, therefore forcing you into the only other option of (b) using Javascript and AJAX.

Link to comment
Share on other sites

This makes absolutely no sense to me. I learn by example.

I simply want to use

// Find all <td> whose attribute id="BLAH"
$doc = new DOMDocument ( );
$doc -> loadHTMLFile (
    'index.php'
);
$child = $doc -> getElementsByTagName ( 'td' );
foreach ( $child as $td ) {
    if ( $td -> hasAttribute ( 'id' ) ) {
        echo $td -> attributes -> getNamedItem ( 'id' ) -> nodeValue . '<br>\r\n';
    }
}

Now, this returns "colors" which is the id. not the value OF colors. It also comes with a warning

Quote

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: no name in index.php



It at least SEEMS to me at the moment at least, that you are simply trying to avoid the answer.

Edited by AquariaXI
Link to comment
Share on other sites

On 5/28/2021 at 4:33 PM, AquariaXI said:

The user DOES interact with the page to change what values are being shown in the table.

^^^ this is the upfront information that should have been included in the first post in the thread, i.e. what top level thing you are actually trying to accomplish.

your reactive, after the fact, attempted solution to use DOMDocument, in the php code on the web server, WON'T work because the web server doesn't have direct access to the user modified DOM that only exists in the user's browser. your attempt at opening and reading the index.php FILE, was both misleading and would read the raw content of that file. even if you used a http wrapper and requested the index.php page from your own web server, you would only get the initial DOM, that the web server sends out to the user's browser.

repeating now what @requinix has stated, the only way of getting anything from the browser to the web server requires that you make a http(s) request to the web server that contains the information that you want. you will need to pick some point in the code in the browser where the value(s) have been changed, then get those value(s) and include them in a http(s) request to the web server.

Edited by mac_gyver
Link to comment
Share on other sites

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.