我是php的新手。我曾在asp.net中使用GridView控件来获取列表记录。
现在我想在php中使用一个控件来列出带有复选框选项的记录,以选择它们进行删除并启用页面。任何人都可以指导初学者如何做到这一点吗?
提前致谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
你的想法包括两件事;一个表和一个处理表中输入的php文件。
File: table.php
<form method="post" action="process.php">
<table>
<thead>
<tr>
<th>Title</th>
<th>Activate</th>
<th>Delete</th>
</tr>
</thead>
<?php foreach( $records as $record ): ?>
<tr>
<td><?php echo $record['title']; ?></td>
<td><input type="checkbox" name="activate_ids[]" value="<?php echo $record['id']; ?>" /></td>
<td><input type="checkbox" name="delete_ids[]" value="<?php echo $record['id']; ?>" /></td>
</tr>
<?php endforeach; ?>
</table>
</form>
File: process.php
<?php
//Gather the $_POST
$activate_ids = $_POST['activate_ids'];
$delete_ids = $_POST['delete_ids'];
//Let's make sure we only get ints
for($i = 0; $i < count($activate_ids); $i++)$activate_ids[$i] = intval($activated_ids[$i]);
for($i = 0; $i < count($delete_ids); $i++)$delete_ids[$i] = intval($delete_ids[$i]);
//Get the ids
$activate_ids_string = implode(',', $activate_ids);
$delete_ids_string = implode(',', $delete_ids);
//Make sure we don't have '' as a parameter for SQL
if( $activate_ids_string == '' ) $activate_ids_string = 0
if( $delete_ids_string == '' ) $delete_ids_string = 0;
//UPDATE the table
$sql_activate = 'UPDATE Records SET active=1 WHERE id IN (' . $activate_ids_string . ')';
$sql_delete = 'DELETE FROM Records WHERE id IN (' . $delete_ids_string . ')';
//Execute the queries and get the # of affected rows
$aff_rows_activate = mysql_num_rows(mysql_query($sql_activate));
$aff_rows_delete = mysql_num_rows(mysql_query($sql_delete));
//Last but not least, output how it went for both queries
if( $aff_rows_activate === 1 ) echo 'Activated one record.';
else echo 'Activated ' , $aff_rows_activate, ' records.';
if( $aff_rows_delete === 1 ) echo 'Deleted one record.';
else echo 'Deleted ' , $aff_rows_activate, ' records.';
?>
我没有机会尝试上面的代码,因此可能还会有一些语法错误 - 但是,您应该能够了解为实现结果需要做些什么你想要的。