1

我有一个名为 categoria 的表,有 10 行:

(idcateg, descri) VALUES 
(1, 'Action'), 
(2, 'Classic'), 
(3, 'Fight'), 
(4, 'Others'), 
(5, 'Puzzles'), 
(6, 'Racing'), 
(7, 'Shooting'), 
(8, 'Sports'), 
(9, 'Tower Defense'), 
(10, 'Zombie'); 

它都链接到一个名为 link_categoria.php 的页面

<a href="link_categoria.php?cat=1">Action</a>
<a href="link_categoria.php?cat=2">Classic</a>
<a href="link_categoria.php?cat=3">Fight</a>
<a href="link_categoria.php?cat=4">Others</a>
...

我想知道的是,像这个网站:http ://www.gameonline.org 我想在点击类别时更改每个页面的信息。我怎么做?

谢谢

mysql_select_db($database_gameconnection, $gameconnection);
$query_Recordset1 = "SELECT * FROM categoria ORDER BY categoria.descri";
$Recordset1 = mysql_query($query_Recordset1, $gameconnection) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

while ($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{
echo '<a href="link_categoria.php?cat='.
   $row_Recordset1['idcateg'] .'">'.
   $row_Recordset1['descri']. '</a><br>';
}
4

1 回答 1

0

link_categoria.php您可以使用获取类别$_GET['cat']

// Check if $_GET['cat'] is set [ie. ?cat=##] and is numeric
if(isset($_GET['cat']) && is_numeric($_GET['cat'])){

  // Get categoria and escape it before using in the query
  $cat = mysql_real_escape_string($_GET['cat']);

  // Query to get categoria title
  $sql_categoria = "SELECT * FROM categoria WHERE idcateg = $cat";
  $query_categoria = mysql_query($sql_categoria, $gameconnection) or die(mysql_error()); 
  $categoria = mysql_fetch_assoc($query_categoria);

  // Echo the categoria title
  echo '<h1>'.$categoria['descri']. '</h1>';

} // ends if(isset($_GET['cat']) && is_numeric($_GET['cat']))

假设您有一个按类别列出的游戏表

gameid | idcateg | game
  1         3      game1
  2         5      game2
....

您可以执行类似的查询以获取该类别中的游戏列表

// Check if $_GET['cat'] is set [ie. ?cat=##] and is numeric
if(isset($_GET['cat']) && is_numeric($_GET['cat'])){

  // Get categoria and escape it before using in the query
  $cat = mysql_real_escape_string($_GET['cat']);

  $query_Recordset1 = "SELECT * FROM games WHERE idcateg = $cat";
  $Recordset1 = mysql_query($query_Recordset1, $gameconnection) or die(mysql_error());
  $totalRows_Recordset1 = mysql_num_rows($Recordset1);

  while ($row_Recordset1 = mysql_fetch_assoc($Recordset1))
  {
  echo '<a href="game.php?gameid='.
     $row_Recordset1['gameid'] .'">'.
     $row_Recordset1['game']. '</a><br>';
  }
}

请注意,从文档中 - 自mysql_*PHP 5.5.0 起,该扩展已被弃用,并将在未来被删除。相反,应该使用MySQLiPDO_MySQL扩展。另请参阅MySQL:选择 API指南和相关的常见问题解答以获取更多信息。

于 2013-04-29T01:35:20.263 回答