0

I am building at the moment an achievement system that works on the foundation of checkboxes (since its not post related) However, I bumped into a problem, and I cant seem to wrap my head arround it, since I am not that handy with loops and Arrays.

enter image description here

Okay, So I made group of checkboxes, that are posted on this page and filled appropiately, as see the code below that helps me fill that part of the page.

function MakeProfessionlist(){
    $result = LoadListProffesion();
    $i = 0;
    echo '<table class="Overview">';
    while($row = mysqli_fetch_array($result)){
        $i++;
        if(($i % 2) == 0){
            echo "<td class='small'><td><input id='achievementHidden'  type='hidden' value='0' name='IsChecked[]'>";
            echo "<input type='checkbox' id='achievement' name='IsChecked[]' value = '1'>";
            echo "<input name='AchievementId[]' type='hidden' value='".$row['AchievementId']."'></td>";
            echo "<td class='big'>".$row["AchievementName"]."</td></tr>";
        }else{
            echo "<tr><td class='small'><input id='achievementHidden'  type='hidden' value='0' name='IsChecked[]'>";
            echo "<input type='checkbox'id='achievement' name='IsChecked[]' value = '1'>" ;
            echo "<input name='AchievementId[]' type='hidden' value='".$row['AchievementId']."'></td>";
            echo "<td class='big'>".$row["AchievementName"]."</td>";
            }
    }
    echo '</table>';
}

As you see, i try to shoot through 2 variables, namely the AchievementId, and the IsChecked value (can be either 0 or 1)The problem occurs when I am going to save this information. I set up a table within the database that acts as mediator (The Achievement_User, with just 3 entries, which are UserId, AchievementId and the IsChecked Value)

My start was that I shoot through all the achievements that come through here with the AchievementId's that are posted together with the checkbox through the code down below.

if(isset($_POST['AchievementSaveData']))
    {
    // print_r($_POST);
    $checkbox = isset($_POST['IsChecked']) ? $_POST['IsChecked'] : array();
    $Achievement = isset($_POST['AchievementId']) ? $_POST['AchievementId'] : array();
    foreach (array_combine($checkbox, $Achievement) as $IsChecked => $AchievementId){       
        $sql="SELECT * FROM Achievement_User WHERE UserId='".$UserId."' AND AchievementId ='".$AchievementId."'" ;
        $result=mysqli_query($sql); 
        $count=mysqli_num_rows($result);    

        if ($count==1){
            echo 'Update datarow with UserID '.$UserId.', AchievementId '. $AchievementId.' and with a value of '.$IsChecked;' <br>';
            }
        else{
            echo 'Create datarow with UserID '.$UserId.', AchievementId '. $AchievementId.' and with a value of '.$IsChecked.' <br>';
        }
    }
}

Now the problem lies in the fact that when I check a checkbox, appearantly the Array gets increased. Instead of the 40 entrees it makes (which is how many AchievementId's are posted, and when checked everything off is the default) it creates an extra array space for each checked value, which makes my comparison useless, cause i get an error then that the arrays can be combined.

Is there any way I can work arround it what would make my array of AchievementId's match up with my IsChecked Value?

Edit: Next to that, the whole foreach loop doesn't seem to work anymore when I tried to merge the arrays (even if they match in values). So my thought here is maybe is there a way I can post the array from IsChecked with already the value of the AchievementId attached to it. If that is so, how could I work this out?

1 Answer 1

1

why don't you just give the checkbox the value of the achievement and get rid of the hidden input

<input type='checkbox' id='achievement' name='IsChecked[]' value = '<?php echo $row['AchievementId'];?>'>

then in the php

 foreach ($IsChecked as $AchievementId){       
Sign up to request clarification or add additional context in comments.

5 Comments

The problem lies in that approach that I cant define the ones that are not checked (later for the update statement, that when I have for example some achievements). Hence I am posting 1 and 0 to the database. I think I got it solved by changing the IsChecked[] and inserting in the IsChecked[] the ID from the Achievement. At least, now I need to find out if I can actually also use the Key from an array to define the variable in it, which probably will. Its been a while and I am very rusty with PHP at the moment.
the ones that are not checked won't arrive, so if none is checked you $isChecked array will be empty, the only difference that at the place of getting 1 for the ones that are checked, you'll get the achievement id
hmmm, you have a good point, i am gonna see how I can work this out. I just have to change the update queries with an extra if else statement this way, but I get your drift.
Jep did work out, thank you. That was the insight I needed in the problem!
In all cases was thinking about it you can't have done it in another since the ischecked array contains only the checked checkboxes as for the achievementid array contains all the hidden inputs so there was no way you could have mapped both arrays together. Good to know it worked :)

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.