ragavbpl Posted April 9, 2011 Share Posted April 9, 2011 Before I explain my problem here are the files I have created - create.php <form action="#" method="post"> Name:<input type="text" name="tab"/>Number of Fields:<input tabindex="text" name="colnum"/> <input type="submit" /> </form> <?php if(isset($_POST['tab']) and isset($_POST['colnum'])){ if($_POST['colnum']>0) { echo '<form action="createn.php" method="post">'; foreach(range(1,$_POST['colnum']) as $index) { echo $index; echo ') ColName<input type="text" name="colname'.$index.'"> '; echo 'Type<input type="text" name="type'.$index.'">'; echo '<input type="hidden" name="tab" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum" value="'.$_POST['colnum'].'"/>'; echo '<br/>'; } echo '<input type="submit"/>'; echo '</form>'; } else echo "Number of fields should be greater then 0"; } else { echo "Fill in both the fields"; } ?> createn.php <?php $conn=oci_connect('scott','tiger'); $tabname=$_POST['tab']; foreach(range(1,$_POST['colnum']) as $index) { echo $_POST["colname".$index]; echo $_POST["type".$index]; echo '<br/>'; } echo "Table name =".$tabname; $query='create table '.$tabname.'('.$_POST["colname1"].' '.$_POST["type1"].')'; $statement=oci_parse($conn,$query); if(oci_execute($statement)) { echo "Table created"; } else { echo 'no table created'; } ?> Create.php explanation -> On running it gives a form asking for table name and number of fields: Suppose I enter table name as "EMP" and no. of fields as "2".. and press submit.. now on the same page I'll get two sets of field each containing 2 boxes (ie Name and Type).. When I fill in both the field set with let's say - 1) Name: -"empno" Type: -"number" 2) Name: -"ename" Type: -"varchar2" and press submit it the form submits the values to "createn.php" Createn.php explanation -> On this file I want to form a statement to create a new table with all the inputed data... ie something as follows - "create table <table_name>(<column 1> <type1>,<column 2> <type2>,....)" The code I have written in this file puts only 1 column and it's type as I have manually entered it. But suppose I don't know how many column is user going to put in.. then in that case how can I code the above stated statement in bold? Quote Link to comment https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/ Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 <form action="#" method="post"> Name:<input type="text" name="tab"/>Number of Fields:<input tabindex="text" name="colnum"/> <input type="submit" /> </form> <?php if(isset($_POST['tab']) and isset($_POST['colnum'])){ if($_POST['colnum']>0) { echo '<form action="createn.php" method="post">'; foreach(range(1,$_POST['colnum']) as $index) { echo $index; echo ') ColName<input type="text" name="colname'.$index.'"> '; echo 'Type<input type="text" name="type'.$index.'">'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<br/>'; } echo '<input type="submit"/>'; echo '</form>'; } else echo "Number of fields should be greater then 0"; } else { echo "Fill in both the fields"; } ?> <?php $conn=oci_connect('scott','tiger'); $i=0 $col = $_POST['tab']; foreach ($_POST['tab'] as $tab) { $tabname=$tab; $colnum= $col[$i]; foreach(range(1,$_POST['colnum']) as $index) { echo $_POST["colname".$index]; echo $_POST["type".$index]; echo '<br/>'; } echo "Table name =".$tabname; $query='create table '.$tabname.'('.$_POST["colname1"].' '.$_POST["type1"].')'; $statement=oci_parse($conn,$query); if(oci_execute($statement)) { echo "Table created"; } else { echo 'no table created'; } $i++;} ?> Ted Quote Link to comment https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/#findComment-1199212 Share on other sites More sharing options...
ragavbpl Posted April 9, 2011 Author Share Posted April 9, 2011 <form action="#" method="post"> Name:<input type="text" name="tab"/>Number of Fields:<input tabindex="text" name="colnum"/> <input type="submit" /> </form> <?php if(isset($_POST['tab']) and isset($_POST['colnum'])){ if($_POST['colnum']>0) { echo '<form action="createn.php" method="post">'; foreach(range(1,$_POST['colnum']) as $index) { echo $index; echo ') ColName<input type="text" name="colname'.$index.'"> '; echo 'Type<input type="text" name="type'.$index.'">'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; echo '<br/>'; } echo '<input type="submit"/>'; echo '</form>'; } else echo "Number of fields should be greater then 0"; } else { echo "Fill in both the fields"; } ?> <?php $conn=oci_connect('scott','tiger'); $i=0 $col = $_POST['tab']; foreach ($_POST['tab'] as $tab) { $tabname=$tab; $colnum= $col[$i]; foreach(range(1,$_POST['colnum']) as $index) { echo $_POST["colname".$index]; echo $_POST["type".$index]; echo '<br/>'; } echo "Table name =".$tabname; $query='create table '.$tabname.'('.$_POST["colname1"].' '.$_POST["type1"].')'; $statement=oci_parse($conn,$query); if(oci_execute($statement)) { echo "Table created"; } else { echo 'no table created'; } $i++;} ?> Ted What changes you made to script? Could you please point them.... Quote Link to comment https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/#findComment-1199217 Share on other sites More sharing options...
ragavbpl Posted April 9, 2011 Author Share Posted April 9, 2011 After thinking a bit, I came up with this - PHP Code: echo 'create table '.$tabname.'(';foreach(range(1,$_POST['colnum']) as $index) { echo $_POST["colname".$index].' '.$_POST["type".$index].','; } echo ')'; But this will just output the string in the form I want i.e.- create table emp(empno number,ename varchar2,) with an extra ',' at last.. But I want this full string to be inside a variable like - $query = create table emp(empno number,ename varchar2) and without that extra comma(',').. Waiting for your replies people.... Quote Link to comment https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/#findComment-1199218 Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 Since you have infinite number of columns, the form submits the information much like an array: echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; and to get the information one by one from the array: foreach ($_POST['tab'] as $tab) {...} foreach loop is needed. Ted Quote Link to comment https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/#findComment-1199224 Share on other sites More sharing options...
ragavbpl Posted April 9, 2011 Author Share Posted April 9, 2011 @Ted that is ok.. I know that.. as it works with my code.. My problem is different.. read my 2nd post in which I have written about what I came up with.. and what I want! i.e. my last post.. please help me with that! Quote Link to comment https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/#findComment-1199226 Share on other sites More sharing options...
ragavbpl Posted April 9, 2011 Author Share Posted April 9, 2011 Please somebody help!! Quote Link to comment https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/#findComment-1199283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.