繁体   English   中英

PHP数组的我无法创建动态页面

[英]PHP array's I can't create a dynamic page

我一直在使用PHP数组创建动态页面,但是当我单击显示要单击的数组的链接时,它会显示所有数组,这是我创建的示例,向您展示了http://arqetech.net/arrayTest /

这是我使用的代码:

  1. 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> 

  2. 我将数组放在其中的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>"

    ),

);

?>`

  1. 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]; } 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]; } ?>

     <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> 

但是仍然没有回显我单击的数组链接,请帮助我()。 谢谢你!

在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并且您在preg_replace中使用$input

所以改变:

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所有值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM