Orionsbelter Posted May 22, 2014 Share Posted May 22, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/288696-while-loop-through-sku-codes-not-working/ Share on other sites More sharing options...
ginerjm Posted May 22, 2014 Share Posted May 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288696-while-loop-through-sku-codes-not-working/#findComment-1480532 Share on other sites More sharing options...
joallen Posted May 22, 2014 Share Posted May 22, 2014 (edited) 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 May 22, 2014 by joallen Quote Link to comment https://forums.phpfreaks.com/topic/288696-while-loop-through-sku-codes-not-working/#findComment-1480537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.