Get Data from HTML Table that is stored in php variable

I have a html form, on pressing submit button some data [html tables] are fetched from another webpage and stored in a php variable… now i want data from each cell in this html table as i want to store this into my mysql db… i am stuck on how to get the data from the html table which is stored in the php var $response

on echo $response we get

<table>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>

<tr>
<td>data11</td>
<td>data22</td>
<td>data33</td>
</tr>

<tr>
<td>data111</td>
<td>data222</td>
<td>data333</td>
</tr>

</table>

<table>
<tr>
<td>some data1</td>…
</tr>
</table>
im just a beginer so pls explain ur answer as far as possible [final code]…
Thanks in advance…

no replies ??
atleast can someone give the code to get particular column values from the table ??

You probably want to use a HTML parsing library, like this one: http://simplehtmldom.sourceforge.net/. It gives you a simple interface for extracting data from web pages.

Thanks for the reply…
i saw that page before but didnt get…
actually the external page from where i am extracting data is having a html form ,
after enterin some value in the form,
then on click the submit button some html tables are generated that tables i have stored in php var…
dont know how to acheive this through ur method…

You can do something like this:


include('simple_html_dom.php');

$html = str_get_html($string); // Parse the HTML, stored as a string in $string
$rows = $html->find('tr'); // Find all rows in the table

//Loop through each row
foreach ($rows as $row) {
    //Loop through each child (cell) of the row
    foreach ($row->children() as $cell) {
        echo $cell->plaintext; // Display the contents of each cell - this is the value you want to extract
    }
}

@fretburner thanks for ur interest in solving my prob…
was trying to get the value using table.rows[r].cells[c].childNodes[0].value; inside loops…
ur code should also get the values …

but the thing is the php variable contains more than one table and i need only two or three table values…[mostly the first three tables]
is der a way to target particular table ?? [the tables doesnt id’s to distinguish them and all are named as table]

Thanks again…

According to the docs, if the tables have IDs, you can select them like this:

$tables = $html->find('table[id=foo]');

If not, you could select all the tables on the page, and access the first three using an index (like an array):


$tables = $html->find('table');
$tables[0]->find('tr'); //All rows of first table
$tables[1]->find('tr'); //All rows of second table
//etc.

sorry bro getting this error

Fatal error: Call to a member function find() on a non-object

i first tried ur code and den modified lil bit and tried this code below [going object oriented way] but still the same error…

include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load('<html><body>$response</body></html>');
$tables = $html->find('table');
$rows = $tables[0]->find('tr'); //All rows of first table

//Loop through each row
foreach ($rows as $row) {
    //Loop through each child (cell) of the row
    foreach ($row->children() as $cell) {
        echo $cell->plaintext;
    }
}

and also i would like to know is this code light weight wrt cpu usage, ram?
i will be using this occassionally [once or twice in a few months] but when using this code it will be executed 100s of time…
im in shared server in hostgator…

Your problem is here:

$html->load('<html><body>$response</body></html>');

You’re using single quotes around the string, so php isn’t inserting the contents of the variable $response… it’s just treating the name as a string.
You want to be doing this:

$html->load($response);

You don’t need to add html and body tags either, as the library can parse html fragments as well as complete documents.

Not sure about the performance… that’s something you might want to do some tests for. The docs do suggest that if you’re going to be loading multiple html documents in a script, that you want to free up the memory like this, to avoid memory leaks:


$html = file_get_html(...);
// do something...
$html->clear();
unset($html);

at first i tried ur code
$html = str_get_html($string); …
i was trying the same with html and body tags… i saw it somewhere while googl and as in my variable der was only tables i included that…
now after u said removed that in str_get_html and its working… woow [did few small changes in between so cant say the error]
nw have to sort the data to insert in mysql…
but super thanks bro seriously thanks a lot…

if we have html in 4 or 5 php strings , if there a way to process all of them at the same time ??


$html1 = str_get_html($a1);
$html2 = str_get_html($a2);
$html3 = str_get_html($a3);
$html4 = str_get_html($a4);


$tables = $html1->find('table');
$rows = $tables[0]->find('tr'); //All rows of first table
//Loop through each row 
foreach ($rows as $row) { 
    //Loop through each child (cell) of the row 
    foreach ($row->children() as $cell) { 
        echo $cell->plaintext; 
    } 
}
[jst an example taken frm above, my code is complicated with many more stmts, sql...]


the way to process all the four string are exactly or almost same…
so my question is there a way to run the above code parallel for all the four string ??
or whats d best way …
Thanks

found a way by using foreach loop… mostly it should work…
Thanks