2

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'];
    }
6
  • 1
    as written, it returns either a bool or an empty string, is that intentional? Commented Nov 22, 2011 at 3:36
  • err no I think i messed that up. I want it to check if its in an array, and if it is to check if 1 is in the array and if it is not an array then to do nothing, because if i don't check if it is an array and there are no values I get the warning Warning: in_array() expects parameter 2 to be array, Commented Nov 22, 2011 at 3:37
  • some more context would be helpful. Commented Nov 22, 2011 at 3:38
  • could you please be more descriptive of these variables? what do they mean and their contents? Commented Nov 22, 2011 at 3:40
  • 1
    Are you writing this numerous times? Is that why you want it more concise? You could create a function to spit out that code with conditional variables if that is the case. Commented Nov 22, 2011 at 3:53

4 Answers 4

3

The condition you have above could be more concisely written as:

is_array($current_color_id_array) && in_array('1', $current_color_id_array)

This is actually slightly different than what you had: your code will evaluate to either true or the empty string, this will be either true or false.

Of course, you could shrink the condition even more if you weren't checking if $current_color_id_array is 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.

$current_color_id_array = array();

$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'];
}

Then, you can just directly check for 1 in your array, and there's no chance you'll get a warning:

in_array('1', $current_color_id_array)
Sign up to request clarification or add additional context in comments.

2 Comments

I didnt think it did.. I edited the post to show how the array is made. The problem is the table is empty now so a lot of items dont have any colors yet meaning the array is empty which causes the warning to come up.
Thank you. I accepted this answer because well.. it makes more sense then creating a function since it's unnecessary to check if it's an array. Thanks for your help!
1

In light of the comment to create a function to echo the text, you could use this function:

function is_array_value($a,$b){
return (is_array($b)&&in_array($a, $b)) ? true : false;
}

Comments

0

The only thing I could think of would be to guarantee that $current_color_id_array is actually an array, by including

$current_color_id_array = array();

somewhere at the beginning of your script.

Comments

0

I would probably combine them into one conditional statement like so:

$someBool = (is_array($myArray) && in_array('1', $myArray, true));

Note that you should use strict comparison with the in_array() function when checking things like '1'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.