TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-08-2005, 01:29 AM   #1 (permalink)
The Wanderer
Newcomer 
 
Join Date: May 2005
Posts: 5
Thanks: 0
zeromancer is on a distinguished road
Default HTML Tables to MySQL database

I created this script when I had to use FileMaker Pro to export some data. Then I had to put it into MySQL. The only problem was the CSV wouldn't work like it was supposed to on FileMaker. But the HTML tables were great to work with.
PHP Code:
<?php

  
/* This script was developed to parse HTML
     * tables exported by FileMaker Pro 5
     * for insertion into a MySQL table.
     * It assumes that each cell is on its 
     * own line and tags other than the table family are removed.
     */
     
  
ini_set('display_errors''1');
  
error_reporting(E_STRICT);
    
    include(
"mysql.inc.php"); //connects to my dB. you have to write your own.
    
$dir "./data";
    
$files scandir($dir);
    foreach(
$files as $f) { // loops through the ./data folder and looks for files to parse.
        
if($f != "." && $f != "..") {
            
$in_file "./data/".$f;
            
$table preg_replace('/.htm$/','',$f);
            
$out "";
            
$data file($in_file);
            
$columns "(";
            
$rep1 = array('/\s/','/\(/','/\)/');
            
$with1 = array('_','_','_');
            foreach(
$data as $i) {
                if(
preg_match('/^<TABLE/',$i) ) {  //This loop matches the initial <table> tag, which builds the CREATE TABLE query
                    
$out $out."CREATE TABLE IF NOT EXISTS `".$table."` (";
                } elseif(
preg_match('/<TH>(?P<head>[\D\S]+)<\/TH>/',$i,$matches)) {  //Uses <TH> tags to name the table columns
        
                    
$matches['head'] = preg_replace($rep1,$with1,$matches['head']);  //removes spaces and other column name un-friendly characters
                    
$matches['head'] = strtolower($matches['head']);
                    
$columns $matches['head'].",";
                    
$out $out."`".$matches['head']."` TINYTEXT NOT NULL,";
                } elseif(
preg_match('/^<\/TABLE>/',$i)) {  
                    
$out preg_replace('/,$/',"",$out);
                    
$out .= "\n) TYPE=INNODB";
                }
            }
            
$drop "DROP TABLE IF EXISTS `".$table."`";
            if(
mysql_query($drop)) {
                echo 
"Table `".$table."` dropped.<br />";
            } else {
                die(
mysql_error());
            }
            
            if(
mysql_query($out)) {
                echo 
"Table `".$table."` created<br />\n";
            } else {
                die(
mysql_error()."<p>".$out."</p>");
            }
        
            
$ins 0;
        
            foreach(
$data as $i) {
                    
                if(
preg_match('/^<TR>/',$i)) {   //On each new row of the table (<TR>), a new INSERT statement is built
                    
$insert "INSERT INTO ".$table." VALUES (";
                    
                } elseif(
preg_match('/<TD>(?P<cell>[\D\S]+)<\/TD>/',$i,$matches)) {  //adds data from each cell to the insert statement
                    
$test $matches['cell'];
                    
$rep2 = array('/<TABLE BORDER=1>+/','/<\/TABLE>/+','/<TD>+/','/<\/TD>+/','/<BR>+/','/\'/');
                    
$with2 = array("","","",",","NULL","_");
                    if(
$res preg_replace($rep2,$with2,$test) ) {  // in the case of a small table within the cell, removes tags and replaces with ','
                        
$insert .= "'".$res."'".",";                       // which will later be used to explode and implode for reading
                    
} else {
                        
$insert .= "'".$test."'".",";
                    }    
                }    elseif(
preg_match('/^<\/TR>/',$i)) {   //looks for the end of the table row and closes the insert statement.
                    
$insert preg_replace('/,$/',"",$insert);  //removes the trailing comma from the statement
                    
$insert .= ")";
        
                    if(
mysql_query($insert)) {
                        
$ins++;
                    } else {
                        die(
mysql_error());
                    }
                    unset(
$insert);     //deletes the insert statement from memory to start with a new one.
                
}    
                        
            }
            echo 
$ins." inserts sucessfully executed.<br />\n";  
        }
    }
?>
Some notes are listed in the comments, but any other questions are welcome. Just a note that you might miss: the <TH> tags are used to build the table. so if your table headers aren't made with them, your CREATE TABLE statement won't have any column names. Also, the tags must be capitalized.
Send a message via AIM to zeromancer
zeromancer is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Make html pages work like php sam Tips & Tricks 2 02-09-2007 01:00 PM


All times are GMT. The time now is 08:54 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design