preg_match and iteration with [] brackets

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

  1. #1
    I have textboxes that add to the form as I go:

    textbox1 through textbox8

    each one has an iteration:

    textbox1[1] through textbox8[1]
    textbox1[2] through textbox8[2]
    textbox1[3] through textbox8[3]
    textbox1[4] through textbox8[4]

    and so on...

    I want to echo/print out through php all the values entered in the textboxes

    I used:

    foreach($_POST as $key => $val){
      if(preg_match("/textbox1\d+/", $key)){
        $msg .= "<tr><td>$val</td></tr>";
      };
    PHP:
    the "/textbox1\d+/" part is where it gets confusing.

    I know it works if I dont have the [] brackets but I kinda need them for another code

    I tried "/textbox1[\d+\]/" and it still doesn't work :confused:

    What am I doing wrong?
     
    jodarecode, Nov 7, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You are wasting the system resources here.
    Instead you must use array in your HTML form itself
    
    <form>
       <input type="text" name="textbox[1][1]" />
       <input type="text" name="textbox[2][1]" />
       <input type="text" name="textbox[3][1]" />
       <input type="text" name="textbox[4][1]" />
    ...
    ..
    .
    </form>
    
    HTML:
    
    for($i=1; $i<9; $i++)
    {
    $var=$_POST['textbox'][$i]['1'];
    // handle the vars as you want.
    }
    
    PHP:
     
    rohan_shenoy, Nov 7, 2008 IP
  3. jodarecode

    jodarecode Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I cant change tha way its set up because of other codes accessing the textboxes and would take a while to try to search iron all of them out.

    I was shown this in another forum:

    echo '<pre>';// save making pretty formating for a demo
    foreach($_POST as $key=>$val){
      if(preg_match("/^textbox/", $key)){
         echo "\n{$key} => ";
         var_dump($val);
      }
    }
    echo '</pre>';
    PHP:
    and I replied:

    Ok, I see how that works but how to I post the values from the textbox's only? I see it dumps averything in the $val, im kinda new as far a extracting valus in this manner

    this is what i've be working on:

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

    I need it printed out just like that as you can see with the TD's but values from the textboxes only
     
    jodarecode, Nov 7, 2008 IP
  4. jodarecode

    jodarecode Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4

    Ok.... Im liking your idea much better rohan_shenoy's

    now heres what I have:

    for($i=1; $i<9; $i++)
    {
    
    $var1=$_POST['textbox1'][$i];
        $msg .= "<tr><td>$var1</td>";
    
    $va2r=$_POST['textbox2'][$i];
        $msg .= "<td>$var2</td>";
    
    $var3=$_POST['textbox3'][$i];
        $msg .= "<td>$var3</td>";
    
    $var4=$_POST['textbox4'][$i];
        $msg .= "<td>$var4</td>";
    
    $var5=$_POST['textbox5'][$i];
        $msg .= "<td>$var5</td>";
    
    $var6=$_POST['textbox6'][$i];
        $msg .= "<td>$var6</td>";
    
    $var7=$_POST['textbox7'][$i];
        $msg .= "<td>$var7</td>";
    
    $var8=$_POST['textbox8'][$i];
        $msg .= "<td>$var8</td></tr>";
    
    };
    PHP:

    except:
    for($i=1; $i<9; $i++)
    PHP:
    Is limited to 8 rows. If lets say 30 rows were filled in and I said anything under 100
    for($i=1; $i<101; $i++)
    PHP:
    , then it still prints many empty rows after the first thirty, I only need thirty and not a huge whitespace below....keep in mind this form I am creating has infinite number of rows that can be created and needs to stop at the last entry entered.

    now I know the foreach statment would be best but it wont take the square brackets in which I require for another code, I've tried all regex patterns and nothing. Closest I came was the code I have above.

    Any help appreciated:confused:
     
    jodarecode, Nov 10, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Your code for($i=1; $i<101; $i++) actually means that:
    for textbox1 , there is textbox1[1] to textbox1[100]
    for textbox2 , there is textbox2[1] to textbox2[100]
    for textbox3 , there is textbox3[1] to textbox3[100]
    etc...

    That is the place i get confused...Anyway I try to help with what i understand.

    I assume every textbox got an array of 4 items (according to your first post)

    Let's say your form got 1 submit button with name="something" , and the rest of them is textbox (not fixed as u said)

    
    $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:
     
    ads2help, Nov 11, 2008 IP
  6. jodarecode

    jodarecode Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, first thanks for your post they work fine but one thing, I guess I have to to be a little more clearer

    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"
    • You know the scripts I have used and played with in above posts
    • I cannot change any names as it will affect other scripts for calculations of many sorts

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

    Thanks
     

    Attached Files:

    jodarecode, Nov 17, 2008 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    I tested this on PHP5 with GET so I didn't have to build a form, hopefully I understood right.

    <?php
    
    $corrupt	= false;
    $length		= -1;
    
    for($i = 1; $i < 9; $i++)
    {
    	$key = "textbox$i";
    	if(isset($_GET[$key]))
    	{
    		$$key =& $_GET[$key];
    		if($length == -1)
    		{
    			$length = count($$key);
    		}
    		if(count($$key) != $length)
    		{
    			$corrupt = true;
    			break;
    		}
    	}
    	else
    	{
    		$corrupt = true;
    		break;
    	}
    }
    
    if(!$corrupt)
    {
    	echo '<table border="1">';
    	foreach($textbox1 as $key => $val)
    	{
    		echo sprintf('<tr>' . str_repeat("<td>%s</td>\n", 8) . '</tr>',
    			$val,
    			$textbox2[$key],
    			$textbox3[$key],
    			$textbox4[$key],
    			$textbox5[$key],
    			$textbox6[$key],
    			$textbox7[$key],
    			$textbox8[$key]
    		);
    	}
    	echo '</table>';
    }
    
    ?>
    PHP:
    ?textbox1[1]=a&textbox2[1]=b&textbox3[1]=c&textbox4[1]=d&textbox5[1]=e&textbox6[1]=f&textbox7[1]=g&textbox8[1]=h&textbox1[2]=aa&textbox2[2]=bb&textbox3[2]=cc&textbox4[2]=dd&textbox5[2]=ee&textbox6[2]=ff&textbox7[2]=gg&textbox8[2]=hh
    Code (markup):
     
    joebert, Nov 18, 2008 IP