Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple help with while loop

    I thought I knew how to use a while loop, but I gues not, i'm still a n00b. Anyways, how do you increase a string by 1 each time in a while loop? For example, i did this (the string I am referring to is $e):
    PHP Code:
    <? $e 0;
        while(
    preg_match("/\[quote\](.*?)\[\/quote\]/ise"$formtext$matches))
        {
            
    $e $e 1;
            
    $msg str_replace($matches[0], makequoteBox($matches[1], $e), $msg);
        } 
    ?>
    The number comes out as 1 each time, why?

  • #2
    New Coder
    Join Date
    Jan 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    to increment a value use $e++

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You also want it to be the last thing in the loop so it should be
    PHP Code:
    <?php $e 0;
        while(
    preg_match("/\[quote\](.*?)\[\/quote\]/ise"$formtext$matches))
        {
            
    $msg str_replace($matches[0], makequoteBox($matches[1], $e), $msg);
            
    $e++;
        } 
    ?>
    $e is not a string. Its an integer in your case. Because you had it before $msg $e was already 1 before you even did the str_replace. Putting $e after $msg allows it to be 0 the first time.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, i understand what your saying, but it still isn't increasing at all. The number is 0 each time. Here is my entire script:
    PHP Code:
    <?php
        $send 
    $_POST['send'];
        
    $text $_POST['text'];
    if(!
    $send){
        echo 
    '<form action="" method="post"><textarea name="text"/></textarea><input type="submit" name="send"/></form>';
    }
    elseif(
    $send){
        function 
    makephpcodeBox($pcode$pID)
        {
            
    $pcode stripslashes($pcode);
            
    $pcode highlight_string($pcode);

            
    $phpc "<div style=\"border:1px solid #000000;display:table;width:350;margin:0px auto;\">
      <div style=\"background-image:url(bbcode_top.png);background-repeat:repeat-x;\">
        <a href=\"javascript:menu('pcode$pID','pexpand$pID','pcollapse$pID');\" title=\"Show Or Hide This PHP Code\">
          <span id=\"pexpand$pID\" style=\"display:none;\"><img src=\"expand.gif\" width=\"9\" height=\"9\" alt=\"[+]\" border=\"0\"/></span>
          <span id=\"pcollapse$pID\" display=\"inline\"><img src=\"collapse.gif\" width=\"9\" height=\"9\" alt=\"[-]\" border=\"0\"/></span>
        </a>
        PHP Code :: <a href=\"javascript:code_select('pcode$cID');\" title=\"Highlight The Code\">Select PHP Code</a>
      </div>
      <div id=\"pcode$cID\">
        <div style=\"background-color:#000000;height:1px;width:100%;font-size:0;\"></div>
        <div style=\"background-color:#5A2323;height:1px;width:100%;font-size:0;\"></div>
        <div style=\"background-color:#470A0A;\">$pcode</div>
      </div>
    </div>"
    ;
            return 
    $phpc;
        }
        
    $p 0;
        while(
    preg_match("/\[php\](.*?)\[\/php\]/ise"$msg$matches))
        {
            
    $msg str_replace($matches[0], makephpcodeBox($matches[1], $p), $msg);
            
    $p++;
        }
        echo 
    $text;
    }
    ?>

  • #5
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello?

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by papa_face View Post
    to increment a value use $e++
    This makes no difference.

    PHP Code:
    $e++;
    $e += 1;
    $e $e 1
    These are all the same.

    You also want it to be the last thing in the loop so it should be
    Not really. It depends on if EZE wants it to start at 0, or at 1.

    Okay, i understand what your saying, but it still isn't increasing at all.
    Echo out $p in the while() right after incrementing it. Does it show the correct value?
    Last edited by aedrin; 03-14-2007 at 03:51 PM.

  • #7
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by EZE View Post
    Okay, i understand what your saying, but it still isn't increasing at all. The number is 0 each time.
    Your while() statement looks correct. Is there definitely more than one result for the condition "preg_match("/\[php\](.*?)\[\/php\]/ise", $msg, $matches)"?

  • #8
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes i'm pretty sure. http://flashskillz.com/tests/bbcode/form.php And i don't completely understand what you mean aedrin. Like this?
    PHP Code:
    <? $p 0
        while(
    preg_match("/\[php\](.*?)\[\/php\]/ise"$msg$matches)) 
        { 
            
    $msg str_replace($matches[0], makephpcodeBox($matches[1], $p), $msg); 
            
    $p++;
        }
        echo 
    $p
        
    echo $text;
    ?>
    to see if it has the correct value? I'll try that when i get home from school and see if it outputs the correct number.
    Last edited by EZE; 03-15-2007 at 01:10 PM.

  • #9
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Close. Echo out $p inside the while loop so that every time you get to the end of the loop and add 1 to $p, it will echo it.

    PHP Code:
    <? 
    $p 
    0;  
        
    while(
    preg_match("/\[php\](.*?)\[\/php\]/ise"$msg$matches))  
        {  
            
    $msg str_replace($matches[0], makephpcodeBox($matches[1], $p), $msg);  
            echo 
    $p;
            
    $p++;
            
        } 

    echo 
    $text
    ?>
    The first echo will be 0 and increase from there.


  •  

    Posting Permissions

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