0

below is my code through which I'm trying get certain values to a drop down box from a SQL database. I'm also trying to exclude some of the data before loading to the dropdown box.

In my code $excld[] works fine and the expected zero values are not shown when the dropdown is populated, but the values I expect to exclude via $exclude=$rec['chkNum']; doesn't work, or else the values I dont want to be in the dropdown still shows. Can someone tell me is there anything wrong in the approach? thanks.

$exclude = array();
$query = "SELECT * FROM invoentry WHERE dist_inv='$distUsr'";
$runx=mysqli_query($db,$query) or die ("SQL Error");
$norx=mysqli_num_rows($runx);

while ($rec = mysqli_fetch_array($runx))
{
    $exclude[] = $rec['chkNum']; $excld[] = '0';
}

$SQLx="SELECT * FROM newchk WHERE dist_chk='$distUsr'";
$runx=mysqli_query($db,$SQLx) or die ("SQL Error");
$norx=mysqli_num_rows($runx);

while ($rec = mysqli_fetch_array($runx))
    {
        if($rec['sbstart'] != '0' & $rec['sbend'] != '0') {
        for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
        {
            if (!in_array($i, $exclude, $excld))
            {
                echo "<option id='options' value='$i'>$i<br></option>";
            }
        } }

         if($rec['gwstart'] != '0' & $rec['gwend'] != '0') {
        for($i=$rec['gwstart']; $i<=$rec['gwend']; $i++)
        {
            if (!in_array($i, $exclude, $excld))
            {
                echo "<option id='options' value='$i'>$i<br></option>";
            }
        } }
    }   

EDIT :

Database structure is as follows; Database name :regional_data Two tables in the same database invoentry and newchk

invoentry:

usr_inv dist_inv chkNum InvoNum
---------------------------------
John     Guardian   300455   457gXT

newchk:

usr_chk  dist_chk sbstart sbend totsb gwstart gwend totgw
----------------------------------------------------------
John     Guardian 300400  300550 151   300     310   10
2
  • 1
    Could you edit your question to show us the structure of the invoentry and newchk tables? There's probably a pure-SQL solution to this problem. Commented Dec 9, 2012 at 8:43
  • I don't understand the intended use of $excld, why you are using bitwise &, why $exclude is used to filter sbstart, sbend, gwstart, and gwend. <option> tags don't typically get id attributes, and if they do, all id values in an HTML document must be unique. Your sample data only contains one row per table; might there be more than one from either table? It is unnecessary to duplicate an option's text as its value attribute - you can safely omit the value declaration. Commented Oct 6 at 17:31

1 Answer 1

1

I don't see how it could work in any case (ok, in first iteration it can work, since $i is still string then). Take a look what parameters in_array accepts

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

In your case, you are passing: integer $i, array of strings $exclude (everything fetched from database is a string, unless you cast it yourself) and non empty array $excld filled with string "0". Last argument evaluates to TRUE (array is not empty) so php is checking not only values but also types. Since you ara passing integer and array of strings, php would not find any elements inside with same type and value, so it will print all elements.

What to change to make it work:

while ($rec = mysqli_fetch_array($runx))
{
    $exclude[] = $rec['chkNum']; //remove $excld[] = '0';
}
// add 0 to $exclude
$exclude[] = '0';

and

if (!in_array($i, $exclude)) //remove , $excl
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.