jay_bo Posted February 24, 2010 Share Posted February 24, 2010 I have used this code to remove and item in my array..However, it leaves a hole in my array and i was wondering how i could shuffle the values up. $prod_id = (int) $_GET['id']; unset($_SESSION["product"][$prod_id]); Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/ Share on other sites More sharing options...
Wolphie Posted February 24, 2010 Share Posted February 24, 2010 http://php.net/manual/en/function.shuffle.php Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017501 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 I used shuffle($prod_id); but it didnt seem to work...I get an Warning: shuffle() expects parameter 1 to be array, integer given. Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017504 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 This is my whole block of code..... if (isset($_GET['id'], $_GET["delete"]) && is_numeric($_GET['id']) && $_GET['delete'] =="true")//This line gets the value and reads it { $prod_id = (int) $_GET['id']; unset($_SESSION["product"][$prod_id]); shuffle($prod_id); header ('Location: products.php?cat=1'); } Where am i going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017515 Share on other sites More sharing options...
sasa Posted February 24, 2010 Share Posted February 24, 2010 try <?php if (isset($_GET['id'], $_GET["delete"]) && is_numeric($_GET['id']) && $_GET['delete'] =="true")//This line gets the value and reads it { $prod_id = (int) $_GET['id']; unset($_SESSION["product"][$prod_id]); $_SESSION["product"] = array_values($_SESSION["product"]); header ('Location: products.php?cat=1'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017553 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 Sorry i should of explained what i am trying to do....I am trying to remove an item from my shopping cart which seems to work, but when i readd items they seem to mess up. For example; Before deletion: Quantity Product Price Quantity: 2 - Microsoft Windows 7 Professional - £319.98 Remove Quantity: 3 - Microsoft Windows Vista Ultimate - £269.97 Remove After i remove them and readd products: Quantity Product Price Quantity: 2 - Microsoft Windows 7 Professional - £319.98 Remove Quantity: 2 - - £0 Remove You see the second item i add has no title or price. This is my cart php code $cost = number_format($_SESSION["cost"],2,".",","); //Stores data into the variable echo '<form name="cart" method="post" action="shopping-cart.php" >'; echo '<p class="center"><strong>SHOPPING CART</strong></p>'; echo '<p>No of Items: <strong>'.$_SESSION['products'].'</strong><br/>Total Price: <strong>£'.$_SESSION['cost'].'</strong></p>'; //Variables displays the data echo '<input type="hidden" name="price" value="'.$row["price"].'">'; echo '<p class="button"><input type="submit" name="Submit" value="Check Out"></p>'; echo '</form>'; echo '<form name="cart" method="post" action="logout.php" >'; echo '<p class="button"><input type="submit" name="Submit" value="Logout"></p>'; echo '</form>'; This is My shopping cart deletion code if (isset($_GET['id'], $_GET["delete"]) && is_numeric($_GET['id']) && $_GET['delete'] =="true")//This line gets the value and reads it { $prod_id = (int) $_GET['id']; unset($_SESSION["product"][$prod_id]); $_SESSION["product"] = array_values($_SESSION["product"]); header ('Location: products.php?cat=1'); } Many thanks guys for your help so far. Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017561 Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 Do a print_r and show the array before you delete anything. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017584 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 Array ( [6] => Array ( [title] => Microsoft Windows 7 Professional [description] => Windows 7 Professional includes all the Home Premium features you love and the business features your work demands. Connect to company networks easily and more securely and share files across the various PCs in your home. [price] => 159.99 [qty] => 2 ) [7] => Array ( [title] => Microsoft Windows Vista Ultimate [description] => Windows 7 Professional includes all the Home Premium features you love and the business features your work demands. Connect to company networks easily and more securely and share files across the various PCs in your home. [price] => 89.99 [qty] => 3 ) [8] => Array ( [title] => Microsoft Windows Vista Home [description] => Windows 7 Professional includes all the Home Premium features you love and the business features your work demands. Connect to company networks easily and more securely and share files across the various PCs in your home. [price] => 69.99 [qty] => 0 ) [28] => Array ( [title] => Nero 8 [description] => fdfdsfds [price] => 2.99 [qty] => 0 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017588 Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 Array ( [6] => Array ( [title] => Microsoft Windows 7 Professional [description] => Windows 7 Professional includes all the Home Premium features you love and the business features your work demands. Connect to company networks easily and more securely and share files across the various PCs in your home. [price] => 159.99 [qty] => 2 ) Whatever sets the [6], be it product_id or whatever, thats what you want to unset unset($array[6])//unset($array[$procuct_id]) HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017598 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 Yea product_id is set in my mysql table, so how would i go about this? Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017603 Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 find this: $prod_id = (int) $_GET['id']; unset($_SESSION["product"][$prod_id]); try this: $prod_id = (int) $_GET['id']; unset($_SESSION[$prod_id]); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017608 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 Unfortuantly it hasn't worked $prod_id = (int) $_GET['id']; unset($_SESSION[$prod_id]); $_SESSION["product"] = array_values($_SESSION["product"]); <<<<Would i need to change this? Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017611 Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 Why not try eliminating that. Why are you even doing that? You have a functioning array. you unset the sub_array that has the complete product in it. Why take the values and redo the array. Makes no sense to me. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017616 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 Oh i forgot to remove that from something i was trying before..this is my code now which removes the products... $prod_id = (int) $_GET['id']; unset($_SESSION['product'][$prod_id]); header ('Location: products.php?cat=1'); At the moment i have one item in my shopping cart which has a quantity of two...The item is bold below but ive noticed that there are other items there already which dont have a quantity, should they actually be there? Array ( [6] => Array ( [title] => Microsoft Windows 7 Professional [description] => Windows 7 Professional includes all the Home Premium features you love and the business features your work demands. Connect to company networks easily and more securely and share files across the various PCs in your home. [price] => 159.99 [qty] => 2 ) [7] => Array ( [title] => Microsoft Windows Vista Ultimate [description] => Windows 7 Professional includes all the Home Premium features you love and the business features your work demands. Connect to company networks easily and more securely and share files across the various PCs in your home. [price] => 89.99 [qty] => 0 ) [8] => Array ( [title] => Microsoft Windows Vista Home [description] => Windows 7 Professional includes all the Home Premium features you love and the business features your work demands. Connect to company networks easily and more securely and share files across the various PCs in your home. [price] => 69.99 [qty] => 0 ) [28] => Array ( [title] => Nero 8 [description] => fdfdsfds [price] => 2.99 [qty] => 0 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017617 Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 Are you building the card yourself or just altering an already existing cart? Seems to me that a QTY of zero should never make it into the DB. Shows a lack of error checking between form>process>DB. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017627 Share on other sites More sharing options...
jay_bo Posted February 24, 2010 Author Share Posted February 24, 2010 Yea i have made it myself..The products with 0 quanitity cant actually be added to the cart as i have done an if statement that if quantity equals 0 then it can't show the add button. Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017631 Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 You say that they cant be put into the cart yet in #13 you tell me there are products with a qty of zero. You have products with a qty of zero sneaking in or you dont. Which is it? HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193235-php-array-value-shuffle/#findComment-1017664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.