phpacademy Forum

Watch, Listen, Learn!

Between

For if you require help with PHP programming.

Between

Postby Tweak » Tue Mar 09, 2010 6:40 am

How can I make a search that searches between two numbers, like if someone wanted to search all between 5 and 10.
Tweak
 
Posts: 68
Joined: Tue Dec 29, 2009 6:55 am
Online: 0s
Karma: 0

Re: Between

Advertisment

Advertisment
 

Re: Between

Postby extra » Tue Mar 09, 2010 12:36 pm

Do you mean to get all the numbers between $number1 and $number2 (5 and 10 in your example)?
If yes:
[php]<?php
$number1 = 5;
$number2 = 10;
for($i < $number2; $i = $number1; $i++){
echo $i."<br />";
}
?>[/php]
If you mean to give a random number between 5 and 10, then put this:
[php]<?php
$number1 = 5;
$number2 = 10;
$randomNumber = rand($number1, $number2);
?>[/php]

I would not recommend setting the variables as they slow out the script, just put them directly.
Karma++; me if I helped! :roll:
GameZ4Free::playGames();

I was Hyper.. Now I'm extra.
User avatar
extra
Top Contributor
 
Posts: 1022
Joined: Fri Nov 06, 2009 7:08 pm
Online: 7d 59m 20s
Karma: 18

Re: Between

Postby Tweak » Tue Mar 09, 2010 5:52 pm

Well i have the input type='text' on the page, so i will change to variables to "Get" and it will work?
Tweak
 
Posts: 68
Joined: Tue Dec 29, 2009 6:55 am
Online: 0s
Karma: 0

Re: Between

Postby wide_load » Tue Mar 09, 2010 5:59 pm

Tweak wrote:Well i have the input type='text' on the page, so i will change to variables to "Get" and it will work?


dont know where you got that idea

do you meen between rows 5 and 10 of a database ? :?
Image
User avatar
wide_load
Top Contributor
 
Posts: 5617
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 22h 12m 32s
Karma: 45

Re: Between

Postby Tweak » Tue Mar 09, 2010 6:18 pm

no. Okay so there is levels in the game. I want the user to be able to search through all users. I have it so they can search by username, but i want it so they can search by level. So i have two input boxes. So they will search all users whos level is between the specified searches.
Tweak
 
Posts: 68
Joined: Tue Dec 29, 2009 6:55 am
Online: 0s
Karma: 0

Re: Between

Postby libeco » Tue Mar 09, 2010 6:58 pm

So probably you are searching through a database, in which case you want to use the LIMIT clause to get only 5 results starting at a certain record.
Common PHP/MySQL errors | How to display code on the forum
"It always seems impossible until its done." - Nelson Mandela
User avatar
libeco
Moderator
 
Posts: 2466
Joined: Fri Apr 24, 2009 9:03 pm
Location: Netherlands
Online: 5d 9h 32m 31s
Karma: 25

Re: Between

Postby wide_load » Wed Mar 10, 2010 6:16 pm

libeco wrote:So probably you are searching through a database, in which case you want to use the LIMIT clause to get only 5 results starting at a certain record.


no no

he means that there is a field that has possible values form 1 to 10 and wants to search for the ones that have this value between 5 and 10

thats my understanding of it anyway

i believe you can use the less than < and greater than > opperator but not too sure.
Image
User avatar
wide_load
Top Contributor
 
Posts: 5617
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 22h 12m 32s
Karma: 45

Re: Between

Postby Tweak » Thu Mar 11, 2010 4:11 am

When I try a "mysql_query("SELECT * FROM users WHERE Level > $search AND < $search2");"
I get an error.
Tweak
 
Posts: 68
Joined: Tue Dec 29, 2009 6:55 am
Online: 0s
Karma: 0

Re: Between

Postby Cags » Thu Mar 11, 2010 12:01 pm

To use that syntax you would have to use the field name before both the less than and greater than symbols, not just the first. This is much the same as logic in PHP. You can't do...

[php]if( $a > 10 && < 11 ) {[/php]
Having said that your topic title has the answer in it.

[syntax=mysql]WHERE level BETWEEN $search AND $search2[/php]
"I don't suffer from insanity, I enjoy every minute of it."
- Pete
CodeCanyon - Cheap, High Quality, Ready Made Scripts.
User avatar
Cags
Moderator
 
Posts: 1863
Joined: Fri May 22, 2009 9:35 am
Location: Purgatory
Online: 3d 1h 51m 14s
Karma: 9


Return to PHP Help

Who's online?

Users browsing this forum: jonathan and 1 guest