Page 1 of 1
Php Array value shuffle
Posted: Wed Feb 24, 2010 11:10 am
by jay_bo
I have used this code to remove a vaule in my array and shuffle the array so theres no hole. However, it doesnt work...This is my 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?
Re: Php Array value shuffle
Posted: Wed Feb 24, 2010 11:29 am
by AbraCadaver
First, what does not working mean? Second, shuffle() shuffles an array, $prod_id is an int. Did you mean shuffle($_SESSION["product"])?
Re: Php Array value shuffle
Posted: Wed Feb 24, 2010 11:32 am
by Salaria
Hi, What you are trying to do? You are using shuffle on a single value int not on array.
Please let us know your exact issue.
Re: Php Array value shuffle
Posted: Wed Feb 24, 2010 12:05 pm
by jay_bo
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
Code: Select all
$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
Code: Select all
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.
Re: Php Array value shuffle
Posted: Wed Feb 24, 2010 12:13 pm
by AbraCadaver
You need to show how you add products to the session as well.
Re: Php Array value shuffle
Posted: Wed Feb 24, 2010 12:17 pm
by jay_bo
This is how they are added to the session once the add button has been selected...
Code: Select all
if ($_GET["add"]=="true")//This line gets the value and reads it
{
$_SESSION["cost"]=($_SESSION["cost"]+$_POST["price"]*$_POST["quanity"]);
$_SESSION["products"]=$_SESSION["products"]+$_POST["quanity"];
$_SESSION["order"]=$_SESSION["order"].$_POST["id"].",";
$_SESSION["product"][$_POST["id"]]["qty"]=$_SESSION["product"][$_POST["id"]]["qty"]+$_POST["quanity"];
header ('Location: products.php?cat=1');
}
Re: Php Array value shuffle
Posted: Wed Feb 24, 2010 12:48 pm
by AbraCadaver
I don't see where you add the title of the product to the session. This is really a poor way to go about this and is hard to follow. One better way would be to have an array of the products and related stuff in the session. When you need the total quantities or costs you can calculate it. Without all the code its hard to tell. Most likely your database query isn't return all the data because one of your vars may be empty or not what you think it is.