Jump to content


Photo

$pos How To question


  • Please log in to reply
6 replies to this topic

#1 BluePhoenixNC

BluePhoenixNC
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 22 June 2006 - 05:40 PM

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:

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

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

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 06:13 PM

have sql do it for you.
$findme  = 'SCH21';
$sql = "select * from table where VIN_ID like '%$findme%'";
$result = mysql_query($sql);

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 BluePhoenixNC

BluePhoenixNC
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 22 June 2006 - 06:36 PM

[!--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) View Post[/div][div class=\'quotemain\'][!--quotec--]
have sql do it for you.
$findme  = 'SCH21';
$sql = "select * from table where VIN_ID like '%$findme%'";
$result = mysql_query($sql);
[/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.

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 22 June 2006 - 06:55 PM

[!--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) View Post[/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] COUNT(*) FROM `table` WHERE `VIN_ID` LIKE '%$findme%' [!--sql2--][/div][!--sql3--]
If that query returns a number higher than 0, you have a match.

#5 BluePhoenixNC

BluePhoenixNC
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 22 June 2006 - 07:40 PM

[!--quoteo(post=386929:date=Jun 22 2006, 02:55 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 22 2006, 02:55 PM) View Post[/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.

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 08:22 PM

example:
<?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.";
}
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 BluePhoenixNC

BluePhoenixNC
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 22 June 2006 - 08:52 PM

[!--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) View Post[/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...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users