Jump to content

Make CustID Problem with preg_match *SOLVED*


scottybwoy

Recommended Posts

Hi php freaks,

I have a function to make a customer ID from the customers name, however I can't get it to work.  I have placed in some echo's to see where it is not working, and it seems to fail on preg_match.  And that provides a knock on effect for the rest of the function.  So I was wondering if anyone can spot wher I'm going wrong.

Here's the code :
[code]
<?php
function makeCustId($company)
{
global $company, $rowInt, $custId;

$comp = preg_match('|^[A-Z]+$i |', $company);
echo $comp;
  $comp = substr($comp, 0, 2);


  $this->appCon();

  $sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
  $rowcount = mssql_num_rows($sql);
  $rowInt = ++$rowcount;

  if ($rowInt <= 9)
  {
  $rowInt = "00" . $rowInt;
 
  return $rowInt;
  } elseif ($rowInt <= 99) {
  $rowInt = "0" . $rowInt;

  return $rowInt;
  }

  $custId = $comp . $rowInt;

  return $custId;
}
?>
[/code]

What I want it to do is Take the input for customer name, strip all the numbers, spaces and other characters, make it into 3 letters then compare it with the others in the database and increment the last 3 digits by 1.  i.e if database has TES001 and I input TEST LTD it would create TES002.  Thanks for your help.
Link to comment
Share on other sites

Well, for starters, preg_match() only returns a boolean value. It simply returns whether or not the string you provided matches the regex. If you provide a third parameter, you can capture the matches, but I'm not sure that's what you want. Typically, you want to use preg_match() within an if() statement instead of assigning the return value to a variable as you are doing:
[code]
<?php
// You have this:
$comp = preg_match('|^[A-Z]+$i |', $company);

// Usually, it needs to be like this:
if (preg_match('|^[a-z]+$|i', $company)) {
  // you have a valid match, so process it
} else {
  // there is an error somewhere
}
?>
[/code]

However, it sounds like you want to do something like this, since you're looking to replace characters and whatnot:
[code]
<?php
// Notice the use of preg_replace() instead of preg_match()
// Replace anything that IS NOT alpha
$comp = preg_replace('|[^a-z]|i', '', $company);
$comp = strtoupper(substr(0, 3, $comp));

// now run the rest of your code on $comp
?>
[/code]

Hope this helps
Link to comment
Share on other sites

Thanks Obsidian, however now I get a wrong parameter count, I echoed the $company var and it appears correct, (uppercase company name) looking in the manual, I think I may need preg_match or a string function.  What I want it to do is strip out all numbers and non alpha characters, so if $company = 1st o'brian then it would return $comp = stobrian.  Then further down the line is it possible to add a string to an integer so that if $rowInt = 4 it would become $rowInt = 004.  Or am I going wrong their too, thanks for your time and help.
Link to comment
Share on other sites

Hmm... [b]where[/b] are you receiving the wrong parameter count? I you check out preg_replace() in the manual, it should be just what you're after. For instance, try out the example you posted:
[code]
<?php
$company = "1st o'brian";
$comp = preg_replace('|[^a-z]|i', '', $company);
echo $comp;
?>
[/code]

This outputs exactly what you're after: [b]stobrian[/b]... therefore, I'm really struggling to see where your error is occurring.
Link to comment
Share on other sites

Thanks again Obsidian, stupid me, forgot the 'replace' what do i need to get it to leave in white spaces and numbers?  I have '|^[A-Z0-9]+$i |'  But that also leaves in symbols and i tried taking out the +$i to no avail.  Also could you look at the second part of my question, thanks.
Link to comment
Share on other sites

