Login   Register  
PHP Classes
elePHPant
Icontem

File: index.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Albert Rosa  >  CSV2SQL_AR  >  index.php  >  Download  
File: index.php
Role: Example script
Content type: text/plain
Description: This is the index page
Class: CSV2SQL_AR
Generate SQL from data imported CSV files
Author: By
Last change:
Date: 2010-07-07 20:15
Size: 4,341 bytes
 

Contents

Class file image Download
<?php

/**
 * This is the form we will be using to upload the csv file to convert to sql
 **/
 
 
$error null;
 
$text null;
 
$success false;
 
$table true;
 
$insert true;
 
 
$tableName 'information';
 
 if(
$_FILES['file']['name'] != null)
 {
     require_once 
'CSV2SQL.php';
     
     if(
$_POST['create'])
         
$table true;
     else
         
$table false;
         
     if(
$_POST['insert'])
         
$insert true;
     else
         
$insert false;
         
     if(
$_POST['tableName'])
         
$tableName $_POST['tableName'];
         
     
     if(
$_FILES['file']['tmp_name'] != null)
     {
         
$success true;
         
         
$target_path $_FILES['file']['tmp_name'] . '.csv';
         
         if(
move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
         {
             
$csvsql = new CSV2SQL($target_path$table$insert $tableName);
         
             
$text =  $csvsql->getSql();
         }else{
             echo (
'Error uploading');
         }
         
//here we will be using the file version of class
         
     
}
     else
         
$error 'THERE is an Error with the CSV2SQL'
 }
 
 elseif(
$_POST['tableName'] && $_FILES['file']['name'] == null
 {
     
$error "Please add a file in order to process";
 }
 
?>
 
 <html>
     <head>
         <title>Albert-Rosa.com | CSV to SQL</title>
         <meta name="author" content="Albert Rosa"/>
         <meta name="description" content="This is where I will be using csv files to be converted to sql files" />
         <meta name="keywords" content="Albert, Rosa, CSV, SQL, database, Mysql"/>
         <link href="/screen.css" media="screen" type="text/css" rel="stylesheet" />
         
         <style type="text/css">
             /** ADD RESET **/
             .red{color:#F00;}
             div.container {width:800px; background-color: #cdcdcd; margin-top:15px;}
             div.header{ background-color: #ddd;height:50px;padding:5px;}
             div.content{padding:10px;}
             div.links {float:right; margin-right: 10px;}
             div.footer {padding:10px;}
             ul li {float:right;}
             
         </style>
         
     </head>
     
     <body>
         <div class="container">
             <div class="header">
                 <div class="links">
                     <ul>
                         <li><a href="http://www.albert-rosa.com" title="Albert Rosa">Albert Rosa</a></li>
                         <li><a href="http://blog.albert-rosa.com" title="Albert Rosa : Blog">Blog</a> | </li>
                         <li><a href="http://mta.albert-rosa.com" title="Albert Rosa : MTA Demo">MTA DEMO</a> | </li>
                     </ul>
                 </div>
                 <div class="title"><h1>CSV 2 SQL</h1></div>
                 <div style="clear:both;"></div>
             </div>
             <div class="content">
                <p>
                    This is a form for converting CSV files to SQL.
                </p>
                
                <p>
                    This came about while my development of <a href="http://code.google.com/p/mta-zend" title="Zend MTA Google Repo"> Zend Mta</a>
                </p>
                <p>
                    Please Note: The max file size allowed to be processed is <span class="red">5MB</span>. If the file is larger it will try to process but it will time out and run out of memory. So best case is to split the CSV file into multiple files placing the header columns on the top of each file.
                </p>
                
                <? if($error != null):?>
                <div class="error"><?= $error ?></div>
                <? endif; ?>
                
                <? if($success): ?>
                <div class="success">Successfully submitted Form</div>
                <? endif;?>
                
                
                <form method="post" enctype="multipart/form-data">
                <fieldset>
                    <legend>CSV TO SQL</legend>
                    <dl>
                        <dt>FILE:</dt>
                        <dd><input type="file" name="file" id="file"/></dd>
                        
                        <dt>Table Name:</dt>
                        <dd><input type="text" name="tableName"  value="<?= $tableName ?>" /></dd>
                        
                        <dt>Options:</dt>
                        <dd>
                            <input type="checkbox" value="1" name="create" id="create" checked="checked" /> Create Table <br/>
                            <input type="checkbox" value="1" name="insert" id="insert" checked="checked" /> Insert  Statements<br/>
                        </dd>
                        
                        <dt></dt>
                        <dd><input  type="submit" value="Create SQL"/></dd>
                    </dl>
                </fieldset>
                </form>
                
                <?php if($success):?>
                <hr/>
                
                <fieldset>
                    <legend>Result</legend>
                    <p>Here is your Create and/or Insert Statements</p>
                    <dl>
                        <dt></dt>
                        <dd><textarea class="textarea"><?= $text ?></textarea></dd>
                    </dl>
                </fieldset>
                <?php endif; ?>
             </div> <!-- end of content -->
             <div class="footer">
                 &copy; 2010 <a href="http://www.albert-rosa.com" title="Albert Rosa">Albert Rosa</a> 
             </div>
         </div> <!-- end of container -->
     </body>
 </html>