2017-04-13 49 views
1

我正在從一個數據庫中獲取值並將一行一次吐出到表單文本輸出的程序中。要查看不同的行,有導航按鈕會改變您正在查看的當前行。除了每次按下其中一個按鈕時都會清除並重新初始化的按鈕除外。這些按鈕有四個,分別是「First」,「Previous」,「Next」和「Last」,它們將應該控制當前行的數字設置爲每個按鈕的相應值。PHP行導航按鈕開關總是在運行之前清除變量

這裏是一個不正常的部分:按任一個「第一」或「上一步」按鈕

`

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"; 
}` 

設置button_value(用來保持當前行的軌跡)設置爲0,按「Next」按鈕將button_value設置爲1,然後按下「Last」按鈕將該值設置爲所選表格的最大行數。

我真的不知道這裏發生了什麼。如果有更多的代碼需要幫助解決這個問題,我可以提供它,但我不想發佈全部226行代碼。

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(!isset($button_value)){ 
     $button_value = 0; 
     echo "reset the undefined var button_value <br>"; 
    } 

不包括在頂部,則變量「button_value」始終未設置在每個switch語句命中之前。

哦,這裏的按鈕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> 

回答

0

我終於想出了一個解決方案。通過使用會話全局變量,我可以使變量停止重置爲零。所以我把這個在我的代碼的頂部:

//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"; 
} 

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

這個我更新了我的代碼鍵管理部分利用會話後全球

相關問題