Choosing a record based on a range comparison.

A

Anonymous

Guest
Hey all,

I am attempting to select a record based on comparing a posted value from a form to see if it falls between two numbers.

Table: numbersTable

Fields:
numID
numRange
numName

Here is an example:

numID numRange numName
1 1011-2030 Selection 1
2 2021-3020 Selection 2
3 3052-4087 Selection 3

------------------------------------------

The form would be posting the following variable:
$postedField

The user would put in a number like: 2017 and submit the form

So $postedField would = 2017

I would carry over $postedField as a hidden input type

------------------------------------------

Code:
<?php

$query = "select * from numbersTable";
$result = mysql_query($query);
     $number = mysql_numrows($result);
     echo "<br><br>Here is the query that is being run:  " . $query . "<br><br>";
    
// Here is where I need to split the value of numRange into its respective numbers and 
// see if the posted variable falls between those two numbers, and if it does then tell me the name.  
// Anyone assist, I think it would be something like this:

     while ($rows = mysql_fetch_object($result) AND ($postedField IS NOT GREATER THAN OR LESS THAN (numSplit1 or numSplit2) 


// Now I know I have a " - " separating the two numbers so I need some kind of strip of that out 
// I think, and then to put the two numbers into variables such as numSplit1 and numSplit2 and then 
// see if the posted variable falls between that range.

     {
     echo $rows->numName;
     }

?>
------------------------------------------

Anyone help me out here ??

Peace
 
Hi,

Try using explode to separate both values:

$row = mysql_fetch_assoc($result);
$range = explode('-', $row['numRange']);

$numSplit1 = $range[0];
$numSplit2 = $range[1];

Regards.
 
Hey Vic,

Thanks for the reply I will try that today and let you know. Appreciate it.
 
Back
Top