0

如何使用 GET 参数遍历所有短语并将它们作为链接列表输出?

如何创建一个数组,用整数索引,然后使用 GET 变量来选择这些短语之一。

到目前为止我的代码;

$arr = array('1' => 'Que será, será.', '2' => 'C’est la vie!', '3' => 'Sich nicht um ungelegte Eier kümmern.', '4' => 'Ada asap, ada api.', '5' => 'Batti il ferro finché è caldo.');
?> 

<p><?php print $arr[1]; ?> - Whatever will be, will be.</p>
<p><?php print $arr[2]; ?> – That’s the life.</p> 
<p><?php print $arr[3]; ?> - Don’t count your chickens before they hatch.</p> 
<p><?php print $arr[4]; ?> - There's no smoke without fire.</p> 
<p><?php print $arr[5]; ?> - Strike while the iron is hot.</p>
4

3 回答 3

2

这应该这样做:

$key = (int)$_GET['key'];
if (isset($arr[$key]) {
   echo print $arr[$key]; 
} else {
   echo "Invalid option specified";
}
于 2012-11-13T10:35:01.670 回答
1
<?php  
  $key = mysql_real_escape_string($_GET['value']);//Pass this value from url and sanitize the inputs
  ?>
  <p><?php print $arr[$key]; ?>
于 2012-11-13T10:31:31.357 回答
1
$key = $_GET['index'];
$val = isset($arr[$key]) ? $arr[$key] : false;

编辑:为什么人们一直推荐 mysql_real_escape_string?这是用于数据库查询转义并向数据库发送请求(据我所知)。密钥甚至不需要转义,因为它没有被发送到数据库或输出。

于 2012-11-13T10:36:52.367 回答