我有这个 find_all() 函数,它写在一个单独的文件中:
public static function find_all() {
return self::find_by_sql("SELECT * FROM ".self::$table_name);
}
它在包含我的 foreach 循环的文件顶部被引用:
<?php require_once("../../includes/initialize.php"); ?>
<?php if (!$session->is_logged_in()) { redirect_to("login.php"); } ?>
<?php
$parents = UserParent::find_all();
?>
这是 foreach 循环:
<?php foreach($parents as $parent): ?>
<div class='popup-screen' id = "popup">
<div class = "spacing">
Do you want to delete this data?
</div>
<a href="list_users.php?parentNum=<?php echo $parent->parentNum; ?>"> <input type="button" value="YES" class = "popup-button"> </a>
<input type="button" value="CANCEL" class = "popup-button" onClick = "hide();">
</div>
<tr class = "tr-1">
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><img src="../<?php echo $parent->image_path(); ?>" width="100" height = "100" class = "profile-pic"/></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';">Parent</td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo $parent->username; ?></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo ucwords($parent->firstName); ?></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo ucwords($parent->lastName); ?></td>
<td onClick = "show();"><img src = "../stylesheets/images2/delete-icon.png" height="25" width="25" ></td>
</tr>
<?php endforeach; ?>
这是javascript代码:
function show()
{
document.getElementById("popup").style.display='block';
}
function hide(){
document.getElementById("popup").style.display='none';
}
基本上,我的代码所做的是创建从 foreach 循环中获取数据的信息行。每行的末尾是一个删除图标,如 img 标签所示。单击删除图标后,show() 函数将运行(show() 函数仅显示弹出 div,它是不可见的)——确认用户是否要删除他/她的数据。如果用户单击取消,窗口将关闭,如 javascript 代码所示。如果用户单击“是”,则应该转到链接:list_users.php?parentNum=parentNum; ?> (每行的 $parent->parentNum 的值不同)。但是,锚标签总是检索第一行的链接,无论它是第三行还是其他任何东西(顺便说一下,其他 td 标签上的链接都有效)。现在,我的问题是,