...

View Full Version : Simple help with while loop



EZE
03-04-2007, 04:10 AM
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):
<? $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?

papa_face
03-04-2007, 11:07 AM
to increment a value use $e++

_Aerospace_Eng_
03-04-2007, 11:32 AM
You also want it to be the last thing in the loop so it should be

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

EZE
03-04-2007, 04:01 PM
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
$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
03-14-2007, 04:46 AM
Hello?

aedrin
03-14-2007, 04:48 PM
to increment a value use $e++

This makes no difference.



$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?

JohnDubya
03-14-2007, 05:17 PM
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)"?

EZE
03-15-2007, 02:04 PM
Yes i'm pretty sure. http://flashskillz.com/tests/bbcode/form.php And i don't completely understand what you mean aedrin. Like this?
<? $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.

JohnDubya
03-15-2007, 03:58 PM
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.


<?
$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum