Jump to content

getting fieldnames created by a loop, using a loop


yoshida

Recommended Posts

Hi. I'm new here. My name is Bram Weinreder and I work with the IT department of a 'large' architect bureau (large by Dutch standards, we're The Netherland's 4th architect bureau and there are about 200 employees at work in three departments). Right now I'm writing a system database that should be able to function as a CMDB. I never did or saw any PHP coding until roughly three weeks ago.One of the key elements of my CMDB is adding purchase bills through a form, adding components to them and assigning their own unique serial number to them. In order to do so I created a field in the component specification form in which the user should input the number of components that have been purchased.That number (let's say five for this instance) is used by a loop to create fields in the next form (other frame), and assigning incremental fieldnames to them. The fields are auto-called snum1, snum2, snum3 etc. The component specifications and amount are saved in a session.My next challenge is to add the data to a database (dúh) using another loop. It's using the same number (five) in which the component specifications, saved in the session, are added to the database.You might've guessed it: I can't get the loop to read info from the auto generated fieldnames. the value added to the database field snum is defined $snum, so the post data should be readed as $snum=$_POST['snum$i']; (in which $i is incremental using $i++) but that doesn't work.Loop:

<?$i=0;while ($i < $numsys) {	$i++;	$snum=$_POST['snum$i'];	$query = "INSERT INTO componenten VALUES ('','$merk','$desc','$snum','','$mnum','$ctyp')";	mysql_query($query);}	mysql_close();?>

The fields and names are created using the following loop:

<?echo "<table>";$i=0;while ($i < $numsys) {$i++;echo "<tr><td>$i<td><input type=text name=snum$i><br>";}echo "</tr></table>";?>

I use a table to get the fields aligned when the designated fieldnumber exceeds nine. How do I get a script to read the proper (amount of) values, and add them to a table? Please help me. I'm pretty new at this, and it might give me some new insights to write some other kickass-coding as well. I enjoy doing it, but it frustrates the ###### out of me when things just 'don't work'. I'm using php 4.4.2Thank you very much folks.EDIT: I cracked it!!Now the loop looks like this:

<?$i=0;while ($i < $numsys) {	$i++;	$n="snum$i";	$snum=$_POST[($n)];	$query = "INSERT INTO componenten VALUES ('','$merk','$desc','$snum','','$mnum','$ctyp')";	mysql_query($query);}	mysql_close();?>

Pretty easy, no?

Link to comment
Share on other sites

Although While loops can do what your looking for, I believe it would be much easier for you to use for loops. It would lower the amount of coding you need to do.This is a for loop:for ($i=0;$i<$numsys;$i++) {content}Now the 0 should be replaced with your lower limit, its the value where $i will start the loop at.

Link to comment
Share on other sites

Although While loops can do what your looking for, I believe it would be much easier for you to use for loops. It would lower the amount of coding you need to do.This is a for loop:for ($i=0;$i<$numsys;$i++) {content}Now the 0 should be replaced with your lower limit, its the value where $i will start the loop at.

Thanks. That would make things abit easier. I suppose I'd still be able to make fieldnames (and their respective readouts) incremental by adding the suffix $i?PS I finally got sessions to work. Now I'd like to use classes (if that doesn't mean recoding every single page) because that's supposed to be safer. I might be back with some questions about that later.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...