Create table loop (see my last post)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thats ok! they do come from a POST but you had told me to stop that

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 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

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.
while $endfield <= 2) {
then puts these posts into varaibles the same as the text box names eg $field1 $field2

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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, in this code here...

Code: Select all

$table=$_POST['tbname'];
if ($_POST['form_create_table'] == 'yes'){
$endfield=1;
while ( $endfield <= 2) {
${$field.$endfield}=$_POST['field' . $endfield];
$endfield++;
}
Where does $field get defined?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

it doesnt im am defining it there with a number

eg $field1


i take it i have to define it on its own first


thanks reece
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Then your syntax is wrong. To get $field1 out of your code you would need to do this...

Code: Select all

$table=$_POST['tbname'];
if ($_POST['form_create_table'] == 'yes'){
    $endfield=1;
    while ( $endfield <= 2) {
        $field.$endfield = $_POST['field' . $endfield];
        $endfield++;
    }
Now that I think about it, an array may be in better in order.

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++;
    }
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

Thanks everah the array works perfect
CREATE TABLE (field1 varchar(100) default 'test1',field2 varchar(200) NOT NULL default 'test2',
it echos all the values in the right place with none overriden like before

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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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;
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks thats what I need

Created test

its all done thanks very much for your time everah
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help.
Post Reply