PHP行导航按钮开关总是在运行之前清除变量。

时间:2021-09-18 09:12:57

I am working on a program that gets values from a database and spits out one row at a time into a form text output. To see the different rows there are navigation buttons that will change the current row you are looking at. Everything works except for the buttons which get cleared and reinitialized every time you press one of them. There are four of these buttons, a "First", "Previous", "Next", and "Last" which set the number that is supposed to control the current row to the respective value for each button.

我正在编写一个程序,该程序从数据库中获取值,并一次将一行输出到一个表单文本输出中。要查看不同的行,有导航按钮将会改变当前的行。除了那些在每次按下一个按钮时清除和重新初始化的按钮以外,所有的操作都是有效的。其中有四个按钮,一个“第一个”、“前一个”、“下一个”和“最后一个”,它设置的数字应该控制当前行到每个按钮的相应值。

Here is the section that isn't working:

这里是不工作的部分:

`

if(isset($result)){
    $rows = mysqli_num_rows($result);
    if(isset($button_value)){
        echo "After isset(button_val) in buttons, val = ".$button_value."<br>";
        if(isset($_GET['table_nav'])){
            $button_ch = $_GET['table_nav'];
            switch($button_ch){
                case "First":
                    unset($button_ch);
                    echo "button_value in, before = ".$button_value."<br>";
                    $button_value = 0;
                    echo "button_value in = ".$button_value."<br>";
                    break;
                case "Previous":
                    unset($button_ch);
                    if($button_value > 0){
                        echo "button_value in, before = ".$button_value."<br>";
                        $button_value--;
                        echo "button_value in = ".$button_value."<br>";
                        break;
                    }else{
                        echo "Invalid button action. No previous rows.<br>";
                        break;
                    }
                    break;
                case "Next":
                    unset($button_ch);
                    if($button_value < $rows){
                        echo "button_value in, before = ".$button_value."<br>";
                        $button_value++;
                        echo "button_value in = ".$button_value."<br>";
                        break;
                    }else{
                        echo "Invalid button action. No next rows.<br>";
                        break;
                    }
                    break;
                case "Last":
                    unset($button_ch);
                    echo "button_value in, before = ".$button_value."<br>";
                    $button_value = $rows - 1;
                    echo "button_value in = ".$button_value."<br>";
                    break;
                default:
                    unset($button_ch);
                    echo "Error, invalid row navigation button value<br>";
            }
            echo "button val after set by case = ".$button_value;
        }//*/
    }
} else {
    echo "ERROR in result";
}`

Pressing either the "First" or "Previous" buttons sets the button_value (being used to keep track of the current row) to 0, pressing the "Next" button sets button_value to 1, and pressing the "Last" button sets the value to the maximum number of rows for the selected table.

按下“第一”或“之前”按钮设置button_value(用来跟踪当前行)到0,按下“Next”按钮将button_value设置为1,按“最后”按钮将值设置为所选表的最大行数。

I really can't figure out what's going on here. If there is any more of the code needed to help figure this out I can supply it but I didn't want to post all 226 lines of code.

我真搞不懂这里发生了什么。如果需要更多的代码来帮助解决这个问题,我可以提供它,但是我不想发布所有226行代码。

The larger chunk of PHP code is included below:

更大的PHP代码包含如下:

<?php
    global $table_array;
    //global $button_value;
    $table_array = array();
//* 
    if(!isset($button_value)){
        $button_value = 0;
        echo "reset the undecfined var button_value <br>";
    }//*/ 

    //$result = get_table($_GET["table_name"], $connection_to_mysql);
    if(isset($_GET["table_name"])){
        $table = $_GET["table_name"];
        //echo "table name is ".$table."<br><br>";
        $result = get_table($table, $connection_to_mysql);
        $rows = mysqli_num_rows($result);
        list($table_array, $table_cols) = parse_result($result, $table);
        }

        //The button handling section
        if(isset($result)){
            $rows = mysqli_num_rows($result);
            if(isset($button_value)){
                if(isset($_GET['table_nav'])){
                    $button_ch = $_GET['table_nav'];
                    switch($button_ch){
                        case "First":
                            unset($button_ch);
                            $button_value = 0;
                            echo "button_value in = ".$button_value."<br>";
                            break;
                        case "Previous":
                            unset($button_ch);
                            if($button_value > 0){
                                $button_value--;
                                break;
                            }else{
                                echo "Invalid button action. No previous rows.<br>";
                                break;
                            }
                            break;
                        case "Next":
                            unset($button_ch);
                            if($button_value < $rows){
                                echo "button_value in, before = ".$button_value."<br>";
                                $button_value++;
                                echo "button_value in = ".$button_value."<br>";
                                break;
                            }else{
                                echo "Invalid button action. No next rows.<br>";
                                break;
                            }
                            break;
                        case "Last":
                            unset($button_ch);
                            $button_value = $rows - 1;
                            echo "button_value in = ".$button_value."<br>";
                            break;
                        default:
                            unset($button_ch);
                            echo "Error, invalid row navigation button value<br>";
                    }
                    echo "button val after set by case = ".$button_value;
                }//*/
            }
        } else {
            echo "ERROR in result";
        }
    ?>

