Changing the value using a loop

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

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

Changing the value using a loop

Post by reecec »

Hi all

sorry if this is to much to ask for but i need help on this

I will have a box where you type in how many fields you want in your database and then it will give you that many boxes see picture.

the picture has one row say i asked for 5 rows it would give you 5 rows of boxes



Image




here is my code for the form and the proccess to add the table

Code: Select all

<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>
      <td>1</td>
      <td><input name="field1" type="text" id="field1"></td>
      <td><input name="length1" type="text" id="length1"></td>
      <td><select style="font-size: 70%;" name="attribute1" id="field_0_5">
        <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="null1" id="field_0_6">
        <option value="NOT NULL" selected="selected" >not null</option>
        <option value="">null</option>
      </select></td>
      <td><input id="field_0_7" type="text" name="default1" size="12" value="" class="textfield" /></td>
      <td><select name="extra1" id="field_0_8">
        <option value=""></option>
        <option value="AUTO_INCREMENT">auto_increment</option>
      </select></td>
    </tr>
  </table>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit">
    </label>
</p>
</form>

///here is my php that gets the values of these and then creates a table

   


<?


//conection stuff 

$table=$_POST['tbname'];
$field1=$_POST['field1'];
$field2=$_POST['field2'];
$field3=$_POST['field3'];

$length1=$_POST['$length1'];
$length2=$_POST['$length2'];
$length3=$_POST['$length3'];

$attribute1=$_POST['$attribute1'];
$attribute2=$_POST['$attribute2'];
$attribute3=$_POST['$attribute3'];


$maketable="CREATE TABLE $table(
$field1 varchar($length1) $attribute1 NOT NULL default '',
$field2 varchar($length2) $attribute2 NOT NULL default '',
$field3 varchar($length3) $attribute3 NOT NULL default '')"; 

//so this would be if you had a 3 field table i would need to make it so this would make it for as many rows you have chosen sorry if i have not explained it well

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

?>


thanks reece
Last edited by reecec on Wed Jul 26, 2006 12:52 pm, edited 2 times in total.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

would it be something like this
where it would give me 5 rows where i have $counter <= 5 this would be a variable so a number to be chosen but for the sake of the question this should give me 5 rows of textboxes but the loop doesnt like having all the text box stuff around the $end and just wants the $end as i get this error
Parse error: parse error, unexpected T_STRING, expecting ',' or ';'

Code: Select all

<?
$end = 1; 



echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Name</th>";
echo "<th>Length</th></tr>";
while ( $end <= 5 ) {
	echo "<tr><td>";
	echo "<input name="field$end" type="text" id="field$end">";
	echo "</td><td>";
	echo "<input name="length$end" type="text" id="length$end">";
	echo "</td></tr>";
	$end = $end + 1;
}
echo "</table>";

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

Changing the value using a loop

Post by reecec »

What is wrong with my loop cant i have other code arround the $end
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It shouldn't matter, but try this...

Code: Select all

<?php
$end = 1;

echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Name</th>";
echo "<th>Length</th></tr>";
while ( $end <= 5 ) {
        echo "<tr><td>";
        echo "<input name="field{$end}" type="text" id="field{$end}">";
        echo "</td><td>";
        echo "<input name="length{$end}" type="text" id="length{$end}">";
        echo "</td></tr>";
        $end++;
}
echo "</table>";
?>
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

I relilaised what was wrong your code worked but was not needed as you said
i think this was the problem could you just confirm that im right becuase i have noticed this alot when putting text boxes in an echo

is it because you open the echo " but when you get to input name=" <name> " it sees the other speech marks and ends the echo so i opened it with '
and thanks you have replied alot to my posts and have helped me your a good mod


Thanks Reece

Code: Select all

<?php
$end = 1;

echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Name</th>";
echo "<th>Length</th></tr>";
while ( $end <= 10 ) {
        echo "<tr><td>";
        echo '<input name="field$end" type="text" id="field{$end}">';
        echo "</td><td>";
        echo '<input name="length{$end}" type="text" id="length{$end}">';
        echo "</td></tr>";
        $end++;
}
echo "</table>";
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Make sure you use single quotes (' - string literal) and double quotes (" parsed string) appropriately or you may get unexpected results.

Code: Select all

<?php
$end = 1;

echo '<table border="1" align="center">';
echo '<tr><th>Name</th>';
echo '<th>Length</th></tr>';
while ( $end <= 10 ) {
        echo '<tr><td>';
        echo '<input name="field' . $end . '" type="text" id="field' . $end . '">';
        echo "</td><td>";
        echo '<input name="length' . $end . '" type="text" id="length' . $end . '">';
        echo '</td></tr>';
        $end++;
}
echo '</table>';
?>
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

o yea i have just relised variables dont work with ' so how will i do it with " but stop the echo ending

please help


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

Post by RobertGonzalez »

Run the code I posted. It should work exactly as posted with no parse errors for line endings or mixed quotes.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read up on strings and practice, practice, practice.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

oh backslash thankyou
Post Reply