simona6 Posted February 14, 2020 Share Posted February 14, 2020 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.......... Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/ Share on other sites More sharing options...
gw1500se Posted February 14, 2020 Share Posted February 14, 2020 array_chunk Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574279 Share on other sites More sharing options...
simona6 Posted February 14, 2020 Author Share Posted February 14, 2020 Ooooo, how do I assign the two chunks I need, into their own variables? Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574280 Share on other sites More sharing options...
gw1500se Posted February 14, 2020 Share Posted February 14, 2020 (edited) $array1=$chunk[0]; $array2=$chunk[1]; Assuming it was split into 2 chunks. Edited February 14, 2020 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574281 Share on other sites More sharing options...
simona6 Posted February 14, 2020 Author Share Posted February 14, 2020 $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 Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574282 Share on other sites More sharing options...
gw1500se Posted February 14, 2020 Share Posted February 14, 2020 $chunk is whatever you name the array you split into. RTFM. Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574283 Share on other sites More sharing options...
simona6 Posted February 14, 2020 Author Share Posted February 14, 2020 $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 Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574284 Share on other sites More sharing options...
simona6 Posted February 14, 2020 Author Share Posted February 14, 2020 $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"; Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574285 Share on other sites More sharing options...
simona6 Posted February 14, 2020 Author Share Posted February 14, 2020 Bingo: $followersrange = (explode("-",$rowrange->followersrange)); // with positive NoOfElements $rangelow = $followersrange[0]; $rangehigh = $followersrange[1]; echo "$rangelow > $rangehigh<br/>"; 2000 > 5000 5000 > 10000 Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574286 Share on other sites More sharing options...
ginerjm Posted February 14, 2020 Share Posted February 14, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574287 Share on other sites More sharing options...
simona6 Posted February 14, 2020 Author Share Posted February 14, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574288 Share on other sites More sharing options...
ginerjm Posted February 14, 2020 Share Posted February 14, 2020 Not too late. Modify the table but still use the literal description in your dropdown and then modify your query. POC! Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574290 Share on other sites More sharing options...
Barand Posted February 14, 2020 Share Posted February 14, 2020 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 | +-------+------+------+ Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574292 Share on other sites More sharing options...
simona6 Posted February 14, 2020 Author Share Posted February 14, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310032-how-do-i-split-a-range-string-into-two-lots/#findComment-1574293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.