循环复选框在PHP同时发布到数据库 [英] Checkbox looping in php while posting to database

查看:83
本文介绍了循环复选框在PHP同时发布到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立在目前的成就系统,该系统上的复选框的地基工程(因为它未发布相关的)但是,我碰到了一个问题,我似乎无法换我的头以防万一吧,因为我不是方便与循环和数组。

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.

好吧,所以我做了复选框,那张贴在此页面上,充满appropiately,如见code以下,可以帮助我填补了网页的那部分群​​体。

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

正如你所见,我试图通过两个变量,即AchievementId拍摄和价值器isChecked(可以是0或1)时,会出现问题的时候,我要保存这些信息。我设置了充当调解人数据库中的表(Achievement_User,只有3项,这是用户ID,AchievementId和价值器isChecked)

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)

我的发车,我通过通过这里与AchievementId的来了,是与通过code楼下的复选框贴在一起的所有成就进行拍摄。

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

现在的问题在于,当我选中一个复选框,appearantly则数组被增加。取而代之的是40主菜它使(这是多少AchievementId的张贴,当检查一切OFF为默认值),它会为每个选中的值,这让我比较没用一个额外的阵列空间,因为我得到一个错误,那么该阵列可以结合

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.

有什么办法,我可以以防万一它的工作什么可以让我AchievementId比赛的阵列与我的价值器isChecked?

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

编辑:下一步到,整个foreach循环似乎并不当我试图合并阵列(即使他们在值相匹配)来工作了。所以在这里我的想法是,也许是有办法,我可以张贴从器isChecked阵列已经连接到它的AchievementId的价值。如果是这样,我怎么能来解决这一问题?

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?

推荐答案

你为什么不干脆放弃复选框成果的价值,并摆脱了隐藏输入的

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'];?>'>

然后在PHP

 foreach ($IsChecked as $AchievementId){       

这篇关于循环复选框在PHP同时发布到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