开发者

html php and select box simplification

开发者 https://www.devze.com 2022-12-13 08:13 出处:网络
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?

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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号