Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:41:42+00:00 2026-05-27T03:41:42+00:00

I am adding the following code to check whether a checkbox should be checked

  • 0

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'];
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:41:42+00:00Added an answer on May 27, 2026 at 3:41 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.awt.event.MouseAdapter() {
I have the following code for adding to/extracting from Zip. I'm trying to refactor
I am using the following code --- EDIT @annakata : Adding the complete code
Just having a hard time adding a certain bit of given code to add
Using the following code I get img tags from some html and check them
... It's not as silly as it sounds... I have the following code, which
I' m trying to enable adding/deleting rows to/from table using folowing code: Script: $(document).ready(function()
Normally, I would enable extensions by adding the following to .hg/hgrc : [extensions] hgext.win32text=
I am getting the following exception while adding data into database: org.hibernate.HibernateException: The database
I have the following JQuery statement and it is adding the class 'current' but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.