...

View Full Version : How do i get missing internal no`s



coolumanga
01-24-2012, 03:55 AM
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]);

BluePanther
01-24-2012, 04:21 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum