我一直在使用PHP数组创建动态页面但是当我点击显示我点击的数组的链接时它显示了所有数组这里我创建的示例向你们展示http://arqetech.net/arrayTest/
以下是我使用过的代码:
index.php我已经列出了一个用于回显所有数组的模板:
<ul>
<?php foreach ($menuItems as $dish => $item) { ?>
<li>
<a href="dish.php?item=<?php echo $dish; ?>">
<h1><?php echo $item[title]; ?></h1>
<small><?php echo $item[utime]; ?></small>
</a>
</li>
<?php } ?>
</ul>
我把数组放在其中的arrays.php文件:
`
$menuItems = array(
"hello-world" => array(
title => "Hello World, This Is My First POST!!!",
utime => "June 13, 2014",
content => "<p>You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. </p>"
),
"my-second-post-weeheeeee" => array(
title => "one+1",
utime => "There was an ERROR showing some data!",
content => "<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>"
),
);
&GT;`
article.php PHP会动态放置我点击的数组:
function strip_bad_chars( $item ) {
$output = preg_replace( "/[^a-zA-Z0-9_-]/", "", $input );
return $output;
}
if (isset($_GET['item'])) {
$menuItem = strip_bad_chars( $_GET['item'] );
$dish = $menuItems[$menuItem];
}
?&GT;
<ul>
<li>
<?php foreach ($menuItems as $dish => $item) { ?>
<h1><?php echo $item[title]; ?></h1>
<small><?php echo $item[utime]; ?></small>
<?php echo $item[content]; ?>
<?php } ?>
</li>
</ul>
但仍然没有回应我点击的数组链接,请帮助我()。 Thak You!
答案 0 :(得分:0)
在article.php中更改此内容:
<?php foreach ($menuItems as $dish => $item) { ?>
<h1><?php echo $item[title]; ?></h1>
<small><?php echo $item[utime]; ?></small>
<?php echo $item[content]; ?>
<?php } ?>
对此:
<h1><?php echo $dish["title"]; ?></h1>
<small><?php echo $dish["utime"]; ?></small>
<?php echo $dish["content"]; ?>
此外,
您的strip_bad_chars将始终返回空,因为您的参数为$item
并且您在$input
中使用preg_replace
所以改变:
function strip_bad_chars( $item ) {
$output = preg_replace( "/[^a-zA-Z0-9_-]/", "", $input );
对于这样的事情:
function strip_bad_chars( $input ) {
$output = preg_replace( "/[^a-zA-Z0-9_-]/", "", $input );
在使用其值之前,请务必检查是否已设置$dish
。并在$menuItems
中引用您的密钥以避免通知
您将所点击的项目指定给$dish
,但您没有使用它,而是再次显示$menuItems
的所有值。