简体   繁体   English

从数组中删除搜索结果

[英]Removing a search result from an array

I have a piece of PHP code which produces an array of search results (from a Facebook API search). 我有一段PHP代码,它产生一系列搜索结果(来自Facebook API搜索)。

Would it be possible to include a hyperlink/button that, when clicked, removed that item from the array and refreshed to display the new array? 是否可以包含一个超链接/按钮,在单击时,从阵列中删除该项并刷新以显示新数组?

I gather that I would be using unset() to remove the item. 我认为我将使用unset()删除该项目。

Here is the code I have: 这是我的代码:

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

I know you don't mention Javascript as an option but if it's only on the front end I would use jQuery as follows: 我知道你没有提到Javascript作为一个选项,但如果它只在前端我会使用jQuery如下:

in HTML file: 在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>

chrislondon was give you advice how to remove at at client-side. chrislondon给你建议如何在客户端删除。 But if you want to delete some at PHP side, it can be done some like this: 但是如果你想在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>";

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

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