$_GET 变量 - 为什么以前的数据没有隐藏?
$_GET variables - Why is previous data not hiding?
我正在创建一个包含类别、子类别和列表的网站。单击一个类别后,即计算页面更改以显示数据库中计算类别中的所有子类别。然后,当单击一个子类别时,将显示该子类别的列表,并将子类别选择发布到 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'] . ' ' . '<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_ 函数已被弃用,并且尚未考虑安全性,但当我一切正常时,我会担心这一点。
将 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>";
}
我同意 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"])
}
尝试使用 $_POST 方法,它会起作用
我想我找到了更好的解决方案。检查此代码:
<?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'] . ' ' . '<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 />";
}
}
?>
在这里,我更改了代码以匹配一种在我看来最接近修复您的代码的观点。由于未根据子类别名称选择列表数据库 table,我们可以做的是将列表块与子类别块分开,如果块在 if (isset($_GET['listingid']))
之前结束,则 if(isset($_GET['subcategory']))
并在您调用列表时从 URL 中删除子类别 GET 变量,这样您就不会 运行 子类别 if 块。
当您在 URL 中为 URL 提供子类别变量时会调用列表,并且在尝试显示您正在显示和隐藏的列表时单独不将子类别提供给 URL页面的那些阶段。
您不需要子类别值来显示列表,只需要列表列表,然后您应该只关心 listingid GET 变量。
我认为你的代码没问题,只需要修正这一行:
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']);
}
希望对您有所帮助。
使用下面的代码:
<?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'] . ' ' . '<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';
?>
我正在创建一个包含类别、子类别和列表的网站。单击一个类别后,即计算页面更改以显示数据库中计算类别中的所有子类别。然后,当单击一个子类别时,将显示该子类别的列表,并将子类别选择发布到 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'] . ' ' . '<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_ 函数已被弃用,并且尚未考虑安全性,但当我一切正常时,我会担心这一点。
将 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>";
}
我同意 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"])
}
尝试使用 $_POST 方法,它会起作用
我想我找到了更好的解决方案。检查此代码:
<?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'] . ' ' . '<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 />";
}
}
?>
在这里,我更改了代码以匹配一种在我看来最接近修复您的代码的观点。由于未根据子类别名称选择列表数据库 table,我们可以做的是将列表块与子类别块分开,如果块在 if (isset($_GET['listingid']))
之前结束,则 if(isset($_GET['subcategory']))
并在您调用列表时从 URL 中删除子类别 GET 变量,这样您就不会 运行 子类别 if 块。
当您在 URL 中为 URL 提供子类别变量时会调用列表,并且在尝试显示您正在显示和隐藏的列表时单独不将子类别提供给 URL页面的那些阶段。
您不需要子类别值来显示列表,只需要列表列表,然后您应该只关心 listingid GET 变量。
我认为你的代码没问题,只需要修正这一行:
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']);
}
希望对您有所帮助。
使用下面的代码:
<?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'] . ' ' . '<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';
?>