/    Sign up×
Community /Pin to ProfileBookmark

splitting a string / DB ussage

question here…

I have a variable called number that could equal any of these or more… The length of the file can not be determined

[code=php]$number = 1000 – 2000 – 3000 – 4000 – 5000 – 6000 – 7000 – 8000 –

$number = 5000 – 6000 – 7000 – 8000 –

$number = 4500

$number = 3[/code]

What I am trying to do with this next is SPLIT the variable so that I can use it in a select statement as such:

[code=php]
$query =”SELECT * FROM quicklinks WHERE Link_Num = ‘$number'”;[/code]

I need the string to split so that the numbers are such are just the numbers with no – inbetween or spaces. Then I want to run all numbers through my query one at a time. So that it would be like this:

[code=php]
$query =”SELECT * FROM quicklinks WHERE Link_Num = ‘$number'”;[/code]

do this…..

repeat with next number..

Would the best way to do this be to turn the variable in to an ARRAY and then do a foreach loop? if so.. What is the best way to write this?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@sridhar_423Jul 26.2006 — [code=php]$number = str_replace(" - ","','",$number);
$number = rtrim($number,",'");

$query ="SELECT * FROM quicklinks WHERE Link_Num in($number)";[/code]


change $number accordingly based on your o/p
Copy linkTweet thisAlerts:
@firmanauthorJul 26.2006 — $number = str_replace(" - ","','",$number);

$number = rtrim($number,",'");

$query ="SELECT * FROM quicklinks WHERE Link_Num in($number)";

change $number accordingly based on your o/p[/QUOTE]



Can you explain a little further... I understand the str_replace... but will that make many variables? I want each number in it's own variable...

Also $query ="SELECT * FROM quicklinks WHERE Link_Num in($number)";

what does the in do?
Copy linkTweet thisAlerts:
@NogDogJul 26.2006 — sridhar's method is creating a comma-separated list of all the values. That list can then be used in a single query with the IN() construct to give you all rows that match on any of the numbers in the list. If you want to process them individually, then you could do something like this:
[code=php]
$array = preg_split('/s*-s*/', $number);
foreach($array as $value)
{
$query ="SELECT * FROM quicklinks WHERE Link_Num = '$value'";
// process query, etc. for this value.....
}
[/code]
Copy linkTweet thisAlerts:
@bokehJul 26.2006 — If you want to process them individually, then you could do something like this:[code=php]$array = preg_split('/s*-s*/', $number);[/code][/QUOTE]Personally I'd write the regex for the split like this... not that it really matters:[CODE][^d]+[/CODE]
×

Success!

Help @firman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...