Checkbox looping in php while posting to database

六月ゝ 毕业季﹏ 提交于 2019-12-25 02:09:30

问题


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.

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:


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){       


来源:https://stackoverflow.com/questions/21724994/checkbox-looping-in-php-while-posting-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!