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

3
  • 2
    You need to add your code to the question. Explain where it doesn't work, and what it should be doing. Commented Jul 16, 2013 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: stackoverflow.com/questions/3577641/… Commented Jul 16, 2013 at 13:02
  • question edited, please have a look at it Commented Jul 16, 2013 at 13:05

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.