Page 1 of 1

Dynamic html rows add and delete rows + trouble

Posted: Fri Mar 18, 2005 12:52 am
by harsha
Hi
all

i am trying to generate dynamic table rows containing some form elements and each row has a

checkbox to select and if I click delete button all the selected rows should be deleted

I am able to achieve it, the rows are deleteting but not properly, atleast one extra row

remains even it is checked and subjected for deletion, please help me to overcome the problem.

i have pasted the relevent part of the code below


file: libwizard.inc.php

Code: Select all

<?
function deleteRow($row_arrey="")
{
  	$new_table_rows = array();
	foreach($row_arrey as $key => $value) 
	{
		foreach($_POST['Delete'] as $delvalue) 
		{
			if($delvalue == $key) 
			{ 
				$match = true;
				break;
			}
			else { $match = false; }
		}
	  if($match == true) { ; }
	  else { array_push($new_table_rows, $value); }
	}
	return $new_table_rows;
}



function addRows()
  {
	$rows_in_table = array();
	if(isset($_POST['inColName']) AND is_array($_POST['inColName'])) {
		$num_rows = count($_POST['inColName']);
	} 
	if(isset($_POST['AddCol'])) {
		$num_rows += 4;
	}
	for($i=1; $i < $num_rows; $i++)
	  {
		$table_row = "<tr valign=\"top\" ><td align=\"center\"><input type=\"text\" 

name=\"inColName[]\" id=\"col{$i}\" value=\"";
		if(isset($_POST['inColName'][$i])) {
			$table_row .= $_POST['inColName'][$i];
		}
		$table_row .= "\"></td>\n<td align=\"center\"><select 

name=\"selColGroup[]\"><option value=\"0\">Select Group</option>";
		for($j=1; $j<=10; $j++) { 
			$table_row .= "<option value=\"{$j}\"";
			if(isset($_POST['selColGroup'][$i])) {
				if($_POST['selColGroup'][$i] == $j) {
					$table_row .= "selected";
				}
			}
			$table_row .= ">Group {$j}</option>"; 
		}
		$table_row .= "</select></td>\n<td align=\"center\"><select 

name=\"selColReplicate[]\"><option>Select Replicate</option>";
		for($j=1; $j<=10; $j++) { 
			$table_row .= "<option value=\"{$j}\"";
			if(isset($_POST['selColReplicate'][$i])) {
				if($_POST['selColReplicate'][$i] == $j) {
					$table_row .= "selected";
				}
			}		
			$table_row .= ">Replicate {$j}</option>"; 
		}		
		$table_row .= "</select></td>\n<td align=\"center\"><input type=\"text\" 

name=\"inColDesc[]\" value=\"";
		if(isset($_POST['inColDesc'][$i])) {
			$table_row .= $_POST['inColDesc'][$i];
		}
		$table_row .= "\" size=\"40\"></td><td align=\"center\">\n\n\n\n";
		$table_row .= "<input type=\"checkbox\" name=\"Delete[]\" 

value=\"{$i}\">\n\n\n\n</td>\n</tr>";
		array_push($rows_in_table, $table_row);
	  }
	  return $rows_in_table;	  
  }
?>

File: wizardscreen_nine.php

Code: Select all

<? 
if(isset($_POST['AddCol']) OR isset($_POST['toformX'])) 
	{
		$added_rows = addRows(); 
		foreach($added_rows as $value)
		  {
		  	echo $value;
		  }
	} 
else if(isset($_POST['deleteRows'])) 
   {
   	  if(isset($_POST['Delete']) AND is_array($_POST['Delete'])) {
		  foreach(deleteRow(addRows()) as $value) {
			echo $value;
		  }
		}
	  else {
	  	foreach(addRows() as $rowvalue) {
			echo $rowvalue;
		}
	  }
	}
else if(isset($_POST['attachFile']) OR isset($_POST['Delete']))
	{
		$added_rows = addRows(); 
		foreach($added_rows as $value) {
		  	echo $value;
		}
	}
?>
:(

Posted: Fri Mar 18, 2005 1:10 am
by John Cartwright
I havn't really properly read your code, I'll point you in the right direction as to what you want.

1) As you output each row, include a checkbox with a unique value -- the rows auto incremented id as an array.

Code: Select all

&lt;input type=&quote;checkbox&quote; name=&quote;deleted&#1111;]&quote; value=15&gt;
(note: 15 being row id)

2) get all deleted values, build sql statement and execute the query

Code: Select all

$count = count($_POST['deleted']);

$sql = 'DELETE FROM `table ` WHERE';

for ($i = 0; $i >= $count; $i++) 
{
     $sql .= ' `id` = '.$_POST['deleted'][$i];
     
     if ($i != $count)
     {
          $sql .= ' OR';
     }
}

//execute query
$result = mysql_query($sql) or die(mysql_error());


?>

Posted: Fri Mar 18, 2005 8:15 am
by feyd
harsha, you may want to review how labels are available to [syntax=php]and[/syntax][syntax=php]tags now..[/syntax]