Jump to content

Range numbers in Form?


paulmo

Recommended Posts

Thank you all, esp. JSG and Shadowmage. This works like a charm:

<?phpfor($i=0;$i<=9999;$i++){	echo "<option value='$i'>$i</option>";}?>

Seems like I was close earlier with for loop but $number should have been $i. Why is that? And how do I now use selected $i variable (from dropdown) for MySql statements, etc? This is not working:

$i = $_POST['$i'];echo $i;

Link to comment
Share on other sites

It doesn't matter what the variable is called as long as it doesn't have the same name as something else. Like anything else in a form, the name in post is the name of the form element. If the select has a name of "dropdown", then you'll find the value in $_POST['dropdown'].

Link to comment
Share on other sites

This is working but taking way too long for page to load. I'm guessing because loop goes through 9999 numbers. So how can I use range() instead?

<option selected>Select Your Number<br></option><?phpfor($i=0;$i<=9999;$i++){	echo "<option value='$i'>$i</option>";}?></select>

Link to comment
Share on other sites

With so many options, a select menu is very inefficient no matter how you generate it. Why don't you just use a normal text input instead? You can always validate it, even client-side, to ensure users only enter a number between 0 and 9999.

Link to comment
Share on other sites

I like the convenience of the select menu though. I read that range() is faster than doing a loop. That's really what I'd like to do if the speed is there. If it doesn't make that much difference, I'll take your advice on text input. Would I be validating that with JavaScript? Would that be less secure than validating with PHP since the value will be going in db table? I don't want users to see other users' "numbers" or anything else going on behind the scenes. Thanks.

Link to comment
Share on other sites

user side validation is less secure than server side. user can bypass the client side validation.it is possible with js. you can use js validation with php validation. but dont relly on only js client side validation (user can see that source code so they can bypass the code by editing). js validation are used to less the burden of server. js will try to stop the error at client level. if someone try to skip that.. obviously erronomus data must be caught in server side validation and then it will be secure.

Link to comment
Share on other sites

Personally, I don't consider scrolling through 10000 entries in a select list more convenient than typing four numbers into a box :). But I suppose it depends on the application...JavaScript allows you to validate the input immediately, so if they try to enter more than four characters or non-numerical data you can stop them immediately. But as birbal says, you still need to check again on the server-side in case the user bypasses the JS check, deliberately or otherwise.The PHP range() function generates an array of numbers between two endpoints. This saves you having to loop to generate the different numbers, but then you still have to loop to print out the option tags (there are other functions that can help you here, such as array_walk(), but you'll have to benchmark to see how much faster it really is).

Link to comment
Share on other sites

Got range() working in a foreach statement, but it's not faster, so I take your point about printing out option tags. I'll check out array_walk(). Also, in my db, I don't have an index on the fields id (medint) and message (text), and that's what I query to echo previous messages on page. The docs suggest putting an index on every column you search on. Should I do that? And do I need a Primary? Thanks Synook!

Link to comment
Share on other sites

primary key is must for database row. it will give you a identtical mark for your every row. so you can search it easily. primary key will be automaticaly indexed (phpmyadmin). if you do index on evry field (column) it will slow down your database. cause everytime you will insert a new row it will reindexed all .so for large amount of stored data it will slow for reindexing every field. which field you are refering for searching only that should indexed.you may use id (medint) as prmary key.

Link to comment
Share on other sites

...but you'll have to benchmark to see how much faster it really is).
You know, I saw this and was interested, so I decided to set up something to test this.
<?php// Before the loop goes off, let's get the starting time$starttime = microtime();$startarray = explode(" ", $starttime);$starttime = $startarray[1] + $startarray[0];?><select><option selected>Select Your Number<br></option><?php// Our For Loopfor($i=0;$i<=9999;$i++){	echo "<option value='$i'>$i</option>";}?></select><?php// With everything loaded, let's get the ending time$endtime = microtime();$endarray = explode(" ", $endtime);$endtime = $endarray[1] + $endarray[0];// Now, we subtract the starting time from the ending time to see how much time lapsed$totaltime = $endtime - $starttime;// We take this result and round it to 5 decimal places$totaltime = round($totaltime,5);// Finally, we echo the time so we can see and compareecho "<p>For Loop loaded in $totaltime.</p>";?><?php// Before the loop goes off, let's get the starting time$starttime = microtime();$startarray = explode(" ", $starttime);$starttime = $startarray[1] + $startarray[0];?><select><option selected>Select Your Number<br></option><?php// Our Foreach/Range Loopforeach(range(0,9999) as $usernum){	echo "<option value='$usernum'>$usernum</option>";}?></select><?php// With everything loaded, let's get the ending time$endtime = microtime();$endarray = explode(" ", $endtime);$endtime = $endarray[1] + $endarray[0];// Now, we subtract the starting time from the ending time to see how much time lapsed$totaltime = $endtime - $starttime;// We take this result and round it to 5 decimal places$totaltime = round($totaltime,5);// Finally, we echo the time so we can see and compareecho "<p>Foreach/Range Loop loaded in $totaltime.</p>";?>

From what I can gather on about 20 runs, neither had the advantage over the other by much. Check it out.

Link to comment
Share on other sites

Ixzion, this is VERY COOL! Birbal, MySQL is not letting me add Primary to my medint 'number' field, because there are repeating entries. That's how this app works: users will post multiple times with the same number: SQL query: ALTER TABLE `user` ADD PRIMARY KEY ( `number` ) MySQL said: #1062 - Duplicate entry '7' for key 1 What to do now?

Link to comment
Share on other sites

Ixzion, this is VERY COOL! Birbal, MySQL is not letting me add Primary to my medint 'number' field, because there are repeating entries. That's how this app works: users will post multiple times with the same number: SQL query: ALTER TABLE `user` ADD PRIMARY KEY ( `number` ) MySQL said: #1062 - Duplicate entry '7' for key 1 What to do now?
it is depend on your database structure and app structure .how you will choose primary key. obviously it must be unique. if you have duplicate value it database it will not let you change it to primary key until all row fulfill primary key criteria. i am not sure how your app will work. so its hard to tell what will be primary key. if medint need to have duplicate value then you can add a new column for primary key.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...