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
I don’t see how it could work in
any case(ok, in first iteration it can work, since$iis still string then). Take a look what parametersin_arrayacceptsIn 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$excldfilled with string “0”. Last argument evaluates toTRUE(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:
and