Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-04-2007, 03:10 AM   PM User | #1
EZE
New Coder

 
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
EZE is an unknown quantity at this point
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?
EZE is offline   Reply With Quote
Old 03-04-2007, 10:07 AM   PM User | #2
papa_face
New Coder

 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
papa_face is an unknown quantity at this point
to increment a value use $e++
papa_face is offline   Reply With Quote
Old 03-04-2007, 10:32 AM   PM User | #3
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-04-2007, 03:01 PM   PM User | #4
EZE
New Coder

 
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
EZE is an unknown quantity at this point
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;
}
?>
EZE is offline   Reply With Quote
Old 03-14-2007, 03:46 AM   PM User | #5
EZE
New Coder

 
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
EZE is an unknown quantity at this point
Hello?
EZE is offline   Reply With Quote
Old 03-14-2007, 03:48 PM   PM User | #6
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
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.

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

Quote:
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..
aedrin is offline   Reply With Quote
Old 03-14-2007, 04:17 PM   PM User | #7
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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)"?
__________________
JDub
http://johnnyzone.com/blog
JohnDubya is offline   Reply With Quote
Old 03-15-2007, 01:04 PM   PM User | #8
EZE
New Coder

 
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
EZE is an unknown quantity at this point
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..
EZE is offline   Reply With Quote
Old 03-15-2007, 02:58 PM   PM User | #9
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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.
__________________
JDub
http://johnnyzone.com/blog
JohnDubya is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:37 PM.


Advertisement
Log in to turn off these ads.