Web Analytics Made Easy -
StatCounter Decode This Loop - CodingForum


No announcement yet.

Decode This Loop

  • Filter
  • Time
  • Show
Clear All
new posts

  • Decode This Loop

    A while back someone very kindly helped me with a code that searches a database to determine which sku's (a 4 digit number between 1000 & 9999) have not yet been assigned to rows.

    $sql = "SELECT sku FROM products WHERE sku BETWEEN 1000 and 9999";
    $result = mysql_query($sql) or die(mysql_error());
    $taken = array();
    $possible = range(1000,9999);
        $taken[] = $row[0];
    $unused = array_diff($possible,$taken);
    echo '<pre>'.print_r($unused,1).'</pre>';
    Very helpful indeed... except i can't define each unassigned 4 digit "sku" as a variable, which is what i want to do ie. alter the MySQL code to LIMIT 0,1 so when the code is called it lists the next available 4 digit code and no more, and allows me to define it as say $next_avail

    Can someone help me unravel the above to work without the help of arrays. Can it be done?

    Thanks you as always for all your help.

  • #2
    From what you have there, the first available sku would be $unused[0]. That assumes as well that the $unused is in order, which it should be since you're performing a difference on the $possible which is ranged from 1000, 9999.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


    • #3
      that would work... IF the next available sku was always in $unused[0]; they're not though.

      Whatever way the above code works, if for example the next sku is 1787, the row is $unused[787] so I can't pre-empt what the unused is going to be - any thoughts on this?