Create table loop (see my last post)
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I guess what I was really after was trying to find out what the values of each of those vars were. I had assumed (sorry about that) that those vars came from post. If they didn't, please run the script, but instead of hitting the db with the values, echo them so I can see what the values are. Thanks.
thats ok! they do come from a POST but you had told me to stop that
this is what you told me to do
and all my $_POSTS have been stoped from the script
and in the loop it starts the var $maketable again is this right
eg $maketable="CREATE TABLE $table (";
then in the loop
$maketable .= "${$field.$endfield}; varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
I appreciate your help very much thanks reece
this is what you told me to do
Code: Select all
<?php
if ($_POST['form_create_table'] == 'yes'){
$maketable="CREATE TABLE $table (";
/* $endfield=1;
while ( $endfield <= 2) {
${$field.$endfield}=$_POST['field' . $endfield];
${$length.$endfield}=$_POST['length' . $endfield];
${$attribute.$endfield}=$_POST['attribute' . $endfield];
${$null.$endfield}=$_POST['null' . $endfield];
${$default.$endfield}=$_POST['default' . $endfield];
$maketable .= "${$field.$endfield}; varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
$endfield++;
}
*/
echo '<h1>Field is ' . $field . '</h1>';
echo '<h1>Length is ' . $length. '</h1>';
echo '<h1>Attribute is ' . $attribute. '</h1>';
echo '<h1>Null is ' . $null. '</h1>';
echo '<h1>Default is ' . $default. '</h1>';
echo '<h1>Endfield is ' . $endfield. '</h1>';
echo '<h1>Maketable is ' . $maketable. '</h1>';
}
?>and in the loop it starts the var $maketable again is this right
eg $maketable="CREATE TABLE $table (";
then in the loop
$maketable .= "${$field.$endfield}; varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
I appreciate your help very much thanks reece
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
OK, so here's my question...
In the script above that you just quoted, those vars that are getting echo'ed, where do they get set? Because as it is right now, they are undefined and you are trying to use them as part of another var. Your script cannot work that way. PHP will try to parse those vars into the other var that you are creating on the left side of the equal sign. Is this the case?
In the script above that you just quoted, those vars that are getting echo'ed, where do they get set? Because as it is right now, they are undefined and you are trying to use them as part of another var. Your script cannot work that way. PHP will try to parse those vars into the other var that you are creating on the left side of the equal sign. Is this the case?
Sorry not sure how to answer you question i will try my best.
My script currently asks how many fields they need and it loops the form as many times as they need every time it loops it adds 1 to the end value
eg. fieldname1 fieldname 2
then php retrives this by again looping and taking the values from the post
eg
$_POST['length' . $endfield];
length1
length2
length3
depending on how many fields they need at the moment the loops are set for 2 fields just for testing.
then creates the table using those vars from the post
thanks reece
My script currently asks how many fields they need and it loops the form as many times as they need every time it loops it adds 1 to the end value
eg. fieldname1 fieldname 2
then php retrives this by again looping and taking the values from the post
eg
$_POST['length' . $endfield];
length1
length2
length3
depending on how many fields they need at the moment the loops are set for 2 fields just for testing.
then puts these posts into varaibles the same as the text box names eg $field1 $field2while $endfield <= 2) {
then creates the table using those vars from the post
Code: Select all
$table=$_POST['tbname'];
if ($_POST['form_create_table'] == 'yes'){
$endfield=1;
while ( $endfield <= 2) {
${$field.$endfield}=$_POST['field' . $endfield];
$endfield++;
}
$endfield=1;
while ( $endfield <= 2) {
${$length.$endfield}=$_POST['length' . $endfield];
$endfield++;
}
$endfield=1;
while ( $endfield <= 2) {
${$attribute.$endfield}=$_POST['attribute' . $endfield];
$endfield++;
}
$endfield=1;
while ( $endfield <= 2) {
${$null.$endfield}=$_POST['null' . $endfield];
$endfield++;
}
$endfield=1;
while ( $endfield <= 2) {
${$default.$endfield}=$_POST['default' . $endfield];
$endfield++;
}
$endfield=1;
$maketable="CREATE TABLE $table;(";
while ( $endfield <= 2) {
$maketable .= "${$field.$endfield}; varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
$endfield++;
}
echo $maketable;thanks reece
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Ok, in this code here...
Where does $field get defined?
Code: Select all
$table=$_POST['tbname'];
if ($_POST['form_create_table'] == 'yes'){
$endfield=1;
while ( $endfield <= 2) {
${$field.$endfield}=$_POST['field' . $endfield];
$endfield++;
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Then your syntax is wrong. To get $field1 out of your code you would need to do this...
Now that I think about it, an array may be in better in order.
Code: Select all
$table=$_POST['tbname'];
if ($_POST['form_create_table'] == 'yes'){
$endfield=1;
while ( $endfield <= 2) {
$field.$endfield = $_POST['field' . $endfield];
$endfield++;
}Code: Select all
$table=$_POST['tbname'];
$field = array();
if ($_POST['form_create_table'] == 'yes'){
$endfield=1;
while ( $endfield <= 2) {
$field[$endfield] = $_POST['field' . $endfield];
$endfield++;
}Thanks everah the array works perfect
but these one last small thing how do I end my create table in need to put the closing
Apart from these last things I think we are there
thanks everah
reece
it echos all the values in the right place with none overriden like beforeCREATE TABLE (field1 varchar(100) default 'test1',field2 varchar(200) NOT NULL default 'test2',
but these one last small thing how do I end my create table in need to put the closing
also I have a comma seperating the each field its creating how do I make it stop this on the last one or will it not effect the query if it is there with noting after")
Code: Select all
$endfield=1;
$maketable="CREATE TABLE $table(";
while ( $endfield <= 2) {
$maketable .= "$field[$endfield] varchar($length[$endfield]) $null[$endfield] default '$default[$endfield]',";
$endfield++;
}
echo $maketable;Apart from these last things I think we are there
thanks everah
reece
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Code: Select all
$endfield=1;
$maketable="CREATE TABLE $table(";
while ( $endfield <= 2) {
$delim = ( $endfield == 2) ? '' : ', ';
$maketable .= "$field[$endfield] varchar($length[$endfield]) $null[$endfield] default '$default[$endfield]'$delim";
$endfield++;
}
echo $maketable;- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA