dqbn76906 2015-05-27 01:49
浏览 21
已采纳

多个页面上的多个按钮几乎完全相同

I need some advice on my script. I am developing a roll call system, and I want to get out of a bad habit of mine.

Here is a pic of what I want: http://i.stack.imgur.com/UdqMQ.png

Specifically, the buttons above each of the day columns. Each of these buttons do nearly the same thing, but for different dates in my SQL database.

So here is the code I have:

Inputs:

<th><input type='submit' id='mAllR' name='mAllR' value='All Attended'></th>
<th><input type='submit' id='tAllR' name='tAllR' value='All Attended'></th>
<th><input type='submit' id='wAllR' name='wAllR' value='All Attended'></th>
<th><input type='submit' id='tAllR' name='tAllR' value='All Attended'></th>
<th><input type='submit' id='fAllR' name='fAllR' value='All Attended'></th>

As you can see, its basically each button having its own ID which will call a function by its name.

This would be fine by me if there were only 5 input buttons, which there are in my example, but there are 7 departments, which means 35 buttons. I do not want to make 35 functions that do ALMOST the same thing.

The code I have to work with that I can adapt is below:

if(isset($_POST['mAllR']))
            {
                    $index      = 0;
                    foreach($_POST as $key => $value)
                            {
                                    while($index < $_SESSION['amountT'])
                                            {
                                                    $index++;
                                                    $sql = ("UPDATE `Employee` SET `Monday`='Attended' WHERE `Job`='Remarketing'");
                                                    mysqli_query($con, $sql);
                                            }
                            }
                    if($sql)
                            {
                                    mysqli_close($con);
                                    sleep(4);
                                    echo '<meta http-equiv="refresh" content="0">';
                            }
            }

Each button, the way it is now, will needs its own version of this. (Do not worry about the sessions ETC, those are handled elsewhere to avoid overlapping/underlapping.

So really, the code is not the issue, I just want to optimize this into less code. Any suggestions are welcome.

  • 写回答

2条回答 默认 最新

  • doushangan3690 2015-05-27 02:25
    关注

    Since all of your buttons do almost the same thing, I'm guessing in each case just change some value in the query.

    I would do it like this

    First I would create an array containing the different data for each button (day and job since this is the only difference I can see the code needs)

    // Monday
    $array["mAllR"]["day"] = "Monday";
    $array["mAllR"]["job"] = "Marketing";
    // Tuesday
    $array["tAllR"]["day"] = "Tuesday";
    $array["tAllR"]["job"] = "Aonther job";
    // Etc...
    

    Then in the foreach loop I'd check for the button's name (see that in the array the main key is the name button)

    foreach($_POST as $key => $value) {
        $button = "";
        // I check for the value of the button to get the key (button's name)
        if($value == 'All attended') {
            $button = $key;
    
            // Then I call the 'day' and 'job' values from the array
            // where the main key is the button's name
            while($index < $_SESSION['amountT'])
            {
                $index++;
    
                // See that I pass the day and job  defined in the array using the button's name
                $sql = ("UPDATE `Employee` SET `"+$array[$button]["day"]+"`='Attended' WHERE `Job`='"+$array[$button]["job"]+"'");
                mysqli_query($con, $sql);
            }
        }
    }
    

    Edit : Wrap it inside a function.

    // Wrap
    function postFunc() {
        // The code above
    }
    
    // Use
    if(isset($_POST)) {
        postFunc();
    }
    

    That would be my approach. But it has a cons, since I'm checking for the value of the button and doing everything inside that condition I lose control over every other field you might want to control. I don't see in the code you're trying to control another field, so this approach might be useful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题