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

reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

CREATE TABLE test;(
its finishing the create table before it gets to the while statement


any ideas


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

Post by RobertGonzalez »

It means that $howmanyfields is either 0, 1 or not set. That is controlling the while loop and you are comparing $maketable to being less that $howmanyfields in a while loop. If that whle comparison returns false, then the while never executes.

Just after

Code: Select all

<?php
$maketable = 1
?>
add...

Code: Select all

<?php
echo '<strong>' . $howmanyfields . '</strong>';
?>
To see what is in the var $howmanyfields. Post back what you find.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

what like this

Code: Select all

$endfield=1;
$maketable="CREATE TABLE $table;(";
echo '<strong>' . $howmanyfields . '</strong>'; 
while ( $endfield <= 2) {
    $maketable .= "${$field.$endfield} varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
    $endfield++;
}
it dint echo anything so you are right so i changed $howmanyfields to 2 and i now got this far which is further but still not right

Code: Select all

CREATE TABLE test;( varchar() default '', varchar() default '',
which means it isnt putting in the values

i just also echoed ${$field.$endfield} and the value is blank so i will to sort that out and come back to you if i need help again thanks
thanks reece
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

is it not getting the value because of this
$_POST['attribute$endfield']; and isnt getting the value of $endfield in the _POST and is thinking it is attribute$endfield

Code: Select all

${$attribute.$endfield}=$_POST['attribute1$endfield'];
thanks 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

${$attribute.$endfield}=$_POST['attribute1' . $endfield];
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks everah thats sorted that and it now gets the text box value

but when i enter a value for the field name and length and defaults it puts the field name value in all boxes

heres the create table echo for a two field table
CREATE TABLE ;(testfieldname1; varchar(testfieldname1) testfieldname1 default 'testfieldname1',testfieldname1; varchar(testfieldname1) testfieldname1 default 'testfieldname1',
and here is the current code for the create table

Code: Select all

$endfield=1;
$maketable="CREATE TABLE $table;(";

while ( $endfield <= 2) {
    $maketable .= "${$field.$endfield}; varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
    $endfield++;
}
thanks for all your help the script is nearly done

reece
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

I have just sorted all the other fields as there whiles wernt working and have noticed that the last box you fill in on the form eg Defaults it will fill every thing in with the same
CREATE TABLE ;(testdefualtfield1; varchar(testdefualtfield1) testdefualtfield1 default 'testdefualtfield1',testdefualtfield2; varchar(testdefualtfield2) testdefualtfield2 default 'testdefualtfield2',

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

Post by RobertGonzalez »

So are you ok with this, or do you still need help?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

still need help as im dont know why its putting my last typed text box value in all like (name, length, defaults, extra) all become the same last value so if i typed in test in the default box as it is last it makes every thing test

why is this.

i think its to do with the while statement


sorry to be a pain


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

Post by RobertGonzalez »

Those values are being sent from post data, right? Or how are the values being set?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

yes it gets the form data from $_POST then puts it into variables like this

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

Post by RobertGonzalez »

I would probably do something like...

Code: Select all

<?php
$maketable = '';
$table=$_POST['tbname'];
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 $maketable;
?>
But I am really wondering, in all those ${$varname.$endfield} vars, where does the $varname var get set? And where does the $endfield var get set?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thats what i have tried before but it gives this

CREATE TABLE (; varchar() default '',; varchar() default '',

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

Post by RobertGonzalez »

OK, I hate to be working backwards here, but we really need to start from the beginning of your code to see what is really going on here. Please execute this snippet and post back what is displayed.

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

Post by reecec »

that wont work anyway as you have stoped the $_POST but you know what your doing most so heres what it gave
Field is
Length is
Attribute is
Null is
Default is
Endfield is
Maketable is CREATE TABLE (
(
Post Reply