<?php
/*
+-----------------+------------------------------------------------------------+
| Class | setup |
| Author | Brian Onang\'o |
| Version | 1.0 |
| Last Modified | 10:16 AM 10/2/2010 GMT+3 |
+-----------------+------------------------------------------------------------+
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+------------------------------------------------------------------------------+
|Description:: |
|A minute class for creating mysql tables without much hassle |
+------------------------------------------------------------------------------+
*/
Class setup{
var $tables = array();
/**************************************************************************************************
This function may or may not take any arguments. Passing arguments to here is equivalent to passing
the arguements to $this->setopt();
***************************************************************************************************/
function __construct(){
$args = func_get_args();
if($args)$this->setopt($args);
}
/******************************************************************************************************
This is the brain of our class. Arguments can be passed directly to this function, or they are passed
from $this->__construct();
The accepted arguments formats are:
# The first argument is forever(unless you modify the code) a string that denotes the tablename for
which the options are being set.
#The available options are:
1. Field passed as
a). setopt('tablename',field,'field|datatype') OR
b). setopt('tablename',array(field,'field|datatype')) OR
c). setopt('tablename','field',array('field|datatype','field1|datatype'))
2. mysql server passed as
a). setopt('tablename','server':your mysql server')
In the same way that the server argument is passed, so is the mysql username(user),
password(pass),database(db) passed
And that is almost all the documentation there is for anyone who doesn't intend to modify this program.
*******************************************************************************************************/
function setopt(){
$num = func_num_args();
$args = func_get_args();
#if the arguments are passed from $this->__construct()
#args[0] is the array of arguments from __construct()
#args[0][0] is the first arguement from __construct()
if(isset($args[0][0])){
if($num==1){
#arguments passed to $this->__construct()
$bibi = $args[0];
}else{
#arguments passed to $this->setopt();
$bibi=$args;
}
}
#if the first argument passed to $this->__construct() is not an array
if(!is_array($bibi[0])){
$count = count($bibi);
#if only one string arguement is passed to $this->__construct()
#$obj=new Setup('tablename');
if($count==1){
$tmp=array('db'=>NULL,
'pass'=>NULL,
'field'=>array(NULL));
$this->tables[$bibi[0]] = $tmp;
#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>datatype)
#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>array(datatypes)
#$obj=new setup('tablename',array('db'=>password'));
}else{
$temp=array();
for($i=1;$i<$count;$i+=2){
#obj=new setup('tablename','database','password','field','dataype');
if(!is_Array($bibi[$i])){
#obj=new setup('tablename','field','dataype');
if($bibi[$i]=='field'){
#obj=new setup('tablename','field','dataype');
if(!is_Array($bibi[$i+1])){
$temp['field'][] = $bibi[$i+1];
#obj=new setup('tablename','field',array('dataype'));
}else{
foreach($bibi[$i+1] as $bri=>$an){
$temp['field'][] = $an;
}
}
}else{
$temp[$bibi[$i]] = $bibi[$i+1];
}
#obj=new setup('tablename','database','password',array('field'=>'dataype'),'field','datatype');
#$obj=new setup('tablename','database','password',array('field','dataype','field','field1','field','field2'));
}else{
$cnt=count($bibi[$i]);
for($j=0;$j<$cnt;$j+=2){
if(isset($bibi[$i][$j])){
$me=$bibi[$i][$j];
if($me=='field'){
$temp['field'][] = $bibi[$i][$j+1];
}else{
$temp[$bibi[$i][$j]] = $bibi[$i][$j+1];
}
unset($bibi[$i][$j]);
unset($bibi[$i][$j+1]);
}
}
foreach($bibi[$i] as $bri=>$an){#fields are not over written.
if(is_Array($an)){
foreach($an as $me=>$she){
if($me=='field'){
$temp['field'][] = $she;
}else{
$temp[$me] = $she;
}
}
}else{
if($bri=='field'){
$temp['field'][] = $an;
}else{
$temp[$bri] = $an;
}
}
}
$i--;
}
}
if(isset($this->tables[$bibi[0]])){
$tmp=$this->__combine($this->tables[$bibi[0]],$temp);
$this->tables[$bibi[0]]=$tmp;
}else{
$this->tables[$bibi[0]]=$temp;
}
}
}
}
#adding elements to an array=>combining two arrays
function __combine($x,$y){
$tmp=array();
foreach($x as $bri=>$an){
$tmp[$bri]=$an;
}
foreach($y as $bri=>$an){
$tmp[$bri]=$an;
}
return $tmp;
}
#Executing our class
function exec($tb=null){
$tables=array();
if(isset($tb)){$tables[$tb]=0;}else{$tables=$this->tables;}
foreach($tables as $bibi=>$ana){
try{
$this->__con($bibi);
$this->__setfields($bibi);
$this->__create($bibi);
}catch(Exception $e){
echo $e->getmessage();
}
}
}
#Creating a connection to mysql_server
function __con($tb){
if(isset($this->tables[$tb])){$tables=$this->tables[$tb];}else{throw new Exception("You are trying to create a table that has no been defined");}
if(isset($tables['server'])){$server=$tables['server'];}else {throw new exception("No server name given");}
if(isset($tables['user'])){$user=$tables['user'];}else {throw new exception("No username given");}
if(isset($tables['pass'])){$pass=$tables['pass'];}else {throw new exception("No password given");}
if(isset($tables['db'])){$db=$tables['db'];}else {throw new exception("No database given");}
$con = mysql_connect($server,$user,$pass);
if(!$con){throw new Exception("Unable to connect to SQL server because: ".mysql_error());}
mysql_select_db($db,$con)or die(mysql_error());
}
function __setfields($tb){
$tables=$this->tables[$tb];
if(isset($tables['field'])){$fields = $tables['field'];}else{throw new Exception("You haven't provided any fields");}
foreach($fields as $bibi=>$ana){
$this->__split($ana);
}
}
#Extracting field and datatype from the fields passed f.e from ('fieldname| CHAR(128)')
function __split($x){
$parts = explode("|",$x);
$this->fields[$parts[0]]=$parts[1];
}
function __create($tb){
$drop= "drop table $tb";
$create="";#state VARCHAR(128),
$fields=array();
foreach($this->fields as $bibi=>$ana){
$fields[]="$bibi $ana";
}
foreach($fields as $bibi){
$create .="$bibi,";
}
#remove last comma in $create. I haven't found as yet a more efficient way to do it.
$create=substr($create,0,(strlen($create)-1));
$create="Create table $tb($create)";
echo $create;
$result=mysql_query($drop);
$result=mysql_query($create);
if ($result){mysql_close();Return "sucess - created table.";}
else
{
$Error = "Mysql ERROR: ".mysql_errno().". ".mysql_error();
mysql_close();
Return "$Error";
}
}
}
#sample usage;
$server='your mysql server';
$user='your mysql server username';
$pass='probaly your girlfriend\'s\wife\'s name ';
$db='your database name';
$table='your table name';
$field=array('your unindexed fields');
$obj=new setup($table,'server',$server,'db',$db,'pass',$pass,'user',$user,'field',$field);
$exec=$obj->exec();
echo $exec;
?>
Mysql Table Creator
<?php
/*
+-----------------+------------------------------------------------------------+
| Class | setup |
| Author | Brian Onang\'o |
| Last Modified | 10:16 AM 10/2/2010 |
+-----------------+------------------------------------------------------------+
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| |
+------------------------------------------------------------------------------+
|Description: |
|A minute class for creating mysql tables without much hassle |
+------------------------------------------------------------------------------+
*/
Class setup{
var $tables = array();
/**************************************************************************************************
This function may or may not take any arguments. Passing arguments to here is equivalent to passing
the arguements to $this->setopt();
***************************************************************************************************/
function __construct(){
$args = func_get_args();
if($args)$this->setopt($args);
}
/******************************************************************************************************
This is the brain of our class. Arguments can be passed directly to this function, or they are passed
from $this->__construct();
The accepted arguments formats are:
# The first argument is forever(unless you modify the code) a string that denotes the tablename for
which the options are being set.
#The available options are:
1. Field passed as
a). setopt('tablename',field,'field|datatype') OR
b). setopt('tablename',array(field,'field|datatype')) OR
c). setopt('tablename','field',array('field|datatype','field1|datatype'))
2. mysql server passed as
a). setopt('tablename','server':your mysql server')
In the same way that the server argument is passed, so is the mysql username(user),
password(pass),database(db) passed
And that is almost all the documentation there is for anyone who doesn't intend to modify this program.
*******************************************************************************************************/
function setopt(){
$num = func_num_args();
$args = func_get_args();
#if the arguments are passed from $this->__construct()
#args[0] is the array of arguments from __construct()
#args[0][0] is the first arguement from __construct()
if(isset($args[0][0])){
if($num==1){
#arguments passed to $this->__construct()
$bibi = $args[0];
}else{
#arguments passed to $this->setopt();
$bibi=$args;
}
}
#if the first argument passed to $this->__construct() is not an array
if(!is_array($bibi[0])){
$count = count($bibi);
#if only one string arguement is passed to $this->__construct()
#$obj=new Setup('tablename');
if($count==1){
$tmp=array('db'=>NULL,
'pass'=>NULL,
'field'=>array(NULL));
$this->tables[$bibi[0]] = $tmp;
#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>datatype)
#setopt('tablename',array('database'=>name,'dbpassword'=>password,'fields'=>array(datatypes)
#$obj=new setup('tablename',array('db'=>password'));
}else{
$temp=array();
for($i=1;$i<$count;$i+=2){
#obj=new setup('tablename','database','password','field','dataype');
if(!is_Array($bibi[$i])){
#obj=new setup('tablename','field','dataype');
if($bibi[$i]=='field'){
#obj=new setup('tablename','field','dataype');
if(!is_Array($bibi[$i+1])){
$temp['field'][] = $bibi[$i+1];
#obj=new setup('tablename','field',array('dataype'));
}else{
foreach($bibi[$i+1] as $bri=>$an){
$temp['field'][] = $an;
}
}
}else{
$temp[$bibi[$i]] = $bibi[$i+1];
}
#obj=new setup('tablename','database','password',array('field'=>'dataype'),'field','datatype');
#$obj=new setup('tablename','database','password',array('field','dataype','field','field1','field','field2'));
}else{
$cnt=count($bibi[$i]);
for($j=0;$j<$cnt;$j+=2){
if(isset($bibi[$i][$j])){
$me=$bibi[$i][$j];
if($me=='field'){
$temp['field'][] = $bibi[$i][$j+1];
}else{
$temp[$bibi[$i][$j]] = $bibi[$i][$j+1];
}
unset($bibi[$i][$j]);
unset($bibi[$i][$j+1]);
}
}
foreach($bibi[$i] as $bri=>$an){#fields are not over written.
if(is_Array($an)){
foreach($an as $me=>$she){
if($me=='field'){
$temp['field'][] = $she;
}else{
$temp[$me] = $she;
}
}
}else{
if($bri=='field'){
$temp['field'][] = $an;
}else{
$temp[$bri] = $an;
}
}
}
$i--;
}
}
if(isset($this->tables[$bibi[0]])){
$tmp=$this->__combine($this->tables[$bibi[0]],$temp);
$this->tables[$bibi[0]]=$tmp;
}else{
$this->tables[$bibi[0]]=$temp;
}
}
}
}
#adding elements to an array=>combining two arrays
function __combine($x,$y){
$tmp=array();
foreach($x as $bri=>$an){
$tmp[$bri]=$an;
}
foreach($y as $bri=>$an){
$tmp[$bri]=$an;
}
return $tmp;
}
#Executing our class
function exec($tb=null){
$tables=array();
if(isset($tb)){$tables[$tb]=0;}else{$tables=$this->tables;}
foreach($tables as $bibi=>$ana){
try{
$this->__con($bibi);
$this->__setfields($bibi);
$this->__create($bibi);
}catch(Exception $e){
echo $e->getmessage();
}
}
}
#Creating a connection to mysql_server
function __con($tb){
if(isset($this->tables[$tb])){$tables=$this->tables[$tb];}else{throw new Exception("You are trying to create a table that has no been defined");}
if(isset($tables['server'])){$server=$tables['server'];}else {throw new exception("No server name given");}
if(isset($tables['user'])){$user=$tables['user'];}else {throw new exception("No username given");}
if(isset($tables['pass'])){$pass=$tables['pass'];}else {throw new exception("No password given");}
if(isset($tables['db'])){$db=$tables['db'];}else {throw new exception("No database given");}
$con = mysql_connect($server,$user,$pass);
if(!$con){throw new Exception("Unable to connect to SQL server because: ".mysql_error());}
mysql_select_db($db,$con)or die(mysql_error());
}
function __setfields($tb){
$tables=$this->tables[$tb];
if(isset($tables['field'])){$fields = $tables['field'];}else{throw new Exception("You haven't provided any fields");}
foreach($fields as $bibi=>$ana){
$this->__split($ana);
}
}
#Extracting field and datatype from the fields passed f.e from ('fieldname| CHAR(128)')
function __split($x){
$parts = explode("|",$x);
$this->fields[$parts[0]]=$parts[1];
}
function __create($tb){
$drop= "drop table $tb";
$create="";#state VARCHAR(128),
$fields=array();
foreach($this->fields as $bibi=>$ana){
$fields[]="$bibi $ana";
}
foreach($fields as $bibi){
$create .="$bibi,";
}
#remove last comma in $create. I haven't found as yet a more efficient way to do it.
$create=substr($create,0,(strlen($create)-1));
$create="Create table $tb($create)";
echo $create;
$result=mysql_query($drop);
$result=mysql_query($create);
if ($result){mysql_close();Return "sucess - created table.";}
else
{
$Error = "Mysql ERROR: ".mysql_errno().". ".mysql_error();
mysql_close();
Return "$Error";
}
}
}
#sample usage;
$server='your mysql server';
$user='your mysql server username';
$pass='probaly your girlfriend\'s\wife\'s name ';
$db='your database name';
$table='your table name';
$field=array('your unindexed fields');
$obj=new setup('tablename','server',$server,'db',$db,'pass',$pass,'user',$user,'field',$field);
$exec=$obj->exec();
echo $exec;
?>
smantscheff 265 Veteran Poster
Brianbc 9 Light Poster
smantscheff 265 Veteran Poster
Brianbc 9 Light Poster
smantscheff 265 Veteran Poster
Brianbc 9 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.