#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    14
    I want to create a form that will enable a user to insert (x) number of items into a table.

    The trick is that the items are all going into the same feild in the table.

    To do this I will need to create a loop that will insert each consecutive record into the table.

    For example...

    A user chooses to insert 3 records. When he presses enter, the next page is created with a form similar to the follwing:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <input type="text" name="record1">
    <input type="text" name="record2">
    <input type="text" name="record3">
    [/code]

    On the subsequent page I'll have three variables to insert into the database - $record1, $record2, $record3.

    The insertion loop is the mystery:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    while ($counter <= $numberofitems) {
    $add = "INSERT INTO table (record) VALUES ('$record')";
    $result = mysql_query($add,$db);
    $counter = ++$counter
    }
    [/code]

    If there was a way to print the value of $counter to the end of the variable name $record as the script looped through, I'd have it made.

    On the first loop:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $add = "INSERT INTO table (record) VALUES ('$record1')";
    [/code]

    On the second loop:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $add = "INSERT INTO table (record) VALUES ('$record2')";
    [/code]

    On the third loop:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $add = "INSERT INTO table (record) VALUES ('$record3')";
    [/code]

    I'm not sure how that can be done though...

    Ideas?

    TIA, Mike
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,640
    Rep Power
    4476
    Of course it can be done. If you can explain it, someone can program it. There are two ways you can do this...arrays and variable variables...

    Arrays: (recommended)

    Name your text boxes like this, all of them the same:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <input type=text name=record[]>
    [/code]
    Then, on your processing page, use this:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    for($count=0;$count<count($record);$count++)
    {
    $result = mysql_query("INSERT INTO table (record) VALUES ('$record[$count]')");
    }
    [/code]
    That's it for arrays.


    Variable Variables:

    Read the manual for an explanation on these in case you don't understand. Name your text boxes the way you are now, record1, record2, etc...

    Then use this to process them, assuming you already know $numberofitems:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $count=1;
    while($count <= $numberofitems)
    {
    $var = "record" . $count++;
    $record_num = $$var;
    $result = mysql_query("INSERT INTO table (record) VALUES ('$record_num')");
    }
    [/code]
    Let me know if you have any questions.

    ---John Holmes...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    14
    John,

    Both examples work like a charm. I follow the flow of the second fairly well, but the array method is a slightly foggy.

    I take it the [] commands the form to send over a series of data associated with the same name. The data is sent like:

    record[1], record[2], record[3], and so on.

    Once to the processing page we use a count routine to let php know what peice of data to insert in the table. Otherwise it might just insert the first peice of data over and over.

    count($record) assigns(for lack of a better word right now) a value for the total number of data peices for the variable $record to the variable $count. Once the FOR routine reaches the count($record) value it exits and continues doing other things..

    Clarification/correction welcomed.

    Thanks for the help,
    Mike



    [This message has been edited by mstembri (edited November 17, 2000).]
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,640
    Rep Power
    4476
    I'm surprised you understood the variable variables one first. That usually confuses everyone.

    Whenever you name a form element with [], php will automatically turn it into an array. The first element with be 0, then it just keeps assigning them for how ever many elements there are. You use count($array) to see how many elements are in it. You can do this with any form element, text boxes, checkboxes, file inputs, etc....

    So if you have 3 text boxes named record[], php is going to turn them into $record[0], $record[1], and $record[2]. count($record) will equal 3.

    for($count=0;$count<count($record);$count++)

    is saying to start at zero ($count=0), count by 1 ($count++) until you reach how many elements are in $record ($count<count($record))

    Hope that clears things up and solves your problem. Happy programming.

    ---John Holmes...

IMN logo majestic logo threadwatch logo seochat tools logo