Jump to content

[SOLVED] Number of input fields based on int


2tonejoe

Recommended Posts

I have a database with table that has the name of the user alongside an integer from 0-4. I want to display between 0-4 <input name="text1" value="Text" type="text" size="30" /> based on the number I get from that table. . . how can I do this? I also want to name them 1, 2, 3 or 4 so that I can insert the data back into the database. . .

<?php

$query = mysql_query("SELECT number FROM table")or die(mysql_error());
$row = mysql_fetch_assoc($query);
$number = $row['number']; //this is the number between 0-4

for ($i=0; $i<$number; $i++){
   echo "<input name='text".$i."' value='Text' type='text' size='30' /><br>";
}

?>

  • 1 month later...

<?php

$query = mysql_query("SELECT number FROM table")or die(mysql_error());
$row = mysql_fetch_assoc($query);
$number = $row['number']; //this is the number between 0-4

for ($i=0; $i<$number; $i++){
   echo "<input name='text".$i."' value='Text' type='text' size='30' /><br>";
}

?>

 

The above code works great for me, but I now need it to count starting at "1". For instance, if $number is equal to 4, make four inputs named 1-4. If $number equals 6, make 6 inputs named 1-6, etc, etc. I changed the code to be the following:

<?php

$query = mysql_query("SELECT number FROM table")or die(mysql_error());
$row = mysql_fetch_assoc($query);
$number = $row['number']; //this is the number between 0-4

for ($i=1; $i<$number; $i++){
   echo "<input name='text".$i."' value='Text' type='text' size='30' /><br>";
}

?>

but when $number is equal to 4, I only get three inputs named 1-3. ???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.