I am adding the following code to check whether a checkbox should be checked or not:
is_array($current_color_id_array) ? in_array('1', $current_color_id_array) : ''
I’m just wondering if there is nicer/more concise way to do it perhaps?
Edit: Sorry I was not clear enough, and actually my code was wrong..
This is supposed to check if $current_color_id_array is an array because apparently if I don’t check it will give me the error Warning: in_array() expects parameter 2 to be array
and it should also check if the value is in the array to mark the checkbox as checked.
this is all part of a checkbox function:
function tep_draw_checkbox_field($name, $value = '', $checked = false, $compare = '') {
return tep_draw_selection_field($name, 'checkbox', $value, $checked, $compare);
}
it is the third parameter. So if I can reask this I would say is there a better way of writing:
tep_draw_checkbox_field('color_id[]', '1', (is_array($current_color_id_array) && in_array('1', $current_color_id_array)))
and just in case, this is how the array is made:
$color_id_query = tep_db_query("select color_id from " . TABLE_PRODUCTS_TO_COLORS . " where products_id = '" . (int)$product['products_id'] . "'");
while ($color_id_row = mysql_fetch_array ($color_id_query)) {
$current_color_id_array[] = $color_id_row['color_id'];
}
The condition you have above could be more concisely written as:
This is actually slightly different than what you had: your code will evaluate to either
trueor the empty string, this will be eithertrueorfalse.Of course, you could shrink the condition even more if you weren’t checking if
$current_color_id_arrayis an array. The variable’s got “array” right there in the name, does your code actually leave open a possibility that it’s not really an array?Edit: Looking at your expanded question, since you’re completely in charge of creating the array, it’s easy to avoid the need to “make sure” your array exists. Just make sure you’re initializing your array variable, and you can be sure it will always be set and that it will always be an array.
Then, you can just directly check for
1in your array, and there’s no chance you’ll get a warning: