einstein
einstein

Reputation: 13850

HTML DOM manipulation in PHP

I wonder how I can manipulate the DOM tree using PHP?

I have seen some answers with XML DOM that loads in a html file. But what if I don't need to load? What if have XML DOM scripts inside the document I want to manipulate?

I have an example below that prints out all the folders. Fill in the blanks in your answers. I want to create div-elements with the folder's name as text node. The answer need to have some XML DOM scripts because I will create more elements than just one div-element in my website. And using for example echo is not practical, because you might insert an element inside the wrong element etc.

$sql = "
    SELECT name
    FROM folders
    WHERE profileId = '$profileId'
";
$result = mysql_query($sql) or die('Error6: '.mysql_error());
while($row = mysql_fetch_array($result)) {

}

Upvotes: 3

Views: 22749

Answers (6)

Greg
Greg

Reputation: 21909

I find using PHP's inbuilt DOM implementation (libxml) very clunky due to it only supporting DOM 2. I prefer using phpgt/dom (https://github.com/phpgt/dom) available through composer.

Usage example:

$document = new \phpgt\dom\HTMLDocument(
    "<!doctype html><h1>My list of amazing things</h1> <ul class='my-list'></ul>"
);

$result = $database->query("SELECT thing FROM Amazing");
$ul_output = $document->querySelector("ul.my-list");

foreach($result as $row) {
    $li = $document->createElement("li");
    $li->textContent = $row["thing"];
    $ul_output->appendChild($li);
}

echo $document->saveHTML();

Full documentation available on the project's Github wiki: https://github.com/phpgt/dom/wiki

Upvotes: 1

Ruan Mendes
Ruan Mendes

Reputation: 92274

If you're thinking that PHP creates a DOM as you echo HTML, that does not happen. However, you can use Marc B samples to generate DOM nodes (that you can append to). After you've manipulated that node,you can then echo the nodes HTML using http://www.php.net/manual/en/domdocument.savehtml.php

Upvotes: 0

brian_d
brian_d

Reputation: 11385

For PHP driven DOM manipulation try phpQuery. If you are familiar with jQuery, the similar syntax makes it a good library to use.

Upvotes: 3

Marc B
Marc B

Reputation: 360692

You don't need PHP to manipulate the dom of a document you're generating already. It's by far easier to just have PHP directly generate some HTML. DOM's there to disect/change HTML that was generated elsewhere. In other words, your script would just be:

while($row = mysql_fetch_array($result)) {
    echo "<div>{$row['name']}</div>";
}

DOM example, to demonstrate the tediousness

Ok, here's the PHP method to generate a paragraph of text with some internal spans and whatnot:

echo "<div> This is my div, <span>There are many like it, <b>but this one</b> is</span> mine</div>";

The equivalent DOM calls: (not tested, just to demonstrate):

$dom = new DOM;

$bold = new DOMElement('b');
$bold->appendChild(new DOMText('but this one'));

$span = new DOMElement('span');
$span->appendChild(new DOMText('There are many like it,'));
$span->appendChild($bold);
$span->appendChild(new DOMText(' is');

$div = new DOMElement('div');
$div->appendChild(new DOMText(' This is my div,'));
$div->appendChild($span);
$div->appendChild(' mine');

echo $div->saveXML();

So....... still thing using DOM is easier than using echo?

Upvotes: 6

dunos
dunos

Reputation: 755

Technically this is pre DOM since you are building the page in HTML prior to outputting it to the client at which point it becomes the document object model within the context of the client. Therefore your DOM manipulation is effectively just HTML generation in PHP on the server side.

<html>
<head>
</head>
<body>

<?php
$sql = "
SELECT name
FROM folders
WHERE profileId = '".mysql_escape_string($profileId)."'
";
$result = mysql_query($sql) or die('Error6: '.mysql_error());
while($row = mysql_fetch_array($result)){
  echo "<div>".htmlentities($row['name'])."</div>";
}
?>
</body></html>

I have also added mysql_escape_string to your sql statement to help protect from sql injection attacks while using htmlentities on the echo output to help prevent script injection into the page.

Upvotes: 0

Cem Kalyoncu
Cem Kalyoncu

Reputation: 14593

I hope I understood correctly.

$result = mysql_query($sql) or die('Error6: '.mysql_error());
while($row = mysql_fetch_array($result)){
   ?><div><?php echo $result[0]; ?></div><?php
}

If this is not what you seek please detail your question by adding what are you trying to achieve.

Upvotes: 0

Related Questions