Page 1 of 1

Deleting From Arrays

Posted: Fri Jun 27, 2003 8:26 am
by massiveone
Here is my problem if anyone can help :lol: , its more of a structual issue
heres what i got. I am trying to think how to use the unset command to destroy a line in my array. but write the code so that it works with the button... any ideas anyone? thanks in advance.

print "<table>";
foreach ($skus as $sk => $qt ) {
print "<tr><td bgcolor=$tog>$sk</td>";
print "<td align='right' bgcolor=$tog>$qt</td>";
print "<td><input type=button value=Del </td><tr>";
}
print "</table>";

Posted: Fri Jun 27, 2003 8:34 am
by AVATAr

Posted: Fri Jun 27, 2003 8:49 am
by massiveone
the slice function is very cool thanks for the links, unset would probably work better as its less math and more direct like unset(key,value) but i dont know how to put it in a form button...

Re: Deleting From Arrays

Posted: Fri Jun 27, 2003 11:38 am
by xisle
print "<table>";
foreach ($skus as $sk => $qt ) {
print "<tr><td bgcolor=$tog>$sk</td>";
print "<td align='right' bgcolor=$tog>$qt</td>";
print "<td><input type=button value=Del </td><tr>";
}
print "</table>";

... think unset would wipe the entire array. Others will respond if I am wrong .. :D


I would do something like this assuming you want a seperate button for each element, the array does not have to be passed every time, and register globals is active:

Code: Select all

if($del != "")&#123;
  $skus&#1111;$sk]="";
&#125;

print "<table>";
foreach ($skus as $sk => $qt ) &#123;
	if($qt !="")&#123;
	   print "<tr><td bgcolor=$tog>$sk</td>";
	   print "<td align='right' bgcolor=$tog>$qt</td>";
	   print"<form method="POST" action="$PHP_SELF">";
	   print"<input type="hidden" name="sk" value="$sk">";
	   print "<td><input type="button" name="del" value="Del">
          </td><tr>";
	   print "</form>";
	&#125;
&#125;
print "</table>";

xisle

Posted: Fri Jun 27, 2003 11:49 am
by patrikG
If you want to retain a coherent index ([0],[1],[2],[3] etc.) you should use array_splice.
If you don't, just unset($array[$key]), example:

Code: Select all

<?php
if(isset($_POST["del"]) && !empty($_POST["del"]))
   unset($skus[$_POST["del"]]);
?>

Posted: Fri Jun 27, 2003 12:53 pm
by massiveone
Cool Thanks All for your help. Here was my final solution., when i click on the del button it now removes the item from the array... the array is associative with format SKU => Quantity ... so you can read this easier.

Code: Select all

echo "<form name=fin>";
 if ($skus) &#123;
    print "<center>Items Added so far</center>";
    print "<center><table cellspacing=1 bgcolor='4682B4' ><tr><td><b>SKU</b></td><td align='right'>Quantity</td></tr>";	
	
	$tog="E0FFFF";
	foreach ($skus as $sk => $qt ) &#123;
 	
	print "<tr><td bgcolor=$tog>$sk</td>";
	print "<td align='right' bgcolor=$tog>$qt</td>";
	//print "<td><input type=name=delete&#1111;%s] value='Del'> </td></tr>";
	printf(" <td> <input type='submit' name=delete&#1111;%s] value='Del'> </td>\n",$sk);
	
	&#125;
    
	print "</table></center>";
	echo "<input type=hidden name=option value=addcos>";
    echo "<input type=hidden name=companyid value=$companyid>";
 &#125;
 echo "</form>";
 
 if (is_array($delete)) &#123;
  foreach ($delete as $this => $junk) &#123;
    unset ($skus&#1111;$this]);
  &#125;
 &#125;
 
 &#125;