Testing one numeric $string against another
3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 28, 2011 (RSS)
By Perchpole - April 26, 2011
Here's a quick poser which I'm sure will prove useful to people working with multi-select menus.
How do I test to see is a number(s) in $string1 matches a number(s) in $string2
Just to make things interesting, as things stand $string1 is a tab-delimited sequence whilst $string2 is comma delimited.
The result will be used to define a WHERE clause.
:oS
Perch
How do I test to see is a number(s) in $string1 matches a number(s) in $string2
Just to make things interesting, as things stand $string1 is a tab-delimited sequence whilst $string2 is comma delimited.
The result will be used to define a WHERE clause.
:oS
Perch
Re: [Perchpole] Testing one numeric $string against another
By Jason - April 27, 2011
Hi Perch,
From what you're describing, $string1 is the value being stored in the database (ie, what we're comparing against in the WHERE clause) and $string2 is a string we've gotten somewhere else in the PHP code. Does that sound correct?
The quickest way to do this would be to change $string2 into a tab delimited string and then do a straight comparison. for example:
Then, assuming the field in the database is called string1, the WHERE clause would look like this:
Now, it's important to note that this is a string comparison, so, if the numbers in each string are all the same, but are in a different order, this comparison would fail.
Hope this helps get you started.
From what you're describing, $string1 is the value being stored in the database (ie, what we're comparing against in the WHERE clause) and $string2 is a string we've gotten somewhere else in the PHP code. Does that sound correct?
The quickest way to do this would be to change $string2 into a tab delimited string and then do a straight comparison. for example:
<?php
$string2 = "\t". strreplace(",", "\t", str_replace(" ", "", trim(mysql_escape($string2)))). "\t";
?>
Then, assuming the field in the database is called string1, the WHERE clause would look like this:
'where' => "string1 = '$string2'",
Now, it's important to note that this is a string comparison, so, if the numbers in each string are all the same, but are in a different order, this comparison would fail.
Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Testing one numeric $string against another
By Perchpole - April 28, 2011
Hi, Jason -
Thanks for your help - and the code. I will certainly try to implement it next time I need to compare 2 strings.
However, my immediate problem went off at an angle a couple of days ago and became this...
Very Odd WHERE Clause Issue!
I'd appreciate your thoughts.
:0)
Perch
Thanks for your help - and the code. I will certainly try to implement it next time I need to compare 2 strings.
However, my immediate problem went off at an angle a couple of days ago and became this...
Very Odd WHERE Clause Issue!
I'd appreciate your thoughts.
:0)
Perch