Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use PHP to print 100 to 10000? 2

Status
Not open for further replies.

meeble3

Programmer
Nov 8, 2004
52
0
0
GB
Hello,

I want to make a drop down box with values 100 to 10,000 in 100 increment.

So I need

<select>

<option>100<option>200<option>300 etc

</select>

Instead of doing this by hand can I use PHP to create this?

Thanks

James
 
Like this:

Code:
echo '<select name="numbers">';
for ( $i = 100; $i < 10000; $i += 100 ) {
    echo '<option value="' . $i . '">' . $i . '</option>';
}
echo '</select>';

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
ps. after you have created this code, use IE to view source, save as an included file.

why?

For fasting executing script, as well as less system stress.
You might still want some php code there, but once you have your list done, if it does not need a complete regeneration, you can keep it static.

Example about which code you might need afterwards, in the static list:

Code:
<select name="numbers">
<?php
if(isset($numbers)) {
    echo "<option value=\"{$numbers}\" selected=\"selected\">Selected: {$numbers}</option>";
  }
?>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
...etc..etc..

Olav Alexander Mjelde
Admin & Webmaster
 
You can also try to have JavaScript print out the list, to save filesize (and bandwith) on your system, but such scripts are difficult to ensure on all Browser platforms.
 
why is it a bad suggestion???

Known is handfull, Unknown is worldfull
 
vbkris: He actually mentioned it in his own suggestion.

...but such scripts are difficult to ensure on all Browser platforms.

in other words, he knew about it, and mentioned it. I just felt I had to point it out too, so it's not beeing used :p

I dont use client-dependent functions, unless the customer wants it, for some reason.

I feel there are very few times you have to use client-side code.

There might be a bit less bw, but a list from 100 to 1000, with 100 in intervals, is a list with only 10 items!

10 items x 32 chars < 1kb

I actually think that the javascript will take more time to load than 10 x:
<option value="100">100</option>

(remember, javascript also has to be run on the client).

The really important thing here, however, is that client dependent does not work on all clients. Even if the user where to save 0.0002 seconds, it's not worth either the hassle or the loss of potential registrations from visitors.

Olav Alexander Mjelde
Admin & Webmaster
 
point taken and noted ;)

Known is handfull, Unknown is worldfull
 
It's actually 100 items in this case, 100-10000... not that that matters, the important part, as discussed is the client compatability.

Altho, something else to remember is it is 100 incredibly easily compressed items... if you (the general you) haven't done it already turn on html compression, it saves everyone time and/or money, and it doesn't screw with anyone.
 
skiflyer: hmm, I actually thought I saw 100-1000 there.

even so, 100 x select, is often less than a small image..

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top