Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

PHP string issue

hello all!  i am working with a value from a database, and i want it to act as a string, but it continues to act as a number.

Below is my code:

<? include("dbconn.inc") ?>
                <?
                   function right ($str, $howManyCharsFromRight)
                   {
                     $strLen = strlen ($str);
                    return substr ($str, $strLen - $howManyCharsFromRight, $strLen);
                   }

                  
                   $customeridquery = "SELECT Customer_ID FROM customers ORDER BY Customer_ID DESC";
                   $customeridresult = mysql_query($customeridquery);
                  
                   $lastcustomerid = mysql_result($customeridresult,0);
                   $lastcustomerid = strval($lastcustomerid);
                   $lastcustomerid = trim($lastcustomerid);
                   $lastcustomerid = right($lastcustomerid,4);
                   $lastcustomerid = $lastcustomerid+1;
                   //echo $lastcustomerid;
                  
                  
                  
                   $year = date('y');
                   $customerid = $year.$lastcustomerid;
                   echo $customerid;
?>

the output i am expecting of this would be 060002 if the last customer id was 060001.

however, all i am getting is 062 - basically the 000 are being removed because the $lastcustomerid is being treated as a number.  any way to make this work properly?
0
flow79
Asked:
flow79
  • 4
  • 2
1 Solution
 
wildzeroCommented:
What if you did

                $lastcustomerid = mysql_result($customeridresult,0);
                $lastcustomerid = strval($lastcustomerid);
                $lastcustomerid = trim($lastcustomerid);

                $lastcustomerid = $lastcustomerid+1;
                $lastcustomerid = right($lastcustomerid,4);
                //echo $lastcustomerid;

so we are adding the 1 before we do the right()
0
 
kamermansCommented:
When you do this "$lastcustomerid = $lastcustomerid+1;" you are typecasting $lastcustomerid back to a number.
0
 
wildzeroCommented:
damn :P exact times as well!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kamermansCommented:
No kidding :P  You can use the points more than me - go ahead and give them to wildzero if it works.
0
 
kamermansCommented:
Try this out if the above doesn't work:

<?php
      include("dbconn.inc")
      $customeridquery = "SELECT Customer_ID FROM customers ORDER BY Customer_ID DESC";
      $customeridresult = mysql_query($customeridquery);
      $lastcustomerid = mysql_result($customeridresult,0);
      $lastcustomerid = $lastcustomerid+1;
      $lastcustomerid = sprintf("%'010s",  $$lastcustomerid);
      echo $lastcustomerid;
            
      $year = date('y');
      $customerid = $year.$lastcustomerid;
      echo $customerid;
?>
0
 
kamermansCommented:
Ooops, typo:

<?php
     include("dbconn.inc")
     $customeridquery = "SELECT Customer_ID FROM customers ORDER BY Customer_ID DESC";
     $customeridresult = mysql_query($customeridquery);
     $lastcustomerid = mysql_result($customeridresult,0);
     $lastcustomerid++; // add 1
     $lastcustomerid = sprintf("%'04s",  $lastcustomerid); // pad with 0's
     echo $lastcustomerid;
         
     $year = date('y');
     $customerid = $year.$lastcustomerid;
     echo $customerid;
?>
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now