Login   Register  
PHP Classes
elePHPant
Icontem

File: generator.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Bryan  >  PHP Class Generator Bryan Version  >  generator.php  >  Download  
File: generator.php
Role: Example script
Content type: text/plain
Description: Generator.php
Class: PHP Class Generator Bryan Version
Generate classes to manipulate MySQL table rows
Author: By
Last change: change the file path
Date: 2010-08-25 04:30
Size: 12,356 bytes
 

Contents

Class file image Download
<?php



include("resources/class.database.php");
$database = new Database();







if($_REQUEST["f"]=="")
{
?>

<font face="Arial" size="3"><b>
PHP MYSQL Class Generator
</b>
</font>

<font face="Arial" size="2"><b>

<form action="generator.php" method="POST" name="FORMGEN">

1) Select Table Name: 
<br>

<select name="tablename">

<?php
$database->OpenLink();
$tablelist = mysql_list_tables($database->database, $database->link);
while ($row = mysql_fetch_row($tablelist)) {
print "<option value=\"$row[0]\">$row[0]</option>";
}
?>
</select>

<p>
2) Type Class Name (ex. "test"): <br>
<input type="text" name="classname" size="50" value="">
<p>
3) Type Name of Key Field:
<br>
<input type="text" name="keyname" value="" size="50">
<br>
<font size=1>
(Name of key-field with type number with autoincrement!)
</font>
<p>


<input type="submit" name="s1" value="Generate Class">
<input type="hidden" name="f" value="formshowed">

</form>

</b>
</font>
<p>
<font size="1" face="Arial">
<a href="#" target="_blank">
this is a script by Bryan Jayson Tan
</a>
</font>


