SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How would you?.... Handle mutli-add form

    Hey i have a question,

    I am trying to figure out the best way to handle a form with many different rows. Right now what i have going on is there are some calculations done and the start/stop are determined by that. And i have a var being incremented by one in a while loop. Now this all works fine and dandy if all the id's are numerical increments of each other. But i know in the 'real' world this well not be the case. See i have the information for the form pulled from a table only where one column is a match. So the id's well not always be an increment of the previous match ( do you get what i mean?). So the way i was naming the inputs before ( having the id in the name) has to change. Or i need a better way to process what is returned by the form.

    -C.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Or you just make your inputs into arrays and match up the values.
    <input type='text' name='name[]' value=''><input type='text' name='wark[]'>
    <input type='text' name='name[]' value=''><input type='text' name='wark[]'>
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Doh!! <dumb>

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Just be careful to use input types that will always exist (avoid checkboxes, for example) or use a keyed array.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well here's a problem. I have had people request a check box so they can simpley tick if it happened or not.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    <input type='text' name='myname[$key]' value=''><input type='checkbox' name='check[$key]'> .....
    indexes do not need to be sequential for a foreach walk.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    <input type='text' name='myname[$key]' value=''><input type='checkbox' name='check[$key]'> .....
    indexes do not need to be sequential for a foreach walk.
    So then i could just use a foreach state meant to walk through it?
    PHP Code:
     foreach ( $_POST('myname[]') as $input) {
    blah here 

    would something like that work?

    Also have the question how could i deal with multiple inputs, say one for the date, and a hidden one for the record. i assume the same way as the example you gave. But than how could i retrieve that data in the script that processes it (all said fields well have the same id)? unless i was able to set a variable, like $x with the current id and use that to pull out all the other info from the other arrays. Would that work?

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hey Guy's!

    I have made a script and it seems to work (i am sure there is a way for it to be simplified). The one issue i am having with it, is it is printing out everything(even if a box is not ticked) or too many (about 3x what is needed) see this example

    PHP Code:
    INSERT INTO eggs(dateLaid,laidByVALUES ('2013-02-12','4'),('2013-02-12','4'),('2013-02-12','4'),('2013-02-12','4'),('2013-02-12','3'),('2013-02-12','3'),('2013-02-12','3'),('2013-02-12','3'),; 
    and here's the script.

    any advice would be wonderful!

    PHP Code:
    $totalHens post('henCount');
            
    $henId post("id");
            
    $i 0;
            
    $date post("date");
            
    $laid post("laid");


            
    $query ="INSERT INTO eggs(dateLaid,laidBy) VALUES ";

            foreach (
    $henId as $input ) {
                    foreach (
    $laid as $hasLaid => $value) {
                        if(
    $value == 'laid'){
                            foreach (
    $date as $theDate => $dateLaid) {
                                
    $query .= "('{$dateLaid}','{$input}')";
                                    if(
    $i $totalHens){
                                        
    $query  .= ",";
                                    }
                            }
                        }
                    }
                
    $i++;
            }

            
    $query .= ";";
            return 
    $query
    i had to do the foreach for laid, and date, or else it was not getting the correct data. As i said earler i am sure there is a way to make this cleaner. I just kind of hacked everything together

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Use the keys.

    Assuming you have two arrays:
    $VarOne and $VarTwo

    and the values of $VarOne correspond to the values of $VarTwo using their keys
    (So $VarOne[0] corresponds to $VarTwo[0], if it exists. Note that this is a LEFT joining; every value in VarOne will be used; any values in VarTwo that have no key in VarOne will not be walked.)
    PHP Code:
    $VarOne = array(
    => "Moo",
    => "Quack"//Note: This has no corresponding value in $VarTwo.
    => "Splat"
    );

    $VarTwo = array(
    => "Cow",
    => "Chicken"//This has no value in $VarOne
    => "Slime"
    ); 
    In order to walk and associate these values in a meaningful way, using all of the values in $VarOne:

    PHP Code:
    foreach($VarOne AS $key => $sound) {
      echo 
    "Sound: ".$sound.", Animal: ".$VarTwo[$key];

    Result:
    Code:
    Sound: Moo Animal: Cow
    Sound: Quack Animal:     (This would also generate an "Index not found" error)
    Sound: Splat Animal: Slime
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •