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? Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/ 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? Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068012 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 Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068013 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. Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068015 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 Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068017 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. Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068019 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 />'; } ?> Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068022 Share on other sites More sharing options...
newb Posted June 5, 2010 Author Share Posted June 5, 2010 works perfectly, thanks! Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068024 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 />'; Link to comment https://forums.phpfreaks.com/topic/203922-for-every-number-add-50/#findComment-1068025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.