infinite rows, 8 textboxes for each row, print/echo script issue

Discussion in 'PHP' started by jodarecode, Nov 17, 2008.

  1. #1
    This is what I have come up with:

    each line has 8 textboxes named 1 - 8
    everytime I add a line its still 1 - 8 with an iteration on the end with brackets

    to show an example:

    textbox1[1] textbox2[1] textbox3[1] textbox4[1] textbox5[1] textbox6[1] textbox7[1] textbox8[1]

    I push the add button and...
    textbox1[2] textbox2[2] textbox3[2] textbox4[2] textbox5[2] textbox6[2] textbox7[2] textbox8[2]
    again....
    textbox1[3] textbox2[3] textbox3[3] textbox4[3] textbox5[3] textbox6[3] textbox7[3] textbox8[3]
    and so on..can be infinite

    foreach($_POST as $key => $val){
      if(preg_match("/textbox1[\d+\]/", $key)){
        $msg .= "<tr><td>$val</td>";
      };
      if(preg_match("/textbox2[\d+\]/", $key)){
        $msg .= "<td>$val</td>";
      };
      if(preg_match("/textbox3[\d+\]/", $key)){
        $msg .= "<td>$val</td>";
      };
      if(preg_match("/textbox4[\d+\]/", $key)){
        $msg .= "<td>$val</td>";
      };
      if(preg_match("/textbox5[\d+\]/", $key)){
        $msg .= "<td>$val</td>";
      };
      if(preg_match("/textbox6[\d+\]/", $key)){
        $msg .= "<td>$val</td>";
      };
      if(preg_match("/textbox7[\d+\]/", $key)){
        $msg .= "<td>$val</td>";
      };
      if(preg_match("/textbox8[\d+\]/", $key)){
        $msg .= "<td>$val</td></tr>";
      };
    };
    PHP:
    I cant use the above because it recognizes the [] not as plain text.

    I used:

    $num_of_textbox = count($_POST) - 1; // minus the button u have
    $items_per_textbox = 4;
    
    $loop = 1;
    for ($i=1 ; $i<=$items_per_textbox ; $i++) {
    $msg .= "<tr>\r\n";
      for ($ii=1 ; $ii<=$num_of_textbox ; $ii++) {
        $name = 'textbox'.$ii;
        $msg .= "\t<td>".$_POST[$name][$loop]."</td>\r\n";
      }
    $loop++;
    $msg .= "</tr>\r\n";
    }
    
    print $msg;
    PHP:
    works great tho Im limited and get to much white space trailing at the end if $items_per_textbox = 4; was increased to 100 and only 2 or 3 or 20 rows are filled in.


    I used DOM table delete row from http://www.mredkj.com/tutorials/tabledeleterow.html and created my own form.

    [​IMG]

    There are 8 textboxes per row, the rows can be infinite and thats the issue.

    There are other areas not shown in the image above that also print out and some that do not ie...hidden fields for calculations so I cannot print all textboxes.

    The ones that are specific to the DOM addline are the as the image above shows.

    If someone adds 27 or 52 or 74 or whatever many rows of 8 textboxes each, I need to php echo them out into an email or printer friendly page but only whats been entered and not a specific # of rows.

    Review:
    • You know the names of each textbox in each row
    • There are 8 textboxes in each row
    • The rows can be infinite
    • there are other unrelated "other textboxes not in that name group ie..textbox1[1]...could be name, date or a hidden field calculating a script"
    • I cannot change any names as it will affect other scripts for calculations of many sorts

    I hope this is descriptive enough as I've been racking my head around this one for a while.

    Thanks
     
    jodarecode, Nov 17, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Something like this?

    
    $msg = ''; // create the variable
    if (isSet($_POST['textbox1']))
    {
        // is $_POST['textbox1'] found?
        foreach($_POST['textbox1'] AS $firstkey => $notused)
        // read all textbox1 boxes, starting at 1 and ending at the lastone found in $_POST
        {
            // loop number of sub textboxes (1 to 8)
            for ($x = 1; $x < 9; $x++)
            {
                $box = "textbox" . $x;
                if (isSet($_POST[$box]) && isSet($_POST[$box][$firstkey]))
                {
                    // add message
                    $msg .= "<td>" . $_POST[$box][$firstkey] . "</td>";
                }
            }
        }
    }
    
    Code (markup):
    This code reads all boxes and checks if they exists! :) so if you have something like this.

    textbox1[1] - textbox2[1] - etc
    textbox1[2] - textbox2[2] - etc
    textbox5[1] - textbox2[5] - etc

    it will read all the data!

    hope it works for you!
     
    EricBruggema, Nov 19, 2008 IP