1

I have been trying to populate a list of checkboxes which values and names are retrieved from MySQL as well. I believe I need to match two iD's together to be able to mark it as checked but I am not sure at the moment.

$boardsiD = $pClass->checkedBoards();

foreach($pClass->retrieveBoards() as $data)
{
    $boardiD    =   $data['boardiD'];
    $boardName  =   $data['boardName'];

    if($boardiD == $boardsiD) ------------------ 1 ---------------------
    {
        $checked = 'checked="1"';
    }

    echo '
        <li><input '.$checked.' type="checkbox" value="'.$boardiD.'">'.$boardName.'</li>
    ';
}

Where the dashes with the number one in the middle is, I am there checking if the board id's are the same and if so then the checked="1" is applied. But it does not seem to be working.

The following is the output for the $boardsiD; These are the checkboxes that have been checekd in the database; saved the ids which is what I am trying to match against the $boardiD = $data['boardiD'] which is the whole list of id's(about 30 id's).

Array (
        [0] => Array
                    ([board_iD] => 106468047379795203)
        [1] => Array
                    ([board_iD] => 286119451265381250)
        [2] => Array
                    ([board_iD] => 136234026166934321)
        [3] => Array
                    ([board_iD] => 468022654964640361)
        [4] => Array
                    ([board_iD] => 409757334785529893)
        [5] => Array
                    ([board_iD] => 409757334785575605)
        [6] => Array
                    ([board_iD] => 490681390589888313)
        ) 

I'm not sure if I can use preg_match for this or if I even should, I tried but it did not work very well, any ideas why the following does not work?

17
  • 3
    Sidenote: If you want it to be checked, you'd do $checked = 'checked'; if that's your intention. Unless you want your checked value to appear as <input checked="1" Commented May 20, 2014 at 5:31
  • The above is correct fred, thank you. I want the box input box to be set to 1(checked="1") if the id matches with any that is retrieved from MySQL. That is my goal. Commented May 20, 2014 at 5:32
  • 1
    You probably could make use of a WHERE clause too, straight in your query. Commented May 20, 2014 at 5:56
  • 1
    There's also in_array() that could be of added use. Commented May 20, 2014 at 6:00
  • 1
    You're welcome. Keep me posted. Cheers (am off to Lah-Lah Land). Commented May 20, 2014 at 6:07

2 Answers 2

1

checked="1" is invalid HTML. The correct Syntax for HTML4 would be

<input type="checkbox" value="someValue" checked>

or for HTML5

<input type="checkbox" value="someValue" checked="checked" />

To check if your $boardID is a value of $boardsID you can do the following:

$checked = '';

for($i = 0; !$checked && $i < sizeof($boardsID); $i++)
  if($boardID == $boardsID[$i]['board_iD'])
    $checked = 'checked="checked"';
Sign up to request clarification or add additional context in comments.

3 Comments

@Fred-ii-, It was pretty much the same idea, it would pretty much be considered the same but I do have a question. Why didn't the foreach() work but the for() worked just fine, I though that foreach() also counted all that was returned from MySQL..
@iBrazilian2 This page will explain it better than I can ibmphp.blogspot.ca/2012/10/… --- Basically, for loop is to do something over and over again until the task has been completed. foreach works only on arrays. Even it will run the loop until end of the array.
@Fred-ii-, be a dear and check this out. (When/if you have time) stackoverflow.com/questions/23796257/…
1

Your $boardsiD is a 2 dimentional array so lets convert it to a single dimentional array first.

$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($boardsiD));
$newBoardIDs = iterator_to_array($it, false);

then,

foreach($pClass->retrieveBoards() as $data)
{
    $boardiD    =   $data['boardiD'];
    $boardName  =   $data['boardName'];

    if(in_array($boardID,$newBoardIDs))
    {
        $checked = 'checked="1"';
    }

    echo '
        <li><input '.$checked.' type="checkbox" value="'.$boardiD.'">'.$boardName.'</li>
    ';
}

should solve your problem.

1 Comment

Did you go over the 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.