<?php
} else {

// fill parameters from form
$table = $_REQUEST["tablename"];
$class = $_REQUEST["classname"];
$key = $_REQUEST["keyname"];
$excludes = $_REQUEST["exclude"];

$excludes = explode(",", $excludes);

$dir = dirname(__FILE__);

$filename = $dir . "/resources/" . "class." . $class . ".php";

// if file exists, then delete it
if(file_exists($filename))
{
  unlink($filename);
}

// open file in insert mode
$file = fopen($filename, "w+");
$filedate = date("d.m.Y");


$c = "";

$c = "<?php
/*
*
* -------------------------------------------------------
* CLASSNAME:        $class
* GENERATION DATE:  $filedate
* CLASS FILE:       $filename
* FOR MYSQL TABLE:  $table
* FOR MYSQL DB:     $database->database
* -------------------------------------------------------

*
* CODE GENERATED BY: BRYAN JAYSON TAN
* DATE EDIT: MARCH 26 2010
*	ADDED METHOD: init(), selectOne(), selectAll(), toString()
* EDITED METHOD: update(), delete()
* DATE EDIT: JULY 31 2010
* ADDED METHOD: doCount()
* DATE EDIT: AUGUST 19, 2010
* EDITED METHOD: selectOne, selectAll,and doCount make them static
* -------------------------------------------------------
*
*/

include_once(dirname(__FILE__).\"/class.database.php\");

// **********************
// CLASS DECLARATION
// **********************

class $class
{ // class : begin


// **********************
// ATTRIBUTE DECLARATION
// **********************

  private $$key;   // KEY ATTR. WITH AUTOINCREMENT
";

$sql = "SHOW COLUMNS FROM $table;";
$database->query($sql);
$result = $database->result;


while ($row = mysql_fetch_row($result)) 
{
$col=$row[0];

if($col!=$key)
{

$c.= "
  private $$col;   // (normal Attribute)";


} // endif
//"print "$col";
} // endwhile

$cdb = "$" . "database";
$cdb2 = "database";
$c.="

  private $cdb; // Instance of class database

";

$cthis = "$" . "this->";
$thisdb = $cthis . $cdb2 . " = " . "new Database();";

$c.= "
// **********************
// CONSTRUCTOR METHOD
// **********************

function $class()
{
  $thisdb
}

";

$c.="
// **********************
// GETTER METHODS
// **********************

";
// GETTER
$database->query($sql);
$result = $database->result;
while ($row = mysql_fetch_row($result)) 
{
$col=$row[0];
$pieces = explode("_",$col);
$getname = "";
foreach($pieces as $piece)
{
  $getname .= ucfirst($piece);
}
$mname = "get" . $getname . "()";
$mthis = "$" . "this->" . $col;
$c.="
public function $mname
{
  return $mthis;
}
";
}


$c.="
// **********************
// SETTER METHODS
// **********************

";
// SETTER
$database->query($sql);
$result = $database->result;
while ($row = mysql_fetch_row($result)) 
{
$col=$row[0];
$val = "$" . "val";
$pieces = explode("_",$col);
$getname = "";
foreach($pieces as $piece)
{
  $getname .= ucfirst($piece);
}
$mname = "set" . $getname . "($" . "val)";
$mthis = "$" . "this->" . $col . " = ";
$c.="
public function $mname
{
  $mthis $val;
}
";
}

// create by Bryan Jayson Tan
// Date Created: March 26 2010
$row = "$" . "row";
$c.="
// **********************
// INIT METHOD
// **********************

public function init($row)
{
";
$sql = "SHOW COLUMNS FROM $table;";
$database->query($sql);
$result = $database->result;
while ($row = mysql_fetch_row($result)) 
{
	$col=$row[0];
	$cthis = "$" . "this->" . $col . " = $" . "row->" . $col;

	$c.="
	$cthis;
	";
}
$c.="
}
";

// ------------------- to String --------------------
$tostring = "$" . "s";

$c.="
// **********************
// TO STRING METHOD
// **********************

public function toString()
{
";	
$sql = "SHOW COLUMNS FROM $table;";
$database->query($sql);
$result = $database->result;
$cthis = $tostring . " = '';\n\n";
while ($row = mysql_fetch_row($result)) 
{
$col=$row[0];
$cthis .= "" . $tostring . " .= '" . $col . ": '." . "$" . "this->" . $col . " . ', ';";

$cthis .= "\n\n";

}
  $c.= substr($cthis, 0, -10) . ";";
  $c.="

  return $tostring;
 
}
";

// ---------------------- SELECT ALL -------------
$sql = "$" . "sql = ";
$id = "$" . "id";
$result = "$" . "result = ";
$row = "$" . "row";
$result1 = "$" . "result";

$classname = "$" . strtolower($class);
$classinit = $classname. "->init($row);";
$objarray = "$" . "objarray";
$thisdb = "$classname->" . "database";
$thisdbquery = "$classname->" . "database->query($" . "sql" . ")";
$res = "$" . "result = $classname->" . "database->result;";
$criteria = "$" . "criteria";
$addquery = "$" . "sql.=";

$c.="
// **********************
// SELECT METHOD / LOAD ALL
// **********************

public static function selectAll($criteria = null)
{
	$objarray = array(); // list of objects
	$sql \"SELECT $table.* FROM $table\";
  if ($criteria)
  {
    $addquery \" \" . $criteria;
  }
  $classname = new $class();
	$result $thisdbquery;
	$res
	while($row = mysql_fetch_object($result1))
	{
		$classname = new $class();
		$classinit
		array_push($objarray,$classname);
	}
	return $objarray;
}
";

// Date Edited: March 26 2010
$sql = "$" . "sql = ";
$id = "$" . "id";

$result = "$" . "result = ";
$row = "$" . "rows";
$result1 = "$" . "result";
$classname = "$" . strtolower($class);
$classinit = "$classname->" . "init(mysql_fetch_object($result1));";
$thisdb = "$classname->" . "database";
$thisdbquery = "$classname->" . "database->query($" . "sql" . ")";
$res = "$" . "result = $classname->" . "database->result;";
$rowinit = "$row = $classname->" . "database->rows;";
$criteria = "$" . "criteria";
$addquery = "$" . "sql.=";

$c.="
// **********************
// SELECT METHOD / LOAD ONE
// **********************

public static function selectOne($id,$criteria = null)
{
  $sql \"SELECT $table.* FROM $table WHERE $key = $id\";
  if ($criteria)
  {
     $addquery \" \" . $criteria;
  }
  $classname = new $class();
  $result $thisdbquery;
  $res
  $rowinit
  if ($row > 0){
    $classinit
    return $classname;
  }else{
    return null;
  }
  ";





$c.="
}
";

// ----------- Old Select Method -----------
/*
$sql = "$" . "sql = ";
$id = "$" . "id";
$thisdb = "$" . "this->" . "database";
$thisdbquery = "$" . "this->" . "database->query($" . "sql" . ")";
$result = "$" . "result = ";
$row = "$" . "row";
$result1 = "$" . "result";
$res = "$" . "result = $" . "this->database->result;";

$c.="
// **********************
// SELECT METHOD / LOAD
// **********************

public function select($id)
{

$sql \"SELECT * FROM $table WHERE $key = $id;\";
$result $thisdbquery;
$res
$row = mysql_fetch_object($result1);

";

$sql = "SHOW COLUMNS FROM $table;";
$database->query($sql);
$result = $database->result;
while ($row = mysql_fetch_row($result)) 
{
$col=$row[0];
$cthis = "$" . "this->" . $col . " = $" . "row->" . $col;

$c.="
$cthis;
";

}



$c.="
}
";*/

$idwhere = "$" . "this->$key";
$zeile1 = "$" . "sql" . " = \"DELETE FROM $table WHERE $key = $idwhere;\"";
$zeile2 = "$" . "result = $" . "this->database->query($" . "sql);";
$c.="
// **********************
// DELETE
// **********************

public function delete()
{
  $zeile1;
  $zeile2
";
$c.="
}
";


$zeile1 = "$" . "this->$key = \"\"";
$zeile2 = "INSERT INTO $table (";
$zeile5= ")"; 
$zeile3 = "";
$zeile4 = "";
$zeile6 = "VALUES (";

$sql = "SHOW COLUMNS FROM $table;";
$database->query($sql);
$result = $database->result;
while ($row = mysql_fetch_row($result)) 
{
$col=$row[0];

if($col!=$key && $row[1] != "timestamp")
{
//$datatype = mysql_field_type($result,$row);

$zeile3.= "$col" . ",";
$zeile4.= "'$" . "this->$col" . "',";
//$zeile3 = rtrim($zeile3);
//$zeile4 = rtrim($zeile4);
//$zeile3 = str_replace(",", " ", $zeile3);
//$zeile4 = str_replace(",", " ", $zeile4);



}

}

$zeile3 = substr($zeile3, 0, -1);
$zeile4 = substr($zeile4, 0, -1);
$sql = "$" . "sql =";
$zeile7 = "$" . "result = $" . "this->database->query($" . "sql);";
$zeile8 = "$" . "row";
$zeile9 = "$" . "result";
$zeile10 = "$" . "this->$key = " . "mysql_insert_id($" . "this->database->link);";

$chksuccess = "$" . "this->$key";
$c.="
// **********************
// INSERT
// **********************

public function insert()
{
  $zeile1; // clear key for autoincrement

  $sql \"$zeile2 $zeile3 $zeile5 $zeile6 $zeile4 $zeile5\";
  $zeile7
  $zeile10

}
";


// UPDATE ----------------------------------------

$zeile1 = "$" . "this->$key = \"\"";
$zeile2 = "UPDATE $table SET ";
$zeile5= ")"; 
$zeile3 = "";
$zeile4 = "";
$zeile6 = "VALUES (";

$upd = "";

$sql = "SHOW COLUMNS FROM $table;";
$database->query($sql);
$result = $database->result;
while ($row = mysql_fetch_row($result)) 
{
$col=$row[0];

if($col!=$key && $row[1] != "timestamp")
{
$zeile3.= "$col" . ",";
$zeile4.= "$" . "this->$col" . ",";
$upd.= "" . "$col = '$" . "this->$col',";
//$zeile3 = rtrim($zeile3);
//$zeile4 = rtrim($zeile4);
//$zeile3 = str_replace(",", " ", $zeile3);
//$zeile4 = str_replace(",", " ", $zeile4);



}

}

$zeile3 = substr($zeile3, 0, -1);
$zeile4 = substr($zeile4, 0, -1);
$upd = substr($upd, 0, -1);
$sql = "$" . "sql = \"";
$zeile7 = "$" . "result = $" . "this->database->query($" . "sql)";
$zeile8 = "$" . "row";
$zeile9 = "$" . "result";
$zeile10 = "$" . "this->$key = $" . "row->$key";
$id = "$" . "id";
$idwhere = "$" . "this->$key";
$where = "WHERE " . "$key = " . $idwhere;

$c.="
// **********************
// UPDATE
// **********************

public function update()
{
  $sql $zeile2 $upd $where \";

  $zeile7;";

$c.="
}
";



// ---------------------- SELECT ALL -------------
$sql = "$" . "sql = ";
$id = "$" . "id";

$result = "$" . "result = ";
$row = "$" . "row";
$result1 = "$" . "result";
$row = "$" . "row";
$rowinit = $row . '->count';
$classname = "$" . strtolower($class);
$classinit = $classname. "->init($row);";
$thisdb = "$classname->" . "database";
$thisdbquery = "$classname->" . "database->query($" . "sql" . ")";
$res = "$" . "result = $classname->" . "database->result;";
$objarray = "$" . "objarray";
$criteria = "$" . "criteria";
$addquery = "$" . "sql.=";

$c.="
// **********************
// DO COUNT
// **********************

public static function doCount($criteria = null)
{
  $sql \"SELECT COUNT(*) as count FROM $table\";
  if ($criteria)
  {
    $addquery \" \" . $criteria;
  }
  $classname = new $class();
	$result $thisdbquery;
	$res
  $row = mysql_fetch_object($result1);
	
  return $rowinit;
}";







$c.= "

} // class : end

?>";
fwrite($file, $c);

print "
<font face=\"Arial\" size=\"3\"><b>
PHP MYSQL Class Generator
</b>
<p>
<font face=\"Arial\" size=\"2\"><b>
Class '$class' successfully generated as file '$filename'!
<p>
<a href=\"javascript:history.back();\">
back
</a>

</b></font>

";


?>
<p>
<font size="1" face="Arial">
<a href="#" target="_blank">
this is a script by Bryan Jayson Tan
</a>
</font>





<?php
} // endif
?>