Jump to content

How do I split a 'range' string into two lots?


simona6

Recommended Posts

We have a database that shows for example:

2500-5000

5000-10000

10000-15000

15000-20000

I need to show in a screen what people are have 3000 followers, and as such as grouped into that 2500-5000.

The best way I can see how to do this, is to run through the database table for these values.

Then for each value, put the first and second set of numbers into two respective variables, say $low, $high.

Then find those people who are on a particular platform with followers between those two values.

So how do I turn $row->followsrange (which might be for example: "2500-5000"), into:

$low = 2500
$high = 5000.

Many thanks.  ps  I have a feeling the hyphen might play a bit part, as it cannot be done by the amount of characters..........

Link to comment
Share on other sites

$input_array = array($rowrange->followersrange);
$array1=chunk[0];
$array2=chunk[1];
echo "$array1 > $array2";;


Warning: Use of undefined constant chunk - assumed 'chunk' (this will throw an Error in a future version of PHP) in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 209

Warning: Use of undefined constant chunk - assumed 'chunk' (this will throw an Error in a future version of PHP) in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 210
c > h
Warning: Use of undefined constant chunk - assumed 'chunk' (this will throw an Error in a future version of PHP) in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 209

Warning: Use of undefined constant chunk - assumed 'chunk' (this will throw an Error in a future version of PHP) in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 210
c > h

Link to comment
Share on other sites

$input_array = array($rowrange->followersrange);
$array1=$input_array[0];
$array2=$input_array[1];
echo "$array1 > $array2";;

Notice: Undefined offset: 1 in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 210
2000-5000 >
Notice: Undefined offset: 1 in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 210
5000-10000

Link to comment
Share on other sites

$input_array = array_chunk($rowrange->followersrange);
$array1=$input_array[0];
$array2=$input_array[1];
echo "$array1 > $array2";

Warning: array_chunk() expects at least 2 parameters, 1 given in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 208
>
Warning: array_chunk() expects at least 2 parameters, 1 given in C:\xampp\phpMyAdmin\site\includes\admin-gig.inc on line 208
>

For reference here..................
$rowrange->followersrange = "5000-10000";

Link to comment
Share on other sites

One way might have been to use a different table design.  Put the low and high values into it as separate columns, ie, 'low_value', 'high_value' and include the current string as a literal description of this range.  Then do a query using a BETWEEN test against "low_value' and 'high_value' and choose whichever one you want to move forward with.

Link to comment
Share on other sites

I agree with ginerjm - add "low" and "high" columns. (You can run an update query using the same functions I have used below to populate them)

But, given what you have currently

SELECT * FROM simona6;

+----+-------+-------------+
| id | name  | followers   |
+----+-------+-------------+
|  1 | Curly | 2500-5000   |
|  2 | Larry | 5000-10000  |
|  3 | Mo    | 2500-5000   |
|  4 | Peter | 5000-10000  |
|  5 | Paul  | 10000-15000 |
|  6 | Mary  | 15000-20000 |
+----+-------+-------------+

SELECT name
     , substring_index(followers, '-', 1) as low
     , substring_index(followers, '-', -1) as high
FROM simona6
WHERE 3000 BETWEEN substring_index(followers, '-', 1) AND substring_index(followers, '-', -1); 

+-------+------+------+
| name  | low  | high |
+-------+------+------+
| Curly | 2500 | 5000 |
| Mo    | 2500 | 5000 |
+-------+------+------+

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.