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 » Thu Jul 27, 2006 8:53 am
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
Last edited by
reecec on Thu Jul 27, 2006 2:26 pm, edited 2 times in total.
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Thu Jul 27, 2006 9:34 am
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
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Thu Jul 27, 2006 9:45 am
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
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Thu Jul 27, 2006 11:26 am
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> </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";
}
}
?>
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu Jul 27, 2006 1:14 pm
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Thu Jul 27, 2006 2:01 pm
thank you that worked perfect
thanks reece
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Thu Jul 27, 2006 2:14 pm
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
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Jul 27, 2006 2:35 pm
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";
}
?>
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu Jul 27, 2006 2:35 pm
You can't put a while loop inside double quotes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Thu Jul 27, 2006 3:13 pm
so what do i do
this isnt posible is it
thanks reece
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Jul 27, 2006 3:15 pm
You do like I did in my code. Generate a SQL statement string piece by piece, then execute it.
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Thu Jul 27, 2006 3:43 pm
sorry everah i dint see your code i will give that a go thanks
thanks reece
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Jul 27, 2006 3:53 pm
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.
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Thu Jul 27, 2006 3:55 pm
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
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Fri Jul 28, 2006 12:46 am
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";
}
*/
?>