newb Posted June 5, 2010 Share Posted June 5, 2010 i have a number string which is any random integer of 1-10. how can i make it so that if the string turns out to be 5, there will be 50 added on for every number under it. for example: $string = '5'; 1: +0 2: +50 3: +50 4: +50 5: +50 etc etc.. can anyone help? Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 Could you show the actual numbers inside 0:, 1:, 2:, etc.? Also, what would those numbers be if the number string was not 5 but rather 8? Quote Link to comment Share on other sites More sharing options...
newb Posted June 5, 2010 Author Share Posted June 5, 2010 Could you show the actual numbers inside 0:, 1:, 2:, etc.? yes the actual numbers would be 1: 50 2: 100 3: 150 4: 200 5: 250 Also, what would those numbers be if the number string was not 5 but rather 8? they would just continue to add on Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 So you want a loop that loops X times (5 or 8 in our story so far), and prints out 50, 100, etc? foreach ($i = 1; $i <= $number_of_values; $i++) echo $i * 50; I renamed $string to $number_of_values, I think it is a better name. Quote Link to comment Share on other sites More sharing options...
mraza Posted June 5, 2010 Share Posted June 5, 2010 <?php $value = 8; for($i = 1; $i <= $values; $i++) { echo $i . ':'; echo $i * 50; echo '<br />'; } ?> hope this what u wants Quote Link to comment Share on other sites More sharing options...
newb Posted June 5, 2010 Author Share Posted June 5, 2010 ok im getting close: the problem is that it is adding +50 on the first value which is 1 (i dont want this) anyway to fix this? i just want it to add +50 to the all the other values except the first so it should be as. 1. +0 2. +50 3. +50 etc etc. Quote Link to comment Share on other sites More sharing options...
mraza Posted June 5, 2010 Share Posted June 5, 2010 <?php $values = 8; for($i = 1; $i <= $values; $i++) { echo $i . ':'; if($i == 1) { echo $i * 0; } else { echo ($i * 50) - 50; } echo '<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
newb Posted June 5, 2010 Author Share Posted June 5, 2010 works perfectly, thanks! Quote Link to comment Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 Here's a shorter version with the same results: <?php foreach ($i = 0; $i < $number_of_values; $i++) echo "$i: " . $i * 50 . '<br />'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.