Jump to content

$pos How To question


BluePhoenixNC

Recommended Posts

Sorry, but I did not know how to make the header say what I need.
I am trying to do a lookup of a value.

The situation is the following: I have the VIN (something like MHASCH21051035272) and I have a table setup like that:

ID VIN_ID Model Category
1 LYD1C model1 1
2 YCBL1 model2 1
3 KCAL3 model3 1
4 LZH1A model4 1
5 YAEL1 model5 1
6 YCFL1 model6 1
7 JCHL1 model7 1
8 JCJL1 model8 1
9 JCKL1 model9 1
10 JCML0 model10 1
11 SCBL1 model11 2
12 SCHZ1 model12 2
13 SCH21 model13 2
...

Now what I want to do is see if a value of VIN_ID is contained within the VIN and then give me the table row.
I assume I have to do something like:

[code]$mystring ='MHASCH21051035272';
$findme  = 'SCH21'; // loop through table to get those values
$pos = strpos($mystring, $findme);[/code]

Any help in this matter is greatly appreciated and I appologize for the lengthy post, but I would not know how to explain it better than by examples.

Thank you
Link to comment
Share on other sites

[!--quoteo(post=386921:date=Jun 22 2006, 02:13 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 22 2006, 02:13 PM) [snapback]386921[/snapback][/div][div class=\'quotemain\'][!--quotec--]
have sql do it for you.
[code]
$findme  = 'SCH21';
$sql = "select * from table where VIN_ID like '%$findme%'";
$result = mysql_query($sql);
[/code]
[/quote]
Crayon: thank you for your reply, that is what I eventually want to do, but first I want to "validate" the VIN. In other words, I want to see if the string in VIN_ID can be found in the VIN. For that I need to check the entire column VIN_ID against the VIN and see if any of those can be found in the VIN, if this makes any sense.
Link to comment
Share on other sites

[!--quoteo(post=386926:date=Jun 22 2006, 07:36 PM:name=Blue Phoenix)--][div class=\'quotetop\']QUOTE(Blue Phoenix @ Jun 22 2006, 07:36 PM) [snapback]386926[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Crayon: thank you for your reply, that is what I eventually want to do, but first I want to "validate" the VIN. In other words, I want to see if the string in VIN_ID can be found in the VIN. For that I need to check the entire column VIN_ID against the VIN and see if any of those can be found in the VIN, if this makes any sense.
[/quote]
Thats what Crayons code does... Searches all of the VIN_ID's in the database and returns all of the VIN_IDs that are found within the VIN held in $findme. You can just return a count if you like with:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] [color=blue]COUNT[/color](*) [color=green]FROM[/color] [color=orange]`table`[/color] [color=green]WHERE[/color] `VIN_ID` [color=orange]LIKE[/color] [color=red]'%$findme%'[/color] [!--sql2--][/div][!--sql3--]
If that query returns a number higher than 0, you have a match.
Link to comment
Share on other sites

[!--quoteo(post=386929:date=Jun 22 2006, 02:55 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 22 2006, 02:55 PM) [snapback]386929[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thats what Crayons code does... Searches all of the VIN_ID's in the database and returns all of the VIN_IDs
[/quote]

Semi: Thank you, too. I think I confused you guys more than necessary> I appologize, English is not my native language and php/mysql well... let's just say... n00b hits it.. ;)

let me try that again and hopefully I make more sense now:
$mystring ='MHASCH21051035272'; // the only known value and "static" user typed that in
$findme = $something out of the table; // loop through table to get those values
$pos = strpos($mystring, $findme);

so what I want to know is: "is in the variable $mystring any of the "strings" contained in the tablecolumn VIN_ID ?
Column Vin_ID has just that part of the vin which indicates the Model, therefore I need to find a match and it needs to loop through the table to return a result. I should get a match at position (ID) 13 since SCH21 can be found in $mystring all others do not match.... Does that make more sense ? Again my apologies for making things more complicated than they need to be.
Link to comment
Share on other sites

example:
[code]
<?php
$vin = "MHASCH21051035272";

$sql = "select VIN_ID from table";
$result = mysql_query($sql);

$found = FALSE;

while ($list = mysql_fetch_array($result)) {
   $pattern = "/" . $list['VIN_ID'] . "/i";
   if (preg_match($pattern, $vin)) {
      echo "found" . $list['VIN_ID'] . "<br>";
      $found = TRUE;
   }
}

if ($found == FALSE) {
   echo "no matches found.";
}
?>
[/code]
Link to comment
Share on other sites

[!--quoteo(post=386961:date=Jun 22 2006, 04:22 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 22 2006, 04:22 PM) [snapback]386961[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[/quote]

Crayon: Thank you very much :) That was exactly what I was looking for..... now I have to do some readup on php.net to fully understand your code example..... :) but it works out the box..... :) Thanks again...
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.