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

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

问题描述

我现在正在建立一个成就系统,在复选框的基础上工作(因为它的没有post相关)然而,我碰到一个问题,我不能把我的头围绕它,因为我不是

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



如你所见,我试图通过2个变量,即AchievementId,和IsChecked值(可以是0或1)当我要保存此信息时,会出现问题。我在数据库中设置了一个表作为中介者(Achievement_User,只有3个条目,即UserId,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)

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

现在问题出在事实,当我检查一个复选框,表面上Array增加。而不是40个Entrees它(它是多少AchievementId的发布,并且当检查一切关闭是默认值),它为每个选中的值创建一个额外的数组空间,这使我的比较无用,导致我得到一个错误,那么数组可以组合。

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天全站免登陆