I have finally got my multidimensional array going for my shopping cart.
I am having dificulty with the unset command.
currently my array churns out like this:-
Cart: Array
(
[[url]http://somedomain.com.au/albums/BrochureImages/C_A25.jpg[/url]] => Array
(
[album] => Brochure Images
[picname] => C_A25
)
[[url]http://somedomain.com.au/albums/BrochureImages/D12.sized.jpg[/url]] => Array
(
[album] => Brochure Images
[picname] => D12
)
)
but when I unset one of the arrays it does this :-
Cart: Array
(
[[url]http://somedomain.com.au/albums/BrochureImages/C_A25.jpg[/url]] => Array
(
[album] => Brochure Images
[picname] => C_A25
)
[] => Array
(
[album] =>
[picname] =>
)
)
My formdata is sent from this include code.
<form method=GET>
<input type=hidden name=album value="<?=$albumtitle?>">
<input type=hidden name=picname value=<?=$picname?>>
<input type=hidden name=cart_add value=<?=$pthimg?>>
<input type=submit value="Add <?=$picname?> to cart">
</form>
my code for the cart looks like this (album.footer):-
<?
session_register("cart");
$itemname = $GLOBALS["itemname"];
$book = $GLOBALS["album"];
$name = $GLOBALS["picname"];
$add = $GLOBALS["cart_add"];
$remove = $GLOBALS["cart_remove"];
$cart = &$GLOBALS["cart"];
if ($add) {
$cart[$add]++;
$cart[$book];
$cart[$name];
}
if ($remove) {
echo"DEBUG:removing $itemname<br>";
unset($cart[$remove]);
}
if (count($cart)) {
print "Your chosen photos: <br><table width=100% cellpadding=2 cellspacing=0 border=1>\n";
$contents = array (album => $book, picname => $name);
$cart[$add] = $contents;
foreach ($cart as $itemname => $itemamount)
{
print "<tr><td><img src=$itemname height=60 align=\"absmiddle\"></td><td>$itemname<br></td><td><form method=GET>
<input type=hidden name=cart_remove value=$itemname><input type=submit value=\"Remove from cart\"></form></td></tr>\n";
}
print "</table><form method=GET>
<input type=submit value=\"Proceed to Step 2\"></form>\n";
}
echo "<pre>Cart: "; print_r($cart); echo "</pre>";
?>
<span class="head">
<?= $album->fields["title"] ?>
</span>
<br>
<?php
include("../admin/config.inc");
$footer_query = "SELECT text_blob FROM text WHERE text_name='footer'";
$footer_result = mysql_result(mysql_query($footer_query),0);
print "$footer_result";
?>
My Problem is that I want to get rid of the empty pointer and re-index the array so it looks as if the entry never existed.
I have tried the following functions out but they dont seem to do the job...
function array_cleanse(&$arr){
$temp = array();
reset($arr);
if(count($arr) == 0) return "";
foreach($arr as $key=>$val):
(is_array($val))? array_cleanse($val) : NULL;
($val)? $temp[$key] = $val : NULL;
endforeach;
$arr = $temp;
reset($arr);
}
function array_filter_multi($input, $filter, $keepMatches) {
if (!is_array($input))
return ($input==$filter xor $keepMatches==false) ? $input : false;
while (list ($key,$value) = @each($input)){
$res = array_filter_multi($value, $filter,$keepMatches);
if ($res !== false)
$out[$key] = $res;
}
return $out;
}
function array_clean ($input, $delete, $caseSensitive = false)
{
$i = 0;
while($i < count($input))
{
if($delete)
{
if($caseSensitive)
{
if(!strstr($input[$i] ,$delete))
{
$return[] = $input[$i];
}
}
else
{
if(!stristr($input[$i], $delete))
{
$return[] = $input[$i];
}
}
}
else
{
if(!empty($input[$i]))
{
$return[] = $input[$i];
}
}
$i++;
}
return $return;
}
many thanks
Cyberfez...