views:

246

answers:

2

Is there a better way to structure this, using html and php?

If I had a billion values, this would be a bad way to set them up, not to mention time consuming as well?

I am a newb, and I am pretty sure there is a better way, however, my way of doing it seems to be the long way, as in long division, I am pretty sure there are easier methods of setting this up, and that's what I am looking for, or asking?

<select name="dimes" >
    <option value=" ">--Select Dimes---</option>
    <option value=".10">10c</option>
    <option value=".20">20c</option>
    <option value=".30">30c</option>
    <option value=".40">40c</option>
    <option value=".50">50c</option>
    <option value=".60">60c</option>
    <option value=".70">70c</option>
    <option value=".80">80c</option>
    <option value=".90">90c</option>
    <option value="1.00">$1.00</option>
    </select>

Thank you for not flaming the Newb, I am still learning.

+4  A: 

Do you mean for generating the list?

Something similar to this should suffice:

<?php for($i=0.1;$i<=1;$i+=0.1): ?>
       <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
Tom
Yeah thanks, it's for generating the list, thanks, but what's the deal with : ?, I know it's shorthand notation but what does it mean:?
Newb
This is just an alternative for loop syntax. It allows you to drop in and out of HTML without using the usual { } syntax and I find it much cleaner to read.http://uk.php.net/manual/en/control-structures.alternative-syntax.php
Tom
Careful with using floating point numbers in a loop. The inaccuracy very quickly adds up and can leave you with one more or fewer iterations than you expected as `$i` might well end up on ‘a little bit more than or less than 1’. Better `for ($i= 1; i<=10; i++) { ` ... `<?php echo $i; ?>0c` (the $1 case needs special handling anyway).
bobince
Enlighten me sir-bobince, on this special handling?Are we talking number format OR something different, if so, what is it that they call special handling?
Newb
+1  A: 

If you prefer to use mixed PHP & HTML, you can use such construction:

<select name="dimes" >
    <option value=" ">--Select Dimes---</option>
    <?php foreach($dates as $key=>$value): ?>
      <option value="<?php echo $key; ?>">
        <?php echo $value; ?>
      </option>
    <?php endforeach; ?>
</select>

Note, that you have to define and fill $dates array before using it in foreach statement. You can fill $dates with any data your want. In this example array has to be something like: array('0.1'=>'0.1', '0.2'=>'0.2'); But also your can go futher and use result of MySQL queries to fill array with keys and values. See foreach for more details.

Alex
Holy smokes, that's awesome-Alex Pilipenko, thank you.
Newb
I take it the professionals use : and ?.And the Newbs are like WTF is : and ?.
Newb
I didn't completely understand the question, but I think you ask about alternative syntax(see: http://www.php.net/manual/en/control-structures.alternative-syntax.php). This syntax makes your code more readable and separates HTML-code from PHP. Sign '?' was used to open and close snippets of PHP-code (see: http://php.net/manual/en/ini.core.php). Common rule for alternative syntax: you need to put colon after "foreach(...):" statement and to close loop with "endforeach;" statement.
Alex
Sweet, thanks for the links.
Newb