需要只显示说明

时间:2014-03-16 10:03:41

标签: php

我是编码php的新手,我首先遇到了一个问题,但是这个网站上有人帮助我了!我已经修复了当您访问site.com/category.php时,您会看到所有类别的内容,当您在site.com/category.php?p=4上查看相似示例时,您会看到与category.php相同,但他只接受ID。当我去category.php时,我现在想要别的什么?p = 4只描述 以下是我的代码。

    <?php
include 'config.php';

// create a where clause
if (isset($_GET['p'])) $where="nameID=".(int)$_GET['p']; 
else $where=1;

// select all or specific using the where clause
$mysql = "SELECT * FROM category WHERE ".$where;
$result = mysql_query($mysql);


// if there are rows returned
if (mysql_num_rows($result)){
    // iterate
    while($data=mysql_fetch_array($result)){
    {
    echo "" . $data['description'] . ""; //displays only when you're on category.php?p=4
    }
    // only want to display this on category.php
        echo "<li><a href=\"category.php?p=" . $data['nameID'] . "\"><b>" . $data['name'] . "</b></a> added on: " . $data['date'] . ", about this game <b>" . $data['description'] . "</b>";
    }
}else{
    echo 'Nothing to find here';
}
?>

3 个答案:

答案 0 :(得分:0)

检查您的选择查询 $mysql = "SELECT * FROM category WHERE ".$where;

它不完整。

应该是

`$mysql = "SELECT * FROM category WHERE id like '$where'

`$mysql = "SELECT * FROM category WHERE id ='$where'

答案 1 :(得分:0)

使用if语句来确定是否设置了p URL参数。

将您想要显示的部分放在

中的category.php?p = ...页面上

if(isset($_GET['p']))

以及要在

的category.php页面上显示的部分

if(!isset($_GET['p']))

您还应该使用mysqli或PDO替换mysql,如果您使用<b>标记进行样式设置,则应将其替换为<span style='font-weight:bold'>

答案 2 :(得分:0)

我想这就是你需要/意味着如果没有请注释,以便我可以编辑它:

<?php
include 'config.php';

$id = $_GET['p']; //get the p (id) from the url

//check if the p (id) is in the url and not empty
if(isset($id) && !empty($id)) {
    //the query
    $query = mysql_query("SELECT * FROM `category` WHERE `nameID` = '$id'");
    $count = mysql_num_rows($query);

    if($count >= 1) {
        while($associate = mysql_fetch_assoc($query)) {
            echo $associate['description'];
        }
    }
} else {
    $query = mysql_query("SELECT * FROM `category`");

    $count = mysql_num_rows($query);

    if($count >= 1) {
        while($associate = mysql_fetch_assoc($query)) {
            echo '<li><a href="category.php?p='.$associate['nameID'].'"><b>'.$associate['name'].'</b></a> added on: '.$associate['date'].', about this game <b>'.$associate['description'].'</b>';
        }
    } else {
        echo 'nothing found';
    }
}
?>