If the section of code

如果是代码段。

if(!isset($button_value)){
        $button_value = 0;
        echo "reset the undefined var button_value <br>";
    }

isn't included at the top, then the variable "button_value" is always unset before each switch statement hit.

不包括在顶部,然后变量“button_value”总是在每个switch语句命中之前未设置。

Oh, and here's the HTML for the buttons:

哦,这里是按钮的HTML:

<form action='' method="GET">
    <table>
        <tr>
            &nbsp;<input id="button_first" type="submit" name="table_nav" value="First">
        </tr>
        <tr>
            &nbsp;<input id="button_previous" type="submit" name="table_nav" value="Previous">
        </tr>
        <tr>
            &nbsp;<input id="button_next" type="submit" name="table_nav" value="Next">
        </tr>
        <tr>
            &nbsp;<input id="button_last" type="submit" name="table_nav" value="Last">
        </tr>
    </table>
</form> 

1 个解决方案

#1


0  

I finally figured out a solution. By using a session global variable I could make the variable stop resetting to zero. So I put this at the top of my code:

我终于想出了一个解决办法。通过使用一个会话全局变量,我可以使变量停止重置为零。我把它放在代码的顶部

if(!isset($_SESSION["button_value"])){
    $_SESSION["button_value"] = 0;
}

After this I updated the button management section of my code to utilize the session global:

在此之后,我更新了代码的按钮管理部分,以利用会话全局:

//The button handling section
if(isset($result)){
    $rows = mysqli_num_rows($result);
    //echo "Between isset(result) and isset(button_val) in buttons, rows = ".$rows."<br>";
        if(isset($_GET["table_nav"])){
            $button_ch = $_GET["table_nav"];
            $button_value = $_SESSION["button_value"];
            switch($button_ch){
                case "First":
                    unset($button_ch);
                    //echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                    $_SESSION["button_value"] = 0;
                    break;
                case "Previous":
                    unset($button_ch);
                    if($button_value > 0){
                        //echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                        $_SESSION["button_value"] = $button_value - 1;
                        break;
                    }else{
                        echo "Invalid button action. No previous rows.<br>";
                        break;
                    }
                    break;
                case "Next":
                    unset($button_ch);
                    if($button_value < $rows - 1){
                        echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                        $_SESSION["button_value"] = $button_value + 1;
                        break;
                    }else{
                        echo "Invalid button action. No next rows.<br>";
                        break;
                    }
                    break;
                case "Last":
                    unset($button_ch);
                    //echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                    $_SESSION["button_value"] = $rows - 1;
                    break;
                default:
                    unset($button_ch);
                    echo "Error, invalid row navigation button value<br>";
            }
            //echo "button val after set by case = ".$_SESSION["button_value"];
        }
}else{
    echo "ERROR in result";
}

#1


0  

I finally figured out a solution. By using a session global variable I could make the variable stop resetting to zero. So I put this at the top of my code:

我终于想出了一个解决办法。通过使用一个会话全局变量,我可以使变量停止重置为零。我把它放在代码的顶部

if(!isset($_SESSION["button_value"])){
    $_SESSION["button_value"] = 0;
}

After this I updated the button management section of my code to utilize the session global:

在此之后,我更新了代码的按钮管理部分,以利用会话全局:

//The button handling section
if(isset($result)){
    $rows = mysqli_num_rows($result);
    //echo "Between isset(result) and isset(button_val) in buttons, rows = ".$rows."<br>";
        if(isset($_GET["table_nav"])){
            $button_ch = $_GET["table_nav"];
            $button_value = $_SESSION["button_value"];
            switch($button_ch){
                case "First":
                    unset($button_ch);
                    //echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                    $_SESSION["button_value"] = 0;
                    break;
                case "Previous":
                    unset($button_ch);
                    if($button_value > 0){
                        //echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                        $_SESSION["button_value"] = $button_value - 1;
                        break;
                    }else{
                        echo "Invalid button action. No previous rows.<br>";
                        break;
                    }
                    break;
                case "Next":
                    unset($button_ch);
                    if($button_value < $rows - 1){
                        echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                        $_SESSION["button_value"] = $button_value + 1;
                        break;
                    }else{
                        echo "Invalid button action. No next rows.<br>";
                        break;
                    }
                    break;
                case "Last":
                    unset($button_ch);
                    //echo "button_value in, before = ".$_SESSION["button_value"]."<br>";
                    $_SESSION["button_value"] = $rows - 1;
                    break;
                default:
                    unset($button_ch);
                    echo "Error, invalid row navigation button value<br>";
            }
            //echo "button val after set by case = ".$_SESSION["button_value"];
        }
}else{
    echo "ERROR in result";
}