1

我有一段 PHP 代码生成一组搜索结果(来自 Facebook API 搜索)。

是否可以包含一个超链接/按钮,单击该超链接/按钮会从数组中删除该项目并刷新以显示新数组?

我收集到我将使用 unset() 删除该项目。

这是我的代码:

foreach ($search['data'] as $key => $list) {
   echo "<li><dt>Name:</dt>" . "<dd>" . $list['name'] . "</dd>\n";

   $gender = $facebook->api('/'.$list['id']);
   echo "<dt>Gender:</dt>" . "<dd>".ucfirst($gender['gender'])."</dd>\n";
   echo "   <a href='fb2.php?fbid=" .$list['id']. "'><img src='https://graph.facebook.com/".$list['id']."/picture?type=normal' /></a>\n";
   echo "<a href='remove.php?id= ????? ";
   echo "</li>";
    } 
    echo"</ol>"; 
4

2 回答 2

2

我知道你没有提到 Javascript 作为一个选项,但如果它只在前端,我会使用 jQuery,如下所示:

在 HTML 文件中:

<script url="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>

<ol id="facebook-tags">

<?php
    foreach ($search['data'] as $key => $list) {
        echo "<li><dt>Name:</dt>" . "<dd>" . $list['name'] . "</dd>\n";
        $gender = $facebook->api('/'.$list['id']);
        echo "<dt>Gender:</dt>" . "<dd>".ucfirst($gender['gender'])."</dd>\n";
        echo "   <a href='fb2.php?fbid=" .$list['id']. "'><img src='https://graph.facebook.com/".$list['id']."/picture?type=normal' /></a>\n";
        echo "<a class="remove">Remove</a>";
        echo "</li>";
    } 
?>

</ol>

<script>
    $('#facebook-tags').delegate('a.remove', 'click', function() {
        $(this).closest('li').remove();
    });
</script>
于 2013-03-24T19:45:56.883 回答
2

chrislondon 为您提供了如何在客户端删除的建议。但是如果你想在 PHP 端删除一些,可以这样做:

foreach ($search[ 'data' ] as $key => $list ) {
    if( $_GET[ 'id' ] == $key ){
//use unset only if you store $search[ 'data' ] in session or some, to remove it totally from results
//        unset( $search[ 'data' ][ $key ] );
        continue;
    }

    echo "<li><dt>Name:</dt>" . "<dd>" . $list[ 'name' ] . "</dd>\n";

    $gender = $facebook -> api( '/' . $list[ 'id' ] );
    echo "<dt>Gender:</dt>" . "<dd>" . ucfirst( $gender[ 'gender' ] ) . "</dd>\n";
    echo "<a href='fb2.php?fbid=" . $list[ 'id' ] . "'>
        <img src='https://graph.facebook.com/" . $list[ 'id' ] . "/picture?type=normal' /></a>\n";
    echo "<a href='remove.php?id=$key ";
    echo "</li>";
}
echo"</ol>";
于 2013-03-24T19:55:00.180 回答