Jump to content

[SOLVED] PHP Array Help


wemustdesign

Recommended Posts

I am creating a cocktail database, its only for fun and to help me learn php more. I have created a favorites facility where users can 'add cocktail to favorites'. Too see the prolem I am having go to the cocktail page and add the cocktail to favorites:

 

http://www.cocktail-recipe-guide.com/index.php?section=view_cocktail&cocktail=Adonis

 

As you can see at the top 2 [x] appear, this [x] is to remove the cocktail from the favorites. I cannot figure out why 2 [x] are created when the first cocktail as added to favorites. All cocktails added after this work fine. Here is the code I wrote to create the 'add to favorites':

 

//===============================================================================
//     Favorites
//===============================================================================

//==========================
//     Add 2 Favs
//==========================
switch ($function){
case ("add_2_favorites"):
$_SESSION['favoritesSet'] = 1;
$favoritesArray = $_SESSION['favoriteArray'];
$favorites_Size = sizeof($favoritesArray);
if (is_array($_SESSION['favoriteArray'])){
$array_ex = "1";
}else{
$array_ex ="0";
}
if ($array_ex == 1){
$in_array = in_array ($cocktail_name, $_SESSION['favoriteArray']);
}
if ($in_array) {
?>
<script language="Javascript">
alert ("Already in favorites")
</script>
<?
}else{
$_SESSION['favoriteArray'][] = $_GET['cocktail'];
}
break;

//==========================
//     Remove favs
//==========================

case ("remove_favorite"):
$ai = $_GET['ai'];
unset ($_SESSION['favoriteArray'][$ai]); 
break;

//==========================
//     Remove all favs
//==========================

case ("remove_all_favorites"):
session_unset();// need to just remove variable
$_SESSION['favoritesSet'] = 0;
break;
}

echo "favoritesSet =".$_SESSION['favoritesSet']."<br>"; 
if(!$_SESSION['favoritesSet'])
{
echo "No favorites";
}else{
$function == "add_2_favorites";
if ($function == "add_2_favorites"){
$favorites_List = "$cocktail_name";
}else{
$favorites_List = "";
}
$favoritesArray = $_SESSION['favoriteArray'];
$favorites_Size = sizeof($favoritesArray);
$i=0;
while ($i <= $favorites_Size){
echo "$favoritesArray[$i] <a href=".$PHP_SELF."?function=remove_favorite&cocktail=$favoritesArray[$i]&ai=$i>[x]</a><br />
";
$i++;
}
echo "size of array: $favorites_Size<br />";

echo "<a href=".$PHP_SELF."?function=remove_all_favorites>Remove all favorites</a><br />";

echo "$favoritesArray[0]";

}

 

I would appreciate if anybody had any suggestions of why this is happening. Not too advanced at php so my code may not be the best.

Link to comment
https://forums.phpfreaks.com/topic/148926-solved-php-array-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.