我正在创建一个包含类别,子类别和列表的网站。在点击类别时,即计算页面改变以显示来自数据库中处于计算类别中的所有子类别。然后,当单击子类别时,将显示该子类别的列表,并将子类别选择发布到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_函数,并且还没有考虑安全性,但是当我完成所有工作时会担心这个问题。
答案 0 :(得分:2)
我认为您的代码很好,只需修复此行:
if (isset($_GET['subcategory']) && trim($_GET['subcategory']) != '') {
$subcategory = trim($_GET['subcategory']) != '';
}
为什么要为$subcategory
var指定一个布尔值,然后在其上触发查询。声明:
$subcategory = trim($_GET['subcategory']) != '';
如果TRUE
为非空或非空,则会给1($_GET['subcategory']
)。
您应该将行更改为:
if (isset($_GET['subcategory']) && trim($_GET['subcategory']) != '') {
$subcategory = trim($_GET['subcategory']);
}
希望这会对你有所帮助。
答案 1 :(得分:1)
将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>";
}
答案 2 :(得分:1)
我认为我有更好的解决方案。检查此代码:
<?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 />";
}
}
?>
在这里,我更改了代码以匹配一种视图,在我看来,这是最接近修复代码的方式。由于未根据子类别名称选择列表数据库表,我们可以做的是将列表块与子类别块分隔为if(isset($_GET['subcategory']))
if块在if (isset($_GET['listingid']))
之前结束并删除子类别当您调用列表时,从URL中获取GET变量,这样您就不会在阻止时运行子类别。
当您在URL中使用子类别变量提供URL并且在尝试显示您正在显示的列表并隐藏页面的这些阶段时,单独未将子类别提供给URL时,会发生这样的情况。
您不需要子类别值来显示列表,只显示列表清单,然后您应该只关心listingid GET变量。
答案 3 :(得分:1)
使用以下代码:
<?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';
?>
答案 4 :(得分:0)
我同意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"])
}
答案 5 :(得分:0)
尝试使用$ _POST方法,它会起作用