Page 1 of 1

Showing numbers from a database that doesn't excist

Posted: Sun Jul 23, 2006 5:05 am
by NiGHTFiRE
Hey,
I'm doing a script that looks in a mysql database and looks for numbers that doesn't excist in it and writes those numbers out.
A little example, I've got these numbers in my database and i want to show the two lowest numbers that doesn't excists in the database.
1
2
3
4
6
7
9

That's what i have in my database. Then i want it to write out the numbers 5 and 8 cause they don't excist in the database.
The code i got atm is:

Code: Select all

<?php
$datum = date("d/m/y"); 
$stora = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Å", "Ä", "Ö");
$sma = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "å", "ä", "ö"); 
$smaword2 = str_replace($stora, $sma, $word[2]);
$sql = "SELECT bud FROM $MYSQL_TABLE WHERE item='$smaword2'"; 
$res = mysql_query($sql); 
$c = 0; 
$min = array(); 
$num = mysql_num_rows($res); 
for ($i = 1; $i < $num; $i++) 
{ 
$row = mysql_fetch_assoc($res); 
if ($i != $row['bud']) 
{ 
$c++; 
$min[] = $i; 
} 
if ($c == 2) {
$tal = explode(" ", $c);
echo "De talen som inte har fått något bud är: ". $tal['1'];
echo " och ". $tal['2'];
break; 
} else {
echo "ERROR";
}
}
?>
But that won't work.
Thanks

Posted: Sun Jul 23, 2006 5:17 am
by daedalus__
This is a simple example:

Code: Select all

$smaword2 = str_replace($stora, $sma, $word[2]); 
$sql = "SELECT bud FROM $MYSQL_TABLE WHERE item='$smaword2'"; 
$result = mysql_query($sql);

$nums = array(1, 2, 3, 4, 6, 7, 9);

while ($row = mysql_fetch_row($result))
{
    if (in_array($row[0], $nums) != FALSE)
    {
        print "$row[0]";
    }
}
Now, this is assuming that 1, 2, 3, 4, 6, 7, 9 are the only numbers you want to check for. If you want to print out numbers that are not in your database, technically you want to print out an infinite amount of numbers.

It's not a perfect example but I guess it's a start. It is definately better than nothing. :)

Posted: Sun Jul 23, 2006 5:38 am
by NiGHTFiRE
Thanks,
How would I do if i want to check more numbers then to 9 and that all are possible to be left out. I mean sometimes it will be diffrent numbers then the once i wrote. How would i just check all the numbers in the database and then print out the two lowest once that isn't in the database.
i hope you understand.

Posted: Sun Jul 23, 2006 5:44 am
by daedalus__
That's more thinking than I can do right now but I'm sure that someone else can help you later this morning (3 am here).

Posted: Sun Jul 23, 2006 5:47 am
by NiGHTFiRE
Okey thanks. I'm going away so i'll be back in some hours. it's 1pm here ;)

This will work on 2 missing

Posted: Sun Jul 23, 2006 10:32 am
by ronverdonk
The following will work only if you look for 2 numbers missing.

Code: Select all

<?php
  $low=0; $found=0;
  $ver = mysql_connect(HOST, NAME, PSW) 
     or die(mysql_error());
  mysql_select_db(DB);
     or die(mysql_error());
  $result=mysql_query("select * from test ORDER BY id");

  while ($row = mysql_fetch_row($result)) {
    if ($found == 2)
        break;
    if ($row[0] == $low+1)
        $low++;
    else {
        $new=$low+1; print "missing: $new <br />";
        if ($low+2 == $row[0]);
           $low = $row[0];
        else
           $low++;
        $found++;
    }
  }
?>
Hope you get the idea
Ronald :mrgreen: