Location: PHPKode > projects > PhpCodeBuilder > phpcb/filgen.php
<? 
/*
PHP Code Builder
Copyright (C) 2007  Revmakx

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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
require_once("header.php");
heading('Code For File Generation');
$db=$_REQUEST['db'];
$table=$_REQUEST['table'];
$btn=$_REQUEST['btn'];
$empty_row=$_REQUEST['empty_row'];
$cap_first=$_REQUEST['cap_first'];
$cap=$_REQUEST['cap'];


//echo '<br> DB='.$db.'<br> Table='.$table.'<br>Empty_row='.$empty_row;//coment 
include('connect.php'); ?>
<? 
if($btn=='Generate Files')
{

}//if btn 
?>
  
<? ////////////////////// function q_db_table($db,$table)
//{
?>
<form name="form1" method="post" action="?">
    <p>
      
    </p>
    <p>&nbsp;</p>
    <p align="center"><strong>Coder's Friend</strong></p>
    <p align="center">&nbsp;</p>
  <table width="427" border="0" align="center">
  <tr>
    <th width="261"  align="center" scope="row">Select DB</th>
    <td colspan="2"><select name="db" onChange="MM_jumpMenu('parent',this,0)">
	 <? $q1e=mysql_query('show databases'); 
	 	if(!$q1e)
		echo 'error q1';
		else
		echo 'succes';
		$n1=mysql_num_rows($q1e);
		if($n1==0)
		echo '<option>No Database is saved </option>';
		else
		{
			for($i=0;$i<$n1;$i++)
			{
				$a1=mysql_fetch_array($q1e);
			
				if($df_db=='')
					$df_db=$a1[0];
				if($db==$a1[0])
					{
						echo '<option selected value=?&db='.$a1[0].'>'.$a1[0].'</option>';
					}
					else
					{
						echo '<option value=?&db='.$a1[0].'>'.$a1[0].'</option>';
					}
			}
		}
		
		
	  ?>

    </select></td>
  </tr>
  <? if($db==NULL)
  {
  	$db=$df_db;
	 include('connect.php');
	}
	else include('connect.php');
	//echo 'DB='.$db.' Table='.$table.' btn_var='.$btn_var.' table='.$table;//coment

  ?>
  <tr>
    <th height="23" align="center" scope="row">Tables</th>
    <td colspan="2"><select name="table" onChange="MM_jumpMenu('parent',this,0)">
	<?
	
	$q2e=mysql_query('show tables');
	if(!$q2e)
		echo 'error q2';
		$n2=mysql_num_rows($q2e);
		if($n2==0)
		echo '<option>No Tables is saved </option>';
		else
		{
			$select2='selected';
			for($i=0;$i<$n2;$i++)
			{
						$a2=mysql_fetch_array($q2e);
						if($df_table=='')
					$df_table=$a2[0];
				if($table==$a2[0])
					{
						echo '<option selected value=?&db='.$db.'&table='.$a2[0].'>'.$a2[0].'</option>';
					}
					else
					{
						echo '<option value=?&db='.$db.'&table='.$a2[0].'>'.$a2[0].'</option>';
					}
			}
		
	?>
        </select></td>
  </tr>
  <?   if($table==NULL)
  {
  	$table=$df_table;
	 //include('connect.php');
	}
	}//end else if no tables saved.
	
	//return $n2;
//////////////}//funtion ends here

//////////////////////$n2=q_db_table($db,$table);
  ?>
  <tr>
    <th height="23" scope="row">&nbsp;</th>
    <td colspan="2">&nbsp;</td>
  </tr>
  <? if($n2!=0){ ?>
   <tr>
     <th height="23" align="left" scope="row"><label><input name="empty_row" type="checkbox" id="empty_row" value="1" <? if($empty_row==1) echo 'checked="checked"'; ?> />
Empty Row Between Data</label></th>
     <td colspan="2">&nbsp;</td>
   </tr>
   <tr>
     <th height="23" align="left" scope="row">
	 <fieldset>
     <legend>
     <label><input type="checkbox" name="cap" value="1" <? if($cap==1) echo 'checked="checked"'; ?>  onClick="return document.forms.form1.submit();" >
     Capitalise</label></legend>
     <p>
<label>
<input name="cap_first" type="radio" <? if($cap!=1){ echo 'disabled';} ?> value="1" <? if($cap_first==1) echo 'checked="checked"'; ?> >
First Letter</label>
     </p>
     <p>
       <label>
       <input name="cap_first" type="radio" <? if($cap!=1){ echo 'disabled';} ?>  value="2" <? if($cap_first==2) echo 'checked="checked"'; ?> >
       Full Word</label>
     </p>
     </fieldset>
</th>
     <td colspan="2">&nbsp;</td>
   </tr>
   <tr>
    <th height="23" align="left" scope="row"><label></label></th>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <th height="23" colspan="3" scope="row"><input name="btn" type="submit" id="btn" value="Generate Files" /></th>
    </tr> 
	<? } ?>
  </table>
  <? if($btn=='Generate Files')
	{
	//echo '<br>table='.$table;
$mypath="PCB_codes";
@mkdir($mypath,0777,TRUE);

$result = mysql_query('select * from '.$table);
if (!$result) {
   die('Query failed: ' . mysql_error());
}
/* get column metadata */
$n=mysql_num_fields($result);


$f='<?

include("connect.php");
@$status=$_REQUEST["status"];
if(@$status=="")
{?>
<form method="POST" action="insert.php">
	<p>&nbsp;</p>
	<div align="center">
		<table border="0" width="100%" id="table1">';

		
for($i=0;$i<$n;$i++)
{
   $meta = mysql_fetch_field($result, $i);
   if($meta->primary_key==1)
   {
   		
		$primary=$meta->name;
		continue;
	}
	$f=$f.' <tr>
				<td>
				<p align="right">'.$meta->name.'</td>
				<td><input type="text" name="'.$meta->name.'" size="20"></td>
			</tr>';
   
}

$f=$f.' 		
		</table>
	</div>
	<p>&nbsp;</p>
	<input type="hidden" name="status" value="ins">
	<p align="center"><input type="submit" value="Insert" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<?
}
if($status=="ins")
{
';
for($i=0;$i<$n;$i++)
{
   $meta = mysql_fetch_field($result, $i);
   $temp=$meta->name;
   $f=$f.'
   '.'$'.$temp.'=$_REQUEST["'.$temp.'"];';
}
$f=$f.'
$q="insert into '.$table.' set ';	
for($i=0;$i<$n;$i++)
{
    $meta = mysql_fetch_field($result, $i);
	 if($meta->primary_key==1)
   		continue;	
	$temp=$meta->name;
	$f=$f.$meta->name." = '$".$temp."'";
	if($i!=$n-1)
		$f=$f.", ";
}
$f=$f.'";
$qe=mysql_query($q);
	if($qe)
		echo \'Inserted<br><a href="insert.php">Go back</a>\';
	else
		echo \'Not Inserted\';
}
		
?>';
$filename ="PCB_codes/insert.php"; 
$myFile= fopen($filename,'w'); 
if(! $myFile){

print ("File could not be opened.");
}
fputs($myFile, $f); 
fclose($myFile); 
echo '<br><center><b>The following form codes are generated</b><br><br>The delete.php and edit.php can be accessed through view.php<br><br>';
echo 'The Insertion Page is generated at /PCB_codes/insert.php&nbsp;<a href="PCB_codes/insert.php">Insert</a> <br><br>';
/* FOR VIEW.PHP */





$f='<?

