Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    122
    Thanks
    6
    Thanked 0 Times in 0 Posts

    How do i get missing internal no`s

    here is my code

    $connection = mysql_connect("localhost", "user", "xxxxx");
    mysql_select_db("firefox");
    $sql = "SELECT Internal FROM CusDec";
    $result = mysql_query($sql);

    $count = 0; // set a counter variable to zero
    $employee_array = null; // this is our empty PHP array

    while ($row = mysql_fetch_array($result)) // iterate through array
    {
    // store results from mysql in our own PHP array
    $employee_array[$count] = $row['Internal'];

    // increment counter
    $count++;
    }

    mysql_close($connection);

    // we can now iterate through our own array to retrieve the data
    for ($i = 0; $i < count($employee_array); $i++)
    //echo $employee_array[$i] . '<br />';

    //$arr1 = $employee_array[$i];

    //echo $arr1;

    // construct a new array:1,2....max(given array).
    $arr2=range(7,max($employee_array[$i]));

    // use array_diff to get the missing elements
    $missing = array_diff($arr2,$employee_array[$i]);

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by coolumanga View Post
    here is my code

    $connection = mysql_connect("localhost", "user", "xxxxx");
    mysql_select_db("firefox");
    $sql = "SELECT Internal FROM CusDec";
    $result = mysql_query($sql);

    $count = 0; // set a counter variable to zero
    $employee_array = null; // this is our empty PHP array

    while ($row = mysql_fetch_array($result)) // iterate through array
    {
    // store results from mysql in our own PHP array
    $employee_array[$count] = $row['Internal'];

    // increment counter
    $count++;
    }

    mysql_close($connection);

    // we can now iterate through our own array to retrieve the data
    for ($i = 0; $i < count($employee_array); $i++)
    //echo $employee_array[$i] . '<br />';

    //$arr1 = $employee_array[$i];

    //echo $arr1;

    // construct a new array:1,2....max(given array).
    $arr2=range(7,max($employee_array[$i]));

    // use array_diff to get the missing elements
    $missing = array_diff($arr2,$employee_array[$i]);
    What? Missing numbers? I don't get it. What exactly is your problem?

    Your code is very inefficient as well. Why do you need to iterate through your results twice? Why not just do all the actions needed the first time? Also, you don't need a counter to push a new index onto an array - you can use array_push OR just do $array[] to append a new numeric index.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •