All, I have the following code:
$qry = "Select * from vendor_options order by vendor_option_name ASC";
$result = mysql_query($qry);
while($resultset = mysql_fetch_array($result)){
if(isset($_SESSION['pav_choosen_vendor_categories'])){
for($z=0;$z<$_SESSION['pav_choosen_vendor_categories'];$z++){
$sVendorId = $_SESSION['pav_vendor_categories_' . $z];
if($sVendorId==$resultset['vendor_option_id']){
$vendor_cats_choosen[] = $sVendorId;
}
}
if(in_array($resultset['vendor_option_id'],$vendor_cats_choosen)){
?>
<input type="checkbox" value="<?php echo $resultset['vendor_option_id']; ?>" class="select_vendor" name="vendor_categories[]" checked><?php echo $resultset['vendor_option_name']; ?><br>
<?php
}else{
?>
<input type="checkbox" value="<?php echo $resultset['vendor_option_id']; ?>" class="select_vendor" name="vendor_categories[]"><?php echo $resultset['vendor_option_name']; ?><br>
<?php
}
}
}
I'm trying to check to see if the value returned in the mysql_fetch_array is already in my array. Say the first value it finds in the array is in the fourth iteration of the while loop. I'll get the following error:
Warning: in_array() expects parameter 2 to be array, null
Once it gets to a value that is in the array the rest of them work fine. Why does itgive an error for the first couple? Thanks.
It looks like you have not initialized $vendor_cats_chosen
to be an array, and so if the condition if($sVendorId==$resultset['vendor_option_id'])
is not true, no elements will be appended to it, turning it implicitly into an array.
Initialize it before the while
loop. You should just about always initialize arrays before use.
// Initialize the array
$vendor_cats_chosen = array();
while($resultset = mysql_fetch_array($result)){
....
Now, when your in_array()
statement executes, the array may be empty, but will be a valid array.
// $vendor_cats_chosen might be an empty array, or may have elements.
if(in_array($resultset['vendor_option_id'],$vendor_cats_choosen)){
The problem is that your array not always creates. To fix this issue just add
$vendor_cats_choosen = array();
somewhere before while
in your code.
You only ever populate the array $vendor_cats_choosen
inside an if
statement, which means it can potentially contain no values. You also do not declare it before you start the loop which populates it - which you should do anyway, because adding a value to an undeclared array will emit an E_NOTICE
.
Add the line
$vendor_cats_choosen = array();
...at the top of the script and the error will disappear. If you think this array should contain values, you may need to examine the logic in your if
statement.
In order to eliminate the warning message, two approaches can be followed:
1) use of @
before the statement which is generating the warning message ( However, this is not an advisable engineering approach )
2) prior to using the array
object, it can be filtered out in an if-else
. For example, in your case, you can add this line
if( $vendor_cats_choosen ){
if(in_array($resultset['vendor_option_id'],$vendor_cats_choosen)){
?>
<input type="checkbox" value="<?php echo $resultset['vendor_option_id']; ?>" class="select_vendor" name="vendor_categories[]" checked><?php echo $resultset['vendor_option_name']; ?><br>
<?php
}else{
?>
<input type="checkbox" value="<?php echo $resultset['vendor_option_id']; ?>" class="select_vendor" name="vendor_categories[]"><?php echo $resultset['vendor_option_name']; ?><br>
<?php
}
} ?>