Jump to content

[SOLVED] Outputting an array


wemustdesign

Recommended Posts

I have looked on the Internet but cannot find an array example that suits my needs. The array that I am outputting in a 'add to favorites' similar to a shopping cart. I am currently outputting the array using the code below. This output ok but doesn't work when removing the items from the favorites via a query string. What I need is something that will output the array and the array key within a query string like '&remove_from_favotites&id=$ARRAY_KEY"

 

$i=0;
while ($i < $favorites_Size){
echo $_SESSION['favoriteArray'][$i]." 
<a href=$url&function=remove_favorite&cocktail=$favoritesArray[$i]&ai=$i>[x]</a><br />";
$i++;
}

Link to comment
Share on other sites

try

$_SESSION['favoriteArray']= array_values($_SESSION['favoriteArray']);
$i=0;
while ($i < $favorites_Size){
echo $_SESSION['favoriteArray'][$i]." 
<a href=$url&function=remove_favorite&cocktail=$favoritesArray[$i]&ai=$i>[x]</a><br />";
$i++;
}

Link to comment
Share on other sites

No this does not work. I thik I can see where the problem is. When I output the array it outputs like below with the querystring matching the array key

 

(Array key [0])Cocktail     remove=0
(Array key [1])Cocktail1   remove=1
(Array key [2])Cocktail2   remove=2

 

If I delete Cocktail1 from the favorites it then outputs like this:

 

(Array key [0])Cocktail     remove=0
(Array key [2])Cocktail2   remove=1

 

This is because the querystring is generated by $i. I need the querystring to output the same value as the arraykey if you know what I men

 

Link to comment
Share on other sites

Sorry if I've misunderstood what you want to do, but this should do the trick.

 

$theArray[0] = "Cocktail";
$theArray[1] = "Cocktail1";
$theArray[2] = "Cocktail2";

foreach( $theArray as $arrayKey => $arrayValue )
	echo $arrayValue." <a href=$url&function=remove_favorite&cocktail=" . $arrayValue . "&ai=" . $arrayKey . ">[x]</a><br />";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.