Page 1 of 3

Create table loop (see my last post)

Posted: Thu Jul 27, 2006 8:53 am
by reecec

Code: Select all

$howmanyfields=$_POST['howmanyfields2'];
$table=$_POST['tbname'];
if ($_POST['form_create_table'] == 'yes'){
$$endfield=1;
while ( $endfield <= $howmanyfields ) {
$field${$}endfield=$_POST['field$endfield'];
$endfield++;
I dont think this is right i have put to variables next to each other

$field${$}endfield

$$endfield will be increasing by one each time on the loop so the variable will be $field1=$_POST['field$endfield']; and then on the next loop round it will be $field2=$_POST['field$endfield']; and i need the post to get a text box with the name field$endfield as i have eriler used the same loop function to declare the names of text boxes in the same way.

Hope someone can help

thanks reece

Posted: Thu Jul 27, 2006 9:34 am
by Jenk
I'm not quite sure I understand what you are asking, but here are some examples of variable variables.

Code: Select all

$hello = 'world';
$world = 'Hello World!';

echo $$hello; //outputs Hello World!


$i = 0;
$array = array('a', 'b', 'c', 'd', 'e');

foreach ($array as $ind) {
    ${'a' . $ind} = $i++;
}

echo $ac; //outputs 2
Hope this helps :)

Posted: Thu Jul 27, 2006 9:45 am
by reecec
thanks for your reply im using $field$endfield=

so im using to variables to create one var

so if $endfield was 1

$field1=

if $endfield was 2

$field2=


but php doesnt understand

$field$endfield=

thanks

Posted: Thu Jul 27, 2006 11:26 am
by reecec
This is my code


It asks how many fields the wamt to create a new table then once the chose a number it gives them the options like varchar for however many fields they need all the text boxes are named things like null but loops round changing every time like if they asked for 3 colums it would call each text box nulll1 then null2 then null3 once they submit that it tries to pull the data from the fields like $null$endfield=$_POST['null$endfield']; but it doesnt like it

please i hope someone understands my code. this code it realy anoying now

im a basicly doing the same as PHPMYADMIN create table but a lot simpler.

thanks reece


theres the code....

Code: Select all

<form name="form1" method="post"
action="#">
<input name="howmanyfields" type="text" id="howmanyfields">
<input type="hidden" name="form_is_sent" value="yes"><input type="submit" name="Go" value="Go"></td>
</form>

<?php
if ($_POST['form_is_sent'] == 'yes'){
$howmanyfields=$_POST['howmanyfields'];
echo "<input type=\"hidden\" name=\"howmanyfields2\" value=\"$howmanyfields\">";
echo '
<form name="form1" method="post"
action="maketable.php">
  <label>
  Table Name  </label>
  <p>
    <input name="tbname" type="text" id="tbname">
  </p>
  <p>&nbsp; </p>
  <table border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#CCCCCC">
    <tr>
      <td width="30">Field</td>
      <td width="144"><div align="center">Name</div></td>
      <td width="144"><div align="center">Length</div></td>
      <td width="201"><div align="center">Attributes</div></td>
      <td width="80">Null</td>
      <td width="81">Default</td>
      <td width="81">Extra</td>
    </tr>
    <tr>

  ';

$end = 1;
    
echo "";
while ( $end <= $howmanyfields ) {
        echo "   <td>$end</td>    <td><input name=\"field$end\" type=\"text\" id=\"field$end\"></td>
      <td><input name=\"length$end\" type=\"text\" id=\"length$end\"></td>
      <td><select style=\"font-size: 70%;\" name=\"attribute$end\" id=\"attribute$end\">
      <option value=\"\" selected=\"selected\"></option>
        <option value=\"UNSIGNED\">UNSIGNED</option>
        <option value=\"UNSIGNED ZEROFILL\">UNSIGNED ZEROFILL</option>
        <option value=\"ON UPDATE CURRENT_TIMESTAMP\">ON UPDATE CURRENT_TIMESTAMP</option>
      </select></td>
      <td><select name=\"null$end\" id=\"null$end\">
        <option value=\"NOT NULL\" selected=\"selected\" >not null</option>
        <option value=\"\">null</option>
      </select></td>
      <td><input id=\"default$end\" type=\"text\" name=\"default$end\" size=\"12\" value=\"\" class=\"default\" /></td>
      <td><select name=\"extra$end\" id=\"extra$end\">
        <option value=\"\"></option>
        <option value=\"AUTO_INCREMENT\">auto_increment</option>
      </select></td></tr> ";
        $end++;
}
echo '</table>
  <label>
    <input type="submit" name="Submit" value="Submit">
    </label>';
} 
?>

<input type="hidden" name="form_create_table" value="yes">
</form>

<?php
$howmanyfields=$_POST['howmanyfields2'];
$table=$_POST['tbname'];
if ($_POST['form_create_table'] == 'yes'){
$$endfield=1;
while ( $endfield <= $howmanyfields ) {
$field${$}endfield=$_POST['fieldendfield'];
$endfield++;
}
$endfield=1;
while ( $endfield <= $howmanyfields ) {
$length$endfield=$_POST['length$endfield'];
$endfield++;
}
$endfield=1;
while ( $endfield <= $howmanyfields ) {
$attribute$endfield=$_POST['attribute1$endfield'];
$endfield++;
}
$endfield=1;
while ( $endfield <= $howmanyfields ) {
$null$endfield=$_POST['null$endfield'];
$endfield++;
}
$endfield=1;
while ( $endfield <= $howmanyfields ) {
$default$endfield=$_POST['default$endfield'];
$endfield++;
}
$endfield=1;
while ( $endfield <= $howmanyfields ) {
$extra$endfield=$_POST['extra$endfield'];
$endfield++;
}

$endfield=1;
$maketable="CREATE TABLE $_POST['tbname'];(
while ( $endfield <= $howmanyfields ) {

$field$end varchar($length$end) $null$end $default$end '',
$endfield++;
}

$domaketable=mysql_query($maketable);
if ($domaketable) {
echo "Created $field1";
}
else
{
echo "Error $field1";
}
}
?>

Posted: Thu Jul 27, 2006 1:14 pm
by pickle
You want:

Code: Select all

${$field.$endfield}

Posted: Thu Jul 27, 2006 2:01 pm
by reecec
thank you that worked perfect


thanks reece

Posted: Thu Jul 27, 2006 2:14 pm
by reecec
just for the last bit now

Code: Select all

$endfield=1;
$maketable="CREATE TABLE $table;(
while ( $endfield <= $howmanyfields ) {

${$field.$endfield} varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',
$endfield++;
}
";
$domaketable=mysql_query($maketable);
if ($domaketable) {
echo "Created $table";
}
else
{
echo "Error creating $table";
}
}
?>

