Important alert: (current site time 7/15/2013 9:48:56 AM EDT)
 

article

Remove HTML table rows using DOM

Email
Submitted on: 8/14/2012 9:26:20 AM
By: Aleksandar Andrijevic 
Level: Intermediate
User Rating: Unrated
Compatibility: PHP 5.0
Views: 3849
author picture
(About the author)
 
     This function will help you if you have some HTML table(for example the description of something) when you need to show same data in different places-structures.It crops table rows based on how many rows to leave.My practical application of this was when I had description of product on some page, in product details I showed full table with all rows, and in products list i showed only first three rows of same table.I hope someone helps.Greetz!!! 8)

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				' . $html;
	$dom = new DOMDocument('1.0', 'windows-1250');
	$dom->substituteEntities = true;
	$dom->loadHTML($html);
	$dom->preserveWhiteSpace = false;
	//We get the tag element we want to be removed
	$elements = $dom->getElementsByTagName('tr');
	//We put all elements in array
	$tags = array();
	foreach($elements as $tag) {
	$tags[] = $tag;
	}
	//If we for example don't want to remove first 2-3 rows, we pass the parametar row
	//for how many rows to leave from top(this can be reversed to begin from down of course)
	for ($i=0; $i<=$rows-1; $i++){
 		unset($tags[$i]);
 	}
	
	//We remove the remaining rows
	foreach($tags as $tag) {
	$tag->parentNode->removeChild($tag);
	}
	$html = $dom->saveHTML();
	return $html;
}
//Generate the table
$some_html_table = '';
for ($i = 1; $i <= 15; $i++) {
	$some_html_table .= '';
	}
$some_html_table .= '
Row'.$i.':Value'.$i.'
'; ?> This is a normal table

This is a cropped table


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.