【问题标题】:$_GET variables - Why is previous data not hiding?$_GET 变量 - 为什么以前的数据不隐藏?
【发布时间】:2015-01-21 23:17:02
【问题描述】:

我正在创建一个包含类别、子类别和列表的网站。单击一个类别(即计算页面)后,页面将更改以显示数据库中位于计算类别中的所有子类别。然后,当单击子类别时,将显示该子类别的列表,并将子类别选择发布到 URL。然后,当单击列表时,会发生同样的事情。没关系,但是当我尝试显示列表详细信息时,它应该隐藏列表,但这并没有发生。有谁知道为什么?我使用的代码如下:

<?php
include 'core/init.php';
include 'includes/overall/header.php';
?>

<h1> Computing </h1>

<?php
$subcategory = 'Computing';

if (isset($_GET['subcategory']) && trim($_GET['subcategory']) != '') {
    $subcategory = trim($_GET['subcategory']) != '';
}

$sql = "SELECT * FROM categories WHERE category = '$subcategory' ORDER BY subcategory ASC";
$result = mysql_query($sql) or die(mysql_error() . "<br>" . $sql);
$catNo = 1;
echo "<table class='categorytable'> <tr class='categoryrow'>";
while ($row = mysql_fetch_array($result)) {
    echo '<td class="categorydata"><a href="computing.php?subcategory=' . strtolower($row['subcategory']) . '"><img class="catimg" src="' . $row['subcategory_img_path'] . '" border="0" /></br>' . $row['subcategory'] . '</a></td>';
    if ($catNo % 3 == 0) {

        echo "</tr><tr>";
    }
    $catNo++;
}
echo "</tr> </table>";
?>
<?php
if (isset($_GET['subcategory'])) {
    $subcat = $_GET['subcategory'];

    $sql2 = "SELECT * FROM listings WHERE subcategory = '$subcat' ORDER BY title ASC";
    $result2 = mysql_query($sql2) or die(mysql_error() . "<br>" . $sql2);
    $catNo = 1;
    echo "<table class='categorytable'> <tr class='categoryrow'>";
    while ($row2 = mysql_fetch_array($result2)) {
        echo '<td class="categorydata"><a href="computing.php?subcategory=' . $subcat . '&' . 'listingid=' . ($row2['listing_id']) . '"><img class="catimg" src="' . $row2['main_image'] . '" border="0" /></br>' . $row2['title'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . '<i class="fa fa-cog"></i>' . ' ' . $row2['cogs'] . ' per week' . '</a></td>';
        if ($catNo % 3 == 0) {

            echo "</tr><tr>";
        }
        $catNo++;
    }
    var_dump($_GET);


    echo "</tr> </table>";
?>
<?php
    if (isset($_GET['listingid'])) {
        $listingid = $_GET['listingid'];

        $sql3 = "SELECT * FROM listings WHERE listing_id='$listingid'";
        $result3 = mysql_query($sql3) or die(mysql_error() . "<br>" . $sql3);
        while ($row3 = mysql_fetch_assoc($result3)) {
            //whatever your values are
            echo $row3['description'] . "<br />";
        }
    }
}
?>

</br>

<?php
include 'includes/overall/footer.php';
?>

我知道 php_ 函数已被弃用并且尚未考虑安全性,但当我一切正常时会担心这一点。

【问题讨论】:

    标签: php variables get hide


    【解决方案1】:

    我认为您的代码很好,只需要修复此行:

    if (isset($_GET['subcategory']) && trim($_GET['subcategory']) != '') {
        $subcategory = trim($_GET['subcategory']) != '';
    }
    

    为什么要为$subcategory var 分配一个布尔值,然后对其进行查询。声明:

    $subcategory = trim($_GET['subcategory']) != '';
    

    如果$_GET['subcategory'] 不为空或不为空,将给出 1 (TRUE)。 您应该将行更改为:

    if (isset($_GET['subcategory']) && trim($_GET['subcategory']) != '') {
        $subcategory = trim($_GET['subcategory']);
    }
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案2】:

      将while部分放入if

      if(!isset($_GET['subcategory'])){
      $sql = "SELECT * FROM categories WHERE category = '$subcategory' ORDER BY subcategory ASC";
      $result = mysql_query($sql) or die(mysql_error() . "<br>" . $sql);
      $catNo = 1;
      echo "<table class='categorytable'> <tr class='categoryrow'>";
      while ($row = mysql_fetch_array($result)) {
          echo '<td class="categorydata"><a href="computing.php?subcategory=' . strtolower($row['subcategory']) . '"><img class="catimg" src="' . $row['subcategory_img_path'] . '" border="0" /></br>' . $row['subcategory'] . '</a></td>';
          if ($catNo % 3 == 0) {
      
              echo "</tr><tr>";
          }
          $catNo++;
      }
      echo "</tr> </table>";
      
      }
      

      【讨论】:

      • 子类别位似乎工作正常,但我已经更改了您建议的 $subcategory 内容。问题在于单击列表时隐藏列表
      • @StevenTrainor 这很简单。只需将列表代码放入if (!isset($_GET['subcategory'])) {/*put it here*/}
      • 对不起,我不明白你的意思:/
      • 我在每个 sql 查询之前添加了该问题,但尚未解决单击列表时隐藏列表但单击子类别时它正在工作的问题(隐藏子类别和列表显示)
      • @StevenTrainor 当列表被点击时,URL 中的变量是什么?也将其放入if
      【解决方案3】:

      我想我有一个更好的解决方案。检查此代码:

      <?php
      if (isset($_GET['subcategory'])) {
          $subcat = $_GET['subcategory'];
      
          $sql2 = "SELECT * FROM listings WHERE subcategory = '$subcat' ORDER BY title ASC";
          $result2 = mysql_query($sql2) or die(mysql_error() . "<br>" . $sql2);
          $catNo = 1;
          echo "<table class='categorytable'> <tr class='categoryrow'>";
          while ($row2 = mysql_fetch_array($result2)) {
              echo '<td class="categorydata"><a href="computing.php?listingid=' . ($row2['listing_id']) . '"><img class="catimg" src="' . $row2['main_image'] . '" border="0" /></br>' . $row2['title'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . '<i class="fa fa-cog"></i>' . ' ' . $row2['cogs'] . ' per week' . '</a></td>';
              if ($catNo % 3 == 0) {
      
                  echo "</tr><tr>";
              }
              $catNo++;
          }
          var_dump($_GET);
      
      
          echo "</tr> </table>";
      }
      ?>
      <?php
      if (isset($_GET['listingid'])) {
          $listingid = $_GET['listingid'];
      
          $sql3 = "SELECT * FROM listings WHERE listing_id='$listingid'";
          $result3 = mysql_query($sql3) or die(mysql_error() . "<br>" . $sql3);
          while ($row3 = mysql_fetch_assoc($result3)) {
              //whatever your values are
              echo $row3['description'] . "<br />";
          }
      }
      ?>
      

      在这里,我更改了代码以匹配一种在我看来是最接近修复代码的方式。由于未根据子类别名称选择列表数据库表,因此我们可以用if(isset($_GET['subcategory'])) 将列表块与子类别块分开,如果块在if (isset($_GET['listingid'])) 之前结束,并从调用列表时的 URL,因此如果阻止则不会运行子类别。 当您在 URL 中提供带有子类别变量的 URL 并且在尝试显示您正在显示的列表和隐藏页面的这些阶段时单独不将子类别提供给 URL 时,会调用列表。 您不需要子类别值来显示列表,只需列表列表,然后您应该只关心 listingid GET 变量。

      【讨论】:

        【解决方案4】:

        使用下面的代码:

        <?php
        $category = 'Computing';
        
        if (isset($_GET['category']) && trim($_GET['category']) != '') {
            $category = trim($_GET['category']);
        }
        
        $sql = "SELECT * FROM categories WHERE category = '$category' ORDER BY category ASC";
        $result = mysql_query($sql) or die(mysql_error() . "<br>" . $sql);
        $catNo = 1;
        echo "<table class='categorytable'> <tr class='categoryrow'>";
        while ($row = mysql_fetch_array($result)) {
            echo '<td class="categorydata"><a href="computing.php?category='. $category.'&subcategory=' . strtolower($row['subcategory']) . '"><img class="catimg" src="' . $row['subcategory_img_path'] . '" border="0" /></br>' . $row['subcategory'] . '</a></td>';
            if ($catNo % 3 == 0) {
        
                echo "</tr><tr>";
            }
            $catNo++;
        }
        echo "</tr> </table>";
        ?>
        <?php
        if (isset($_GET['subcategory'])) {
            $subcat = $_GET['subcategory'];
        
            $sql2 = "SELECT * FROM subcategory WHERE subcategory = '$subcat' ORDER BY title ASC";
            $result2 = mysql_query($sql2) or die(mysql_error() . "<br>" . $sql2);
            $catNo = 1;
            echo "<table class='categorytable'> <tr class='categoryrow'>";
            while ($row2 = mysql_fetch_array($result2)) {
                echo '<td class="categorydata"><a href="computing.php?category='. $category.'&subcategory=' . $subcat . '&' . 'listingid=' . ($row2['listing_id']) . '"><img class="catimg" src="' . $row2['main_image'] . '" border="0" /></br>' . $row2['title'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . '<i class="fa fa-cog"></i>' . ' ' . $row2['cogs'] . ' per week' . '</a></td>';
                if ($catNo % 3 == 0) {
        
                    echo "</tr><tr>";
                }
                $catNo++;
            }
            var_dump($_GET);
        
        
            echo "</tr> </table>";
        ?>
        <?php
            if (isset($_GET['listingid'])) {
                $listingid = $_GET['listingid'];
        
                $sql3 = "SELECT * FROM listings WHERE listing_id='$listingid'";
                $result3 = mysql_query($sql3) or die(mysql_error() . "<br>" . $sql3);
                while ($row3 = mysql_fetch_assoc($result3)) {
                    //whatever your values are
                    echo $row3['description'] . "<br />";
                }
            }
        }
        ?>
        
        </br>
        
        <?php
        include 'includes/overall/footer.php';
        ?>
        

        【讨论】:

          【解决方案5】:

          我同意 zairwolf 的观点,基本上 Steven,您需要一种方法来标记何时应该显示列表,对页面加载结构进行一些规划可能会有所帮助。以下是您实施的可能流程,如果我了解您的需求,它可能会改进您的实施。

          //Proposed layout
          
          function show_categories () {
             //draw the categories down the left side of screen with "/?category={$record->CATEGORYID}" in anchor href
          }
          
          function show_subcategories ($category) {
            //select the subcategories based on the passed category and output also with href as you have already
          }
          
          function show_listings ($subcategory) {
            //select all the listings based on the sub category sent here
          }
          
          //always show the categories ?
          show_categories ();
          
          //other logic when to display sub categories or listings
          if ($_REQUEST["subcategory"]) { 
           show_listings ($_REQUEST["subcategory"]);
          } else {
           show_subcategories ($_REQUEST["category"])
          }
          

          【讨论】:

            【解决方案6】:

            尝试使用 $_POST 方法,它会起作用

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-08-13
              • 2011-04-07
              • 1970-01-01
              • 2014-08-28
              • 2011-06-05
              • 1970-01-01
              相关资源
              最近更新 更多