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. . .

Link to comment
Share on other sites

<?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>";
}

?>

Link to comment
Share on other sites

  • 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. ???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.