this isnt going to work

I think it is because when its creating the table it counts the $endfield++ as part of the table creation or when its looped its not still creating the table hope someone can see

this doesnt give any php error just my own error i have set to check


thanks reece

Posted: Thu Jul 27, 2006 2:35 pm
by RobertGonzalez

Code: Select all

<?php
$endfield=1;
$maketable="CREATE TABLE $table;(";
while ( $endfield <= $howmanyfields ) {
    $maketable .= "${$field.$endfield} varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
    $endfield++;
}

$domaketable=mysql_query($maketable);
if ($domaketable) {
    echo "Created $table";
} else {
    echo "Error creating $table";
}
?>

Posted: Thu Jul 27, 2006 2:35 pm
by pickle
You can't put a while loop inside double quotes.

Posted: Thu Jul 27, 2006 3:13 pm
by reecec
so what do i do


this isnt posible is it


thanks reece

Posted: Thu Jul 27, 2006 3:15 pm
by RobertGonzalez
You do like I did in my code. Generate a SQL statement string piece by piece, then execute it.

Posted: Thu Jul 27, 2006 3:43 pm
by reecec
sorry everah i dint see your code i will give that a go thanks


thanks reece

Posted: Thu Jul 27, 2006 3:53 pm
by RobertGonzalez
Check the syntax output in my code before hitting your database with it. I am not sure it is perfect and I cannot test it. SO before using it with mysql_query, try echoing out $sql to see what is going to hit your database.

Posted: Thu Jul 27, 2006 3:55 pm
by reecec
i tryed it and it echos this CREATE TABLE test;(Error creating test



CREATE TABLE test;(Error creating test


so CREATE TABLE test; is all it creates

and then Error creating test is just the error



thanks for helping reece

Posted: Fri Jul 28, 2006 12:46 am
by RobertGonzalez
Try this and post back what is returned...

Code: Select all

<?php
$endfield=1;
$maketable="CREATE TABLE $table;(";
while ( $endfield <= $howmanyfields ) {
    $maketable .= "${$field.$endfield} varchar(${$length.$endfield}) ${$null.$endfield} default '${$default.$endfield}',";
    $endfield++;
}

echo $maketable;
/*
$domaketable=mysql_query($maketable);
if ($domaketable) {
    echo "Created $table";
} else {
    echo "Error creating $table";
}
*/
?>