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

, 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 ..
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 != ""){
$skusї$sk]="";
}
print "<table>";
foreach ($skus as $sk => $qt ) {
if($qt !=""){
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>";
}
}
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) {
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 ) {
print "<tr><td bgcolor=$tog>$sk</td>";
print "<td align='right' bgcolor=$tog>$qt</td>";
//print "<td><input type=name=deleteї%s] value='Del'> </td></tr>";
printf(" <td> <input type='submit' name=deleteї%s] value='Del'> </td>\n",$sk);
}
print "</table></center>";
echo "<input type=hidden name=option value=addcos>";
echo "<input type=hidden name=companyid value=$companyid>";
}
echo "</form>";
if (is_array($delete)) {
foreach ($delete as $this => $junk) {
unset ($skusї$this]);
}
}
}