include(\'connect.php\');
';
$f=$f.'$q1="select * from '.$table.'";
$q1e=mysql_query($q1);
$n1=mysql_num_rows($q1e);
echo \'<table border="1" width="100%">
	<tr>\';
	echo \'
		<td>
		<p align="center">S.No</td>\';';
for($i=0;$i<$n;$i++)
{
   $meta = mysql_fetch_field($result, $i);
   if($meta->primary_key==1)
   {
   		$primary=$meta->name;
   		continue;
	}
	$f=$f.'
	echo \'
		<td>
		<p align="center">'.$meta->name.'</td>\';';
	$a3[$n3]=$meta->name;
	$n3=$n3+1;
}
$f=$f.'
echo \'<p align="center"><td>Function</td></tr>\';';
$f=$f.';
$q="select * from '.$table.'";
$qe=mysql_query($q);
$n2=mysql_num_rows($qe);
for($i=0;$i<$n2;$i++)
{
	$a2=mysql_fetch_array($qe);
	?><tr><td><? echo $i+1;?></td>
	';
for($i=0;$i<$n;$i++)
{
   $meta = mysql_fetch_field($result, $i);
   if($meta->primary_key==1)
   {
   		$primary=$meta->name;
   		continue;
	}
	$f=$f.'
	<td><? echo $a2[\''.$meta->name.'\'];?></td>';
}
$f=$f.'
<? 
echo \'<td align="center">[<a href="delete.php?'.$primary.'=\'.$a2[\''.$primary.'\'].\'" class="Ab">Delete</a>]/[<a href="edit.php?'.$primary.'=\'.$a2[\''.$primary.'\'].\'" class="Ab">edit</a>]</td>
	</tr>\';
	}
	echo \'</table>\';



?>
	';
$filename ="PCB_codes/view.php"; 
$myFile= fopen($filename,'w'); 
if(! $myFile){

print ("File could not be opened.");

exit;
}


fputs($myFile, $f); 

fclose($myFile); 

echo 'The View page is generated at /PCB_codes/view.php&nbsp;<a href="PCB_codes/view.php">View</a> <br></center><br>';


/* FOR EDIT.PHP */




$f='<?

include(\'connect.php\');
@$status=$_REQUEST[\'status\'];
if(@$status==\'\')
{
$'.$primary.'=$_REQUEST[\''.$primary.'\'];
$q1="select * from '.$table.' where '.$primary.'=$'.$primary.'";
$q1e=mysql_query($q1);
$a1=mysql_fetch_array($q1e);

?>
<form method="POST" action="edit.php">
	<p>&nbsp;</p>
	<div align="center">
		<table border="0" width="100%" id="table1">
			';
for($i=0;$i<$n;$i++)
{
   $meta = mysql_fetch_field($result, $i);
   $temp=$meta->name;
   $f=$f.'<tr>
				<td>
				<p align="right">'.$temp.'</td>
				<td><input type="text" name="'.$temp.'" size="20" value="<? echo $a1[\''.$temp.'\'];?>"></td>
			</tr>';
}
  $f=$f.'</table>
  </div>
	<p>&nbsp;</p>
	<input type="hidden" name="'.$primary.'" value="<? echo $'.$primary.';?>">
	<input type="hidden" name="status" value=\'edt\'>
	<p align="center"><input type="submit" value="Update" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<? 
}
if($status==\'edt\')
{'; 
for($i=0;$i<$n;$i++)
{
    $meta = mysql_fetch_field($result, $i);
	$temp=$meta->name;
	$f=$f.'
	$'.$temp.'=$_REQUEST[\''.$temp.'\'];';
}	
	


$q="update ".$table." set ";	
for($i=0;$i<$n;$i++)
{
    $meta = mysql_fetch_field($result, $i);
	 if($meta->primary_key==1)
   		continue;	
	$q=$q.$meta->name." = '$".$meta->name."'";
	if($i!=$n-1)
		$q=$q.", ";
}
	$q=$q." where ".$primary." ='$".$primary."'";
	$f=$f.'
	$q="'.$q.'";
	$qe=mysql_query($q);
	if($qe)
		echo \'Updated<br><a href="view.php">Go back</a>\';
	else
		echo \'Not Updated\';
}
	

?>';

	$filename ="PCB_codes/edit.php"; 
$myFile= fopen($filename,'w'); 
if(! $myFile){

print ("File could not be opened.");

exit;
}


fputs($myFile, $f); 

fclose($myFile); 













/* FOR DELETE.PHP  */






$f='<?

include(\'connect.php\');
$'.$primary.'=$_REQUEST[\''.$primary.'\'];
$q1="delete from '.$table.' where '.$primary.' =\'$'.$primary.'\'";
$q1e=mysql_query($q1);
if($q1e)
	echo \'Deleted<br><a href="view.php">Go back</a>\';
else
	echo \'Not Deleted\';

?>';
$filename ="PCB_codes/delete.php"; 
$myFile= fopen($filename,'w'); 
if(! $myFile){

print ("File could not be opened.");

exit;
}


fputs($myFile, $f); 

fclose($myFile); 





/* FOR CONNECT.PHP*/

$f='<?php

$connect=mysql_connect(\''.$db_host.'\',\''.$db_user.'\',\''.$db_pass.'\');
if(!$connect)
echo\'not connected\';
$db=mysql_select_db(\''.$db.'\');
if(!$db)
echo \'database is not connected\';
?>';



$filename ="PCB_codes/connect.php"; 
$myFile= fopen($filename,'w'); 
if(! $myFile){

print ("File could not be opened.");

exit;
}


fputs($myFile, $f); 

fclose($myFile); 

echo '<center>The Connection Page is generated at /PCB_codes/connect.php&nbsp;<a href="PCB_codes/connect.php">Connect</a> <br></center>';






	}
	 //echo 'n2='.$n2.'<br> DB='.$db.'<br> Table='.$table;//coment ?>
	<input type="hidden" name="db" value="<? echo $db; ?>">
<input type="hidden" name="table" value="<? echo $table; ?>">
</form>
<? require_once("footer.php");?>
Return current item: PhpCodeBuilder