Jump to content

Recommended Posts

I'm trying to create a part of a script where by the system will generate a sku code and loop through out to see if this exists in the database, if the sku does the script adds one too the code so 001 will turn to 002 then it will keep looping until it finds a SKU that isn't in the database. 

 

However I can get the script to keep generating the new SKU codes but it will only test the first SKU code so the loop will keep going if it exists. 

 

 

$number=001;
if(isset($_POST['submit'])){
 
$product = $_POST['product'];
$brand = $_POST['brand'];
$description = $_POST['description'];
$contruct=strtoupper($product."-".$brand."-".$description);
$number=sprintf("%03s",$number);
$check_for=$contruct."-".$number;
 
$query=$cc_db->query("SELECT `sku` FROM `product` WHERE `sku`='".$check_for."'");
$fetch=$cc_db->fetch_object($query);
$checking=$cc_db->num_rows($query);
while($checking > 0){
echo"$check_for is there : $checking rows : $fetch->sku<br>";
$number=$number+1;
$number=sprintf("%03s",$number);
$check_for=$contruct."-".$number;
}
echo $checking;
echo $check_for;
}

You begin by looking in your db for a certain numbered value.    You don't bother to check if your query ran successfully though, nor do you even check if you had any rows returned before retrieving the first row.  (Bad practice!).  But then it gets weird.  Assuming that you get rows back (more than 1) you echo out messages that say your search argument was found AND that successive ones were found EVEN THOUGH YOUR QUERY COULDN'T POSSIBLY HAVE RETURNED ANYTHING BUT THE FIRST ONE.

 

PS - I'd turn on php error checking to ensure you're not doing something wrong with that echo message.  Also - you are incrementing $number as if it is a number when you keep making it a string.

function checkSku($check_for){
   $query=$cc_db->query("SELECT `sku` FROM `product` WHERE `sku`='".$check_for."'");

   //NEED SOME ERROR HANDLING ON YOUR QUERY HERE

   $fetch=$cc_db->fetch_object($query);
   $checking=$cc_db->num_rows($query);
   if ($checking < 1){
      return true;
   }else{
      return false;
   }
}

if(isset($_POST['submit'])){
 
   $product = $_POST['product'];
   $brand = $_POST['brand'];
   $description = $_POST['description'];
   $contruct=strtoupper($product."-".$brand."-".$description);
   $number = 0;
   $isNewSku = false;   

   while($isNewSku ==  false){
      $number=$number+1;
      $number=sprintf("%03s",$number);
      $check_for=$contruct."-".$number;
      $isNewSku = checkSku($check_for);
   }
  
   //DO WHAT YOU WANT WITH YOUR $check_for variable WHICH WILL CONTAIN YOUR NEW SKU HERE
   echo $check_for. 'is a new sku';
}

Haven't tested it but Im pretty sure it should work for what you need.

 

Good Luck!

 

Josh

Edited by joallen
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.