In this section, let's take a look at how to generate HTML files using core PHP
Recently, when I was querying php.net, I found domdocument this class very interesting, can be used to generate XML or HTML files, DOMDocument provides us with a series of methods to generate xml/html tags and insert into the DOM, now let Let's take a look at how to generate
Let's take a look at the results that are generated by the method it provides, as shown in the following figure:
First, create a new DOM file
Copy Code code as follows:
Instantiate the DOMDocument class and specify the version number
$dom = new DOMDocument (' 1.0 ');
Output the generated label or code to the page
echo $dom->savehtml ();
Second, add new HTML elements to the DOM file
Copy Code code as follows:
$css _text = ' p{color: #ff00ff;} ';
Create a new style label and CSS content
$style = $dom->createelement (' style ', $css _text);
Add the style label to the DOM file
$dom->appendchild ($style);
The output effect is as follows
<style>
P{color: #ff00ff;}
</style>
Here is the CreateElement method, when you want to create <style> tag and write CSS, you can use the method's second parameter as CSS content, as shown above. But if you want to create a <br> tag, the second argument can be omitted as follows:
Copy Code code as follows:
Create a new <br> label
$BR = $dom->createelement (' BR ');
Add the <br> tag to the DOM file
$dom->appendchild ($BR);
adding attributes to HTML elements
HTML elements have a variety of attributes that can be used to add attributes to the CreateAttribute () method
Copy Code code as follows:
$css _text = ' p{color: #ff00ff;} ';
Create a new style label and CSS content
$style = $dom->createelement (' style ', $css _text);
Create a new property ' type '
$domAttribute = $dom->createattribute (' type ');
Add value for property ' type '
$domAttribute->value = ' text/css ';
Add this property to the style label
$style->appendchild ($domAttribute);
Add the style label to the DOM file
$dom->appendchild ($style);
The output effect is as follows
<style type= "Text/css" >
P{color: #ff00ff;}
</style>
Copy Code code as follows:
$p _text = ' This is a paragraph ';
Create a new P tag and content
$p = $dom->createelement (' P ', $p _text);
Create a new property ' ID '
$domAttribute = $dom->createattribute (' id ');
Add value for attribute ' ID '
$domAttribute->value = ' description ';
Add this property to the P tag
$p->appendchild ($domAttribute);
Add the P tag to the DOM file
$dom->appendchild ($p);
The output effect is as follows
<p id= "description" >
One day
</p>
Iv. Add Form Element
Add textbox
Copy Code code as follows:
$input = $dom->createelement (' input ');
$domAttribute = $dom->createattribute (' type ');
$domAttribute->value = ' text ';
$input->appendchild ($domAttribute);
$domAttribute = $dom->createattribute (' name ');
$domAttribute->value = ' e-mail ';
$input->appendchild ($domAttribute);
$dom->appendchild ($input);
The output effect is as follows
<input type= "text" name= "e-mail" >
v. Create a Table
Copy Code code as follows:
$table = $dom->createelement (' table ');
$domAttribute = $dom->createattribute (' id ');
$domAttribute->value = ' my_table ';
$TR = $dom->createelement (' tr ');
$table->appendchild ($TR);
$TD = $dom->createelement (' TD ', ' Label ');
$tr->appendchild ($TD);
$TD = $dom->createelement (' TD ', ' Value ');
$tr->appendchild ($TD);
$table->appendchild ($domAttribute);
$dom->appendchild ($table);
The output effect is as follows
<table id= "My_table" >
<tbody>
<tr>
<td>Label</td>
<td>Value</td>
</tr>
</tbody>
</table>
Finally, we'll have a
a complete and complex example:
Copy Code code as follows:
$dom = new DOMDocument (' 1.0 ');
CSS Content
$css _text = ';
$css _text. = ' body{width:285px;margin:auto;margin-top:50px;} ';
$css _text. = ' #my_table {border:1px solid #ececec;} ';
$css _text. = ' #my_table th{border:1px solid #ececec;p adding:5px;text-decoration:underline;} ';
$css _text. = ' #my_table td{border:1px solid #ececec;p adding:5px;} ';
$css _text. = ' #my_table td:first-child{text-align:right;color: #333333; Font-weight:bold;color: #999999;} ';
Create a new style label and CSS content
$style = $dom->createelement (' style ', $css _text);
Create a new property ' type '
$domAttribute = $dom->createattribute (' type ');
Add value for property ' type '
$domAttribute->value = ' text/css ';
Add this property to the style label
$style->appendchild ($domAttribute);
Add the style label to the DOM file
$dom->appendchild ($style);
Add form
$form = $dom->createelement (' form ');
$dom->appendchild ($form);
$formAttribute = $dom->createattribute (' method ');
$formAttribute->value = ' post ';
$form->appendchild ($formAttribute);
Add Table
$table = $dom->createelement (' table ');
$tableAttribute = $dom->createattribute (' id ');
$tableAttribute->value = ' my_table ';
$table->appendchild ($tableAttribute);
Add a new row (row)
$TR = $dom->createelement (' tr ');
$table->appendchild ($TR);
Add new Columns (column)
$th = $dom->createelement (' th ', ' Generate HTML using PHP ');
$tr->appendchild ($th);
$thAttribute = $dom->createattribute (' colspan ');
$thAttribute->value = ' 2 ';
$th->appendchild ($thAttribute);
Add a new row (row)
$TR = $dom->createelement (' tr ');
$table->appendchild ($TR);
Add new Columns (column)
$TD = $dom->createelement (' TD ', ' Name ');
$tr->appendchild ($TD);
Add new Columns (column)
$TD = $dom->createelement (' TD ');
$tr->appendchild ($TD);
Adding input elements to columns (column)
$input = $dom->createelement (' input ');
$TD->appendchild ($input);
$tdAttribute = $dom->createattribute (' type ');
$tdAttribute->value = ' text ';
$input->appendchild ($tdAttribute);
$tdAttribute = $dom->createattribute (' name ');
$tdAttribute->value = ' f_name ';
$input->appendchild ($tdAttribute);
Add a new row (row)
$TR = $dom->createelement (' tr ');
$table->appendchild ($TR);
Add new Columns (column)
$TD = $dom->createelement (' TD ', ' Email ');
$tr->appendchild ($TD);
Add new Columns (column)
$TD = $dom->createelement (' TD ');
$tr->appendchild ($TD);
Adding input elements to columns (column)
$input = $dom->createelement (' input ');
$TD->appendchild ($input);
$tdAttribute = $dom->createattribute (' type ');
$tdAttribute->value = ' text ';
$input->appendchild ($tdAttribute);
$tdAttribute = $dom->createattribute (' name ');
$tdAttribute->value = ' e-mail ';
$input->appendchild ($tdAttribute);
Add a new row (row)
$TR = $dom->createelement (' tr ');
$table->appendchild ($TR);
Add new Columns (column)
$TD = $dom->createelement (' TD ', ' Gender ');
$tr->appendchild ($TD);
Add new Columns (column)
$TD = $dom->createelement (' TD ');
$tr->appendchild ($TD);
Adding input elements to columns (column)
$select = $dom->createelement (' select ');
$TD->appendchild ($select);
$tdAttribute = $dom->createattribute (' name ');
$tdAttribute->value = ' gender ';
$select->appendchild ($tdAttribute);
Add options for the Select drop-down box
$opt = $dom->createelement (' option ', ' Male ');
$domAttribute = $dom->createattribute (' value ');
$domAttribute->value = ' male ';
$opt->appendchild ($domAttribute);
$select->appendchild ($opt);
$opt = $dom->createelement (' option ', ' Female ');
$domAttribute = $dom->createattribute (' value ');
$domAttribute->value = ' female ';
$opt->appendchild ($domAttribute);
$select->appendchild ($opt);
Add a new row (row)
$TR = $dom->createelement (' tr ');
$table->appendchild ($TR);
Add new Columns (column)
$TD = $dom->createelement (' TD ', ' interest ');
$tr->appendchild ($TD);
Add new Columns (column)
$TD = $dom->createelement (' TD ');
$tr->appendchild ($TD);
Adding input elements to columns (column)
$radio = $dom->createelement (' input ');
$TD->appendchild ($radio);
$radAttribute = $dom->createattribute (' type ');
$radAttribute->value = ' Radio ';
$radio->appendchild ($radAttribute);
$radAttribute = $dom->createattribute (' name ');
$radAttribute->value = ' interest ';
$radio->appendchild ($radAttribute);
$radAttribute = $dom->createattribute (' id ');
$radAttribute->value = ' php ';
$radio->appendchild ($radAttribute);
$label = $dom->createelement (' label ', ' PHP ');
$labelAttribute = $dom->createattribute (' for ');
$labelAttribute->value = ' php ';
$label->appendchild ($labelAttribute);
$TD->appendchild ($label);
$radio = $dom->createelement (' input ');
$TD->appendchild ($radio);
$radAttribute = $dom->createattribute (' type ');
$radAttribute->value = ' Radio ';
$radio->appendchild ($radAttribute);
$radAttribute = $dom->createattribute (' name ');
$radAttribute->value = ' interest ';
$radio->appendchild ($radAttribute);
$radAttribute = $dom->createattribute (' id ');
$radAttribute->value = ' jquery ';
$radio->appendchild ($radAttribute);
$label = $dom->createelement (' label ', ' JQuery ');
$labelAttribute = $dom->createattribute (' for ');
$labelAttribute->value = ' jquery ';
$label->appendchild ($labelAttribute);
$TD->appendchild ($label);
Add a new row (row)
$TR = $dom->createelement (' tr ');
$table->appendchild ($TR);
Add new Columns (column)
$TD = $dom->createelement (' TD ');
$tr->appendchild ($TD);
$tdAttribute = $dom->createattribute (' colspan ');
$tdAttribute->value = ' 2 ';
$TD->appendchild ($tdAttribute);
Adding input elements to columns (column)
$input = $dom->createelement (' input ');
$TD->appendchild ($input);
$tdAttribute = $dom->createattribute (' type ');
$tdAttribute->value = ' Submit ';
$input->appendchild ($tdAttribute);
$tdAttribute = $dom->createattribute (' value ');
$tdAttribute->value = ' sign-up ';
$input->appendchild ($tdAttribute);
Add table to form
$form->appendchild ($table);
echo $dom->savehtml ();