我有一段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>";
我知道你没有提到Javascript作为一个选项,但如果它只在前端,我会使用jQuery如下:
:
<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给了你如何在客户端删除的建议。但如果你想在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>";