I want to write a code that will view all the records within a table and assign a checkbox for each record.. the check box will be used for deleting the records just like in emails. can anyone help me on this?

Recommended Answers

All 11 Replies

hi
try this example:
suppose you want following to be done:
[]record1
[]record2
here [] is check box
then do following to get above:
1.suppose you have two variable $rec1 and $rec2 which contains data to be printed along with check box,
$rec1="record1" ;
$rec2="record2";
2.prepare a $rspString like following:

$rspString="<form>"+$rec1+"<input type='checkbox' name='some_name' value="+$rec1+">
<br>"+$rec2+"<input type='checkbox' name='some_name2' value="+$rec2+">

</form>";
print this.
it will create check box with required variable value.
this is just a example. you can follow this example to generate required string for you.

just assign a checkbox with a different name to each one with id value as the checkbox value. then use the foreach() statement to loop through the post results and regex the key to make sure it belongs to that set of input elements. after that take the value from the foreach statement and use it in a query to delete the record.

i know that sounds complicated and you probably don't understand it. if you need some example code let me know.

Its like in group of records where they all have a checkboxex assigned to them and you can check it to do something to it.. i hope you could give some simple example on this.. thanks

Btw this is how i do it right now. im just using a text link to delete the records.

$action=$_REQUEST['action'];
$id=$_REQUEST['id'];

if ($action==1){

$link=mysql_connect($hostname,$username,$password);
$dbname=mysql_select_db($database);

echo "<table width=100% cellspacing=0>";
echo "<tr><td bgcolor=darkblue><center><font face=verdana size=4 color=white>GV Local Site using PHP and MySQL</font></tr>";

$query  = "SELECT * FROM employee";

echo "<table width=100% cellspacing=0 border=1><tr><td><center><B>ID<td><center><B>First<td><center><B>Last Name<td><center><B>Address<td><center><B>Delete Entry</tr>";

$result = mysql_query($query) or die ("Query failed: " . mysql_error() . "Actual query: " . $query);

//$result = mysql_query("select * from mytable");
//while ($row = mysql_fetch_object($result)) {

while ($row = mysql_fetch_object($result)){
    echo "<tr><td>".$row->id;
    echo "<td>".$row->first;
    echo "<td>".$row->last;
    echo "<td>".$row->address;
    echo "<td><a href='ayan.php?action=2&id=$row->id'>Delete Record</a></tr>";
}
echo "</table><br><br><center><font face=arial size=4 color=white><b><a href='index.php'>Go back to Main</a></b></font>";  
}

i am working on the code, i will post soon.

here is the code. i am not for sure if it will work properly with your setup because i don't know the rest of the code, but i have used a similar system in many of my admin centers i have created and it works nicely.

<?php

$action = $_GET['action'];
if (!empty($action)) {
	$con    = mysql_connect($hostname,$username,$password);
	$db_con = mysql_select_db($database);
	switch($action) {
		case 1:
			$sql = "SELECT * FROM `employee`";
			$query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
			$tr_data = '';
			$i = 0;
			while ($row = mysql_fetch_assoc($query)) {
				$tr_data .= '<tr>';
				$tr_data .= '<td>' . $row['id'] . '</td>';
				$tr_data .= '<td>' . $row['first'] . '</td>';
				$tr_data .= '<td>' . $row['last'] . '</td>';
				$tr_data .= '<td>' . $row['address'] . '</td>';
				$tr_data .= '<td><input type="checkbox" name="delete' . $i . '" value="' . $row['id'] . '" /></td>';
				$tr_data .= '</tr>';
			$i++;
			}
$html =<<<HTML
<table width="100%" cellspacing="0">
	<tr>
		<td bgcolor="#0000FF" align="center">
			<font face="verdana" size="4" color="#FFFFFF">GV Local Site using PHP and MySQL</font>
		</td>
	</tr>
</table>
<form action="ayan.php?action=2" method="post">
<table width="100%" cellspacing="0" cellpadding="3" border="1">
	<tr>
		<th>ID</th>
		<th>First</th>
		<th>Last Name</th>
		<th>Address</th>
		<th>Delete</th>
	</tr>
	$tr_data
</table>
</form>
<br />
<br />
<center>
<font face="arial" size="4" color="#FFFFFF"><strong><a href="index.php">Go back to Main</a></strong></font>
</center>
HTML;
		break;
		case 2:
			foreach($_POST as $key => $value) {
				if (preg_match("/^delete+[0-9]$/",$key)) {
					$sql = "DELETE FROM `employee` WHERE `id` = '" . mysql_real_escape_string($value) . "'";
					$query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
				}
			}
			$html = 'Employees Deleted Successfully';
		break;
	}
}

echo $html;

?>

let me know if there are any problems. i will help you fix them.

Can u explain how the $tr_data and the for each block works?

what i did with the $tr_data is store the html into a variable instead of echoing like you had. this makes it so i can use the header() function to redirect, sorry its a force of habit. i can change if you want. the foreach() block starts off by reading the post data sent from the form where you checked the checkboxes. "foreach" of the array values it checks if the key (name element in html) is valid. if it is then it deletes the employee otherwise it skips it.

clear enough?

$tr_data = '';

don't seem to get it still coz i'm used to having echo statements on using html codes. is it because you started it with the php script first?

and also this one:

foreach($_POST as $key => $value)

for each post variable?

$tr_data = ''; is where i declared and clear the variable.

on the foreach() stuff:

foreach($_POST as $key => $value) pretty much means that it reads each element in the array and seperates the 'key' into the $key variable and seperates the 'value' into the $value variable. $_POST is an array of values submitted from a form. the foreach statement just runs through the $_POST array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.