1

Hello I want to parse a HTML table and assign those values to php variables so that i can insert them in to mysql database

I have tried some html parsing methods like dom_html_parsing but as a beginer i am getting much confused, i would be gald if some provides me some hint on this so that i can code

Parsing code i used is

 include('simple_html_dom.php');
 $dom = str_get_html($result);
 $table = array();

 $html = str_get_html($result);
 foreach($html->find('tr') as $row) {
 $time = $row->find('td',-1)->plaintext;
 $title = $row->find('td',0)->plaintext;
 $title0 = $row->find('td',1)->plaintext;
 $title1 = $row->find('td',2)->plaintext;
 $title2 = $row->find('td',3)->plaintext;
 $title3 = $row->find('td',4)->plaintext;
 $title4 = $row->find('td',5)->plaintext;
 $title5 = $row->find('td',6)->plaintext;

$table[$title][$title0][$title1][$title2][$title3][$title4][$title3] = true;
}

echo '<pre>';
print_r($table);
echo '</pre>';

the arrays are printing but i do not know how to insert those particular values in to mysql database, i want to assign those values to variables first so that i can insert in to the database and the format i need is shown above, name and fathername & htno are printed only once in html table but i need it to be repeated with each row of table

Please help me

  • 2
    You need to add your code to the question. Explain where it doesn't work, and what it should be doing. – andrewsi Jul 16 '13 at 12:59
  • I suggest you read this and try to figure it out, then try something and come back asking for help with specific problems you have: http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php?rq=1 – SharkofMirkwood Jul 16 '13 at 13:02
  • question edited, please have a look at it – user2501866 Jul 16 '13 at 13:05

2 Answers2

1
$table_data = array();

$dom = new DOMDocument();
$dom->loadHTML($html_string);

$rows = $dom->getElementsByTagName('tr');
for ($i = 0; $i < $rows->length; $i++) {
    $cells = $rows->item($i)->getElementsByTagName('td');
    for ($j = 0; $j < $cells->length; $j++) {
        $table_data[$i][$j] = $cells->item($j)->textContent;
    }
}

//print_r($table_data);
shasi kanth
  • 6,987
  • 24
  • 106
  • 158
lePunk
  • 523
  • 3
  • 10
0

You can use phpquery. It's similar to jQuery, but for PHP.

Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43