$button0 = "candy"; $button1 = "pop"; $buttons = 1; $counter = 0; while ($counter < $buttons) { $printbutton = "$"."button".$counter; echo($printbutton."<br>"); // echos $button0 but not the value of $button0 $counter = $counter+1; } PHP: I can get my code to output $button0 but not the value of $button0. I think I need to convert the string to a variable or something like that but I'm stuck. Please help. Thanks
Will this script work for your purposes? Note: Since the $printbutton variable is declared inside the part of the code that will only get executed when $counter is less then $buttons, $printbutton will only be printed once.... when $counter = 0. Because of that, I don't really see the purpose in this script but perhaps you have a reason for it: $button0 = 'candy'; $button1 = 'pop'; $buttons = '1'; $counter = '0'; while ($counter < $buttons) { $printbutton = $button0; echo $printbutton.'<br />'; $counter = $counter+1; } Code (markup): Here is another version... $button0 = 'candy'; $button1 = 'pop'; $buttons = '1'; $counter = '0'; while ($counter < $buttons) { if ($counter == '0') { $printbutton = $button0; } else if ($counter == '1') { $printbutton = $button1; } echo $printbutton.'<br />'; $counter = $counter+1; } Code (markup):
The second version is closer, but I'm trying to automate it so I don't have to write 30 some lines of code as I have 30 buttons. I only showed two in the example as I didn't want to make it look complicated. $printbutton must equal $buttonx where x = $counter
Add a loop to do the script from button 1 to button 30: for ($i=1; $i<=30; $i+=1) { //do something here } PHP:
Well, I could write you a script to do that based on your original script, but it would probably follow a lot of bad coding practices. How hard would it be for you to put your buttons in an array? If the buttons are declared in the script above, that would involve simply changing this: $button0 = 'Candy'; Code (markup): To this: $buttons[] = 'Candy'; Code (markup): And then you loop through the array like this: foreach ($buttons as $value) { echo $value.'<br />'; } Code (markup): So if you could use arrays, the final code would be like this: $buttons[] = 'candy'; $buttons[] = 'pop'; foreach ($buttons as $value) { echo $value.'<br />'; } Code (markup): As you can see, the code would be a lot simpler, if this works for your purposes.
Oh, and if you want a max of 30, even if the array contains more then 30 entries, you could use this code: $buttons[] = 'candy'; $buttons[] = 'pop'; $counter = '0'; foreach ($buttons as $value) { if ($counter < '30') { echo $value.'<br />'; $counter = $counter+1; } } Code (markup): Hope it helps!
Hehe, its quite easy really, thanks to PHP's variable variables. Yes, variable variables! Notice the $$printbutton Hope this helps, Sham.
Was this the point? (Honestly curious, not trying to be inflammatory). Here's my impression (and a problem I find more interesting, although I'm probably missing the entire point): (Disclaimer: This is completely untested) Form Creator: $aCounters = { 'Popcorn', 'peanuts', 'beer', 'sodas', 'hot dogs' }; for($i=0; $i<$lastButton; $i++) { ?> <label for='val<?php echo $i; ?>'>Count of <?php echo $aCounters[$i]; ?></label> <?php // Ideally, you'd check this in JavaScript for really screwy // values, though you also have to check it on the server <input type='textbox' id='<?php echo $aCounters[$i]; ?> value='<?php echo $aCounters[$i]; ?>' /> } Code (markup): To generate the actual buttons, or some such. Now you need a handler. Something like: $i = 0; while(isset($_POST[$i])) { echo $_POST[$i] ."<br />\n"; } Code (markup): That implementation's probably way off. Esp. if there are other variables involved (probably). And I hate websites that try to do layout without CSS. But I think that's probably the direction I'd head for at least a first draft, if this were my project. Then again, I probably read too much into the first post. I tend to read too much into things.