[quote author=scottybwoy link=topic=116698.msg476350#msg476350 date=1164905174]
...what do i need to get it to leave in white spaces and numbers?
[/quote]

Keep in mind when using regular expressions that when the '^' appears within your grouping, it negates that group (thus matching everything BUT what's in that group). So, if you want to match everything [b]except[/b] letters, numbers and spaces, you'd use something like this:
[code]
<?php
$comp = preg_replace('|[^a-z0-9 ]|i', '', $company);
?>
[/code]

The second part of your question is a bit trickier. You'll have to find some way to either SELECT the MAX number of your suffix, or else, somewhere in your database you should have a control table where you store the max suffix for easy retrieval. I tend to lean towards the second solution. Basically, what you would do is have a "config" table or such where you could have simply two columns: [b]name[/b] and [b]value[/b]. Then, just have a record inserted as name => max_suffix and value => 001 (or whatever your current max is). Then, all you have to do is tap that table whenever you're ready to insert:
[code]
// Pseudo-code to create a new record
1. Grab the max suffix and increment it
2. Parse your company to create the prefix of your name
3. Tack on the suffix you've created
4. Insert new record
5. If record was successfully entered, update your max suffix record
[/code]

Obviously, there would be other ways to approach this, but that's the one that comes immediately to mind.

Hope this helps!
Link to comment
Share on other sites

Hmm, yeah we sort of understand each other.  If you look at the code in the first entry, you'll see that I have done something similar.  As the max suffix would be diffrent for each beginning company, the new table would be just as big as the first, (well in terms of rows sort of).

What I mean is that if I have these companies :
Boo Radleys, Book Stoor, Comics, Communication Center.
Their Cust Id's may be : BOO001, BOO002, COM001, COM002.

So what you were saying is that I'd have to have <b>name</b> Sufix BOO <b>value</b> 002 etc.

What I have done, is create the initial first letters and SELECT similar from the CustId value then count the Rows:
[code]
<?php>
$comp = substr($comp, 0, 2);
$sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
$rowcount = mssql_num_rows($sql);
$rowInt = ++$rowcount;
?>
[/code]

This is working fine, until a record gets deleted (thanks for making me think of that, I'll cross that bridge in a bit) But how do I get it to put the 00 before the int i.e 1 => 001?

I've just fount sprintf() but it's not working this is what I have now :
[code]
<?php
if ($rowInt <= '99')
{
  $rowInt = sprintf("%03d", $rowInt);
  return $rowInt;
}
?>
[/code]

Then a simple concatonation of $comp . $rowInt as before.  Thanks again
Link to comment
Share on other sites

[quote author=scottybwoy link=topic=116698.msg476372#msg476372 date=1164907881]
I've just fount sprintf() but it's not working this is what I have now...
[/quote]

It looks like what you've got should work just fine. sprintf() is definitely the way I'd go with it, and your format looks right, however, you shouldn't have to use an if() clause. If the number is 115, and you use that sprintf() on it, you'll still get '115' as your string. So, my only adjustment would be to run [b]every[/b] $rowInt through that:
[code]
<?php
$rowInt = sprintf("%03d", $rowInt);
$newID = $comp . $rowInt;
echo $newID;
?>
[/code]

I don't know why that wouldn't work.
Link to comment
Share on other sites

You can use [url=http://www.php.net/str_pad]str_pad()[/url].  Using this function, you wouldn't even have to check to see if the value is <= 9 or <= 99.

[code]$rowInt = str_pad($rowInt,3,'0',STR_PAD_LEFT);[/code]

This will return $rowInt with 3 character places.  Any places not taken with a number will be replaced with a '0'.
Link to comment
Share on other sites

OK, thanks for all your help so far, now I have taken the initiative to implement a while loop, to find any gap's in my custId's so that I don't get any duplicates, from using $rowcount = mssql_num_rows($sql);  But I've never used a while loop b4 so if you could help me on this one last point, I should be on my way, Thanks.

Here's what I have now :
[code]
<?php
function makeCustId($company)
{
global $company, $rowInt, $custId;

$comp = preg_replace('|[^A-Z]|i', '', $company);
  $comp = substr($comp, 0, 3);

  $this->appCon();

  $sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
$rowcount = mssql_fetch_row($sql);

if (isset($rowcount)) {
$start = 1;
} else {
$start = 0;
}

$rowInt = sprintf("%03d", $start);

while (substr(mssql_fetch_row($sql), 4, 6) == $rowInt) {
$rowInt = substr(mssql_fetch_row($sql), 4, 6);
$rowInt = ++$rowInt;
$rowInt = sprintf("%03d", $rowInt);
echo $rowInt . "<br>";
}

  $custId = $comp . $rowInt;

  return $custId;
}
?>
[/code]

I know there needs to be something more, but I'm not sure what.  Thanks for all your time 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.