FREE Php Cgi Website Scripts
Resources, Snippets and Tutorials
Try out some Websites

Edit a webpage table with Php.

Webpage interactive html table content editor.

Hi again!. I have just added this cool little variation of a html table editor I did for a client to edit order pages that are in a html page format. This is a modified and a simple version as an example on how its done.

Basic run down we use preg match to grab all td cell content in an array and load into a table form with text areas. Then we modify content post the content and preg match again the tds and replace post array with the table cell array. Its easy I also have the script to edit any html tag webpage content too! Try it out below. You can only edit this page table content and text is limited to 50 characters. You know why!

Hope you like it and find it useful!

Webpage table editing demo

An example of one way to edit a table in a webpage using php only.

2 column span td cell content
row 1 column td cell 1 content
row 1 column td cell 2 content
row 2 column td cell 1 content

Edit table

  1: <?php
2:
//function to clean data and reduce the string for demo
3:     
function clean($text)
4: {
5:     
$text strip_tags($text);
6:     
$text htmlspecialchars($textENT_QUOTES);
7:     
$text trim($text);
8:     
//removes more than one space
9:     
$text=trim(preg_replace('/(\s)\s+/'' '$text));
10:     
//alpha num space only 
11:     
$text=trim(preg_replace("/[^A-Za-z0-9\s]/"" ",$text));
12:     
// cuts string to 100
13:     
$text=substr($text,0,50);
14:     return (
$text); //output clean text
15:
}
16:
//set variable
17:
$getfile='';
18:
//get file page string
19:
if (isset($_GET['getfile'])){
20:
21:     
$filename=trim($_GET['getfile']);
22:     
23:
//demo we set 1 file to read only    
24: //    $allowed='95941344.php';
25: //if(file_exists($filename) && $allowed === $filename){
26:
if(file_exists($filename)){
27:  
$contents file_get_contents($filename);
28:
$contents utf8_encode($contents);
29:
$string =$temp_filestr_replace(array("\r","\r\n","\n\r","\n"),"\n",$contents);
30:
$getfile='1';
31: }
32: }
33:
//echo $string ;
34: //get file name editing
35:
if (isset($_GET['edit'])) {
36:  
$orderfile=trim($_GET['edit']);
37:
//array map clean post data
38:  
$data array_map('clean',$_POST);
39:  
40:
//set the td tag for preg match
41:
$value='td';
42:
43:
44:  
$results $newdata = array();
45: if(
file_exists($orderfile)){
46:
$content=file_get_contents($orderfile);
47:
$content str_replace(array("\r","\r\n","\n\r","\n\n"),"\n",$content);
48:
$string =$content;
49:
//preg match array getting the old td content
50:     
preg_match_all("#(<$value.*?>)(.+?)(</$value>)#",$string,$datas);
51:
52:     
$html =$string;
53:
//replace preg td array with post array data
54: //the count of each matches and data same preg match patterns the same
55:     
$html =str_ireplace(array_values($datas[2]), array_values($data), $html);
56:
//table edited back to webpage
57:
if(file_put_contents($orderfile,$html)){
58:
header("Location: $orderfile");
59: }
60: }else{
61: exit;    
62: }
63: }
64:
65:
66:
67:
//if we got file string
68:
if($getfile) {
69:
70: echo 
' <!doctype html><html lang="en"><head>
71: <title>edit table with php</title><meta charset="utf-8">
72: </head><body>
73: <h2 style="text-align: left;">Webpage table editing</h2>
74: <p style="text-align:left;">An example of one way to edit a table in a webpage using php only.<br> See the script to edit any html tag content in a webpage too!</p>'
;
75:
76:
77:
//the tag for preg match
78:
$value='td';
79:
//exlode to get table rows as lines
80:
$end='</tr>';
81:
//key to verify line with strpos
82:
$word '</td>';
83:
84:
85:
//we are returning table data so we explode</tr>
86:
$lines explode($end$string);
87:
88:
//found line to array
89:
$found='';
90:
$line_nums = array();
91: foreach(
$lines as $line){
92:   if(
strpos($line$word)!==false) {
93:    
$line_nums[] = $line;
94:  
$found='1';
95:  }
96: }
97:
98:
99:
100:
$i 0;
101:
//if we got lines
102:
if($found) {
103: echo 
"<form method=\"post\" action=\"?edit=$filename\">\n"
104: echo 
'<table style="border:10px solid RoyalBlue;-webkit-border-radius:13px;-moz-border-radius:13px;-ms-border-radius:13px;-o-border-radius:13px;border-radius:13px;"><tbody>';
105:
// go thru each line  
106:    
foreach($line_nums as $line){
107:
//makes tr rows
108:
echo '<tr>';
109:
//match td and get contents of cell
110:
if(preg_match_all("#(<$value.*?>)(.+?)(</$value>)#"$line$foo)){
111:    
$count=count($foo[2]);
112:   
$counter 0;
113:    foreach(
$foo[2] as $foo1){
114:
//for each cell we make td columns
115: //each textarea has a unique name value using counter values
116:      
echo "<td><textarea name=\"edit$i$counter\">".$foo1."</textarea></td>";
117:      
$counter++;
118:     }
119:
120:   } 
121:   
$i++;
122:   echo 
'</tr>';
123: }
124:
125: echo 
"</tbody></table>";
126:    echo 
'<input class="button" type="submit"  value="Save changes">';
127:  echo 
"</form>";
128: }
129: }
130: echo 
"</body></html>";
131: exit;
132:
133:
?>

Donate n Download click here.


Please help me I am handicapped and support myself

Donate With PayPal

 

Donate Bitcoin

Please donate and help the handicapped.

161vNPw4xe32H1s8QHjjbSSbhmrJPhkYVk



name:Misty Date:08.7.22 @ 06:21am IP:39.01182.7411.
This worked out great I sent you 19 cents.


name:Shantelle Date:08.7.22 @ 06:21am IP:9.204731.1.118
Thank you you are the best. Works like a charm. I sent you 22 cents.


name:Dave Date:08.7.22 @ 06:21am IP:119170823.41..
Thanks I sent you 71 cents.




Name:
Click Here to Reload

My websites do not use cookies or any google spyware.

 

Quick Support: Make it short.
Email:

Message: