Php Array value shuffle

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jay_bo
Forum Newbie
Posts: 4
Joined: Wed Feb 24, 2010 11:08 am

Php Array value shuffle

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Php Array value shuffle

Post by AbraCadaver »

First, what does not working mean? Second, shuffle() shuffles an array, $prod_id is an int. Did you mean shuffle($_SESSION["product"])?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Salaria
Forum Commoner
Posts: 34
Joined: Fri Feb 13, 2009 2:50 am
Location: India
Contact:

Re: Php Array value shuffle

Post 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.
jay_bo
Forum Newbie
Posts: 4
Joined: Wed Feb 24, 2010 11:08 am

Re: Php Array value shuffle

Post 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>&pound;'.$_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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Php Array value shuffle

Post by AbraCadaver »

You need to show how you add products to the session as well.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jay_bo
Forum Newbie
Posts: 4
Joined: Wed Feb 24, 2010 11:08 am

Re: Php Array value shuffle

Post 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');
}
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Php Array value shuffle

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply