Jump to content
simona6

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

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..........

Share this post


Link to post
Share on other sites

Ooooo, how do I assign the two chunks I need, into their own variables?

Share this post


Link to post
Share on other sites
$array1=$chunk[0];
$array2=$chunk[1];

Assuming it was split into 2 chunks.

Edited by gw1500se

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

$chunk is whatever you name the array you split into. RTFM.

Share this post


Link to post
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

Share this post


Link to post
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";

Share this post


Link to post
Share on other sites

Bingo:

    $followersrange = (explode("-",$rowrange->followersrange)); 
    // with positive NoOfElements 
    $rangelow = $followersrange[0];
    $rangehigh =  $followersrange[1];
    echo "$rangelow > $rangehigh<br/>";

2000 > 5000
5000 > 10000

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Totally agree.  But at the time I wasn't aware of the reasoning behind this dropdown option.

If I did it again, yes I would split it anyway.

Share this post


Link to post
Share on other sites

Not too late.  Modify the table but still use the literal description in your dropdown and then modify your query.  POC!

Share this post


Link to post
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 |
+-------+------+------+

 

Share this post


Link to post
Share on other sites

Thanks a lot everyone. :) Really appreciate it.

The forum is great.  I use to use another one years ago, but i dont' think it's covered anymore, and some others are very prickly.

This one is helpful and quick.  Cheers all.

Share this post


Link to post
Share on other sites

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.