[need] array db search

Started on the old forum in July 2012 · view the original in the archive · old links redirect here

9 replies · 622 views · started 14 years ago · 1 min read

6 participants
asifnayem Developer · 119 posts #1

Let me clear the things: I m creating a little e-commerce script for a phone selling website, when the user add a phone to his cart the phone id will saved into session, if the user adds more than a phone to his cart the session data will be 1,2,5,10. when he checks the cart the phone name and the price should show from the database. Now here is my problem. I cannot show the data from the database when more than a phone in the cart. please tell me what to do?

$myshelf = $_SESSION['cart_user'];
$query = "SELECT * FROM phone_info WHERE id IN ($myshelf)";
while ($row = mysql_fetch_array($result)) {
}

also, I need to plus the value of the price as total price. but can't make it through as these value are with while function.

nimeshka Developer · 180 posts #2

I suggest this,

$myshelf = explode(",",$_SESSION['cart_user');

then use a for loop,

for($i=0;$i<count($myshelf);$++){

// your sql goes here

}

not the exact codes, but hope you will get the idea

External image (may be gone)

I'm Proud to be a Sri Lankan!

arnage Lead · 811 posts #3

Well you can do something like nimeshka wrote, but let us know first how did you assigned $_SESSION['cart_user']? Is in it just id or something more?

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
asifnayem Developer · 119 posts #4

I don't know what was the problem when I tried my code first time. but now when I tried the same code i stated above in another page, it works perfectly. thanks for the reply both of u. I saved the data in $_SESSION[cart_user'] like 1,2,3,4,5,6.

Another problem arises, :/. that is: now the problem is how to delete one productid from the session? Like i said b4, my $_SESSION[cart_user'] saves the productid like 1,2,3,4,5,6. now how to delete any of them? If I delete 2 then it will leave the comma(,) and when there is only one product in the cart the delete process is works fine. please can you tell me how to delete one or two from this sets?

xor Junior · 13 posts #5

asifnayem wrote: I don't know what was the problem when I tried my code first time. but now when I tried the same code i stated above in another page, it works perfectly. thanks for the reply both of u. I saved the data in $_SESSION[cart_user'] like 1,2,3,4,5,6.

Another problem arises, :/. that is: now the problem is how to delete one productid from the session? Like i said b4, my $_SESSION[cart_user'] saves the productid like 1,2,3,4,5,6. now how to delete any of them? If I delete 2 then it will leave the comma(,) and when there is only one product in the cart the delete process is works fine. please can you tell me how to delete one or two from this sets?

<?php
    $ids = explode(',', $_SESSION[cart_user']);

    //$ids is now an array with all ids, delete the one u desire then

   $_SESSION[cart_user'] = implode(',', $ids); //
?>
something else Fellow · 2,296 posts #6

I would have used an array in the cart_user session and then you wouldn't have had to worry about comma`s eg:

$_SESSION['cart_user'] [0];
$_SESSION['cart_user'] [1];
//etc

/// then use a foreach statement 
foreach ($_SESSION['cart_user'] as $item) {
    if($item!='')
    {
        print $item; // or do what ever mysql call you want here
    }
}
CreativityKills Lead · 833 posts #7

You can always store them as array, if youre so particular as storing them as strings in session then use the explode method above. I would do this though...

// Saves a new product to cart
function save_cart_product($id)
{
    // Get the existing products
    $products = isset($_SESSION['cart_product_ids']) ? $_SESSION['cart_product_ids'] : NULL;

    if (empty($products))
    {
        // Create a new products array and save as serialized string
        return $_SESSION['cart_product_ids'] = serialize(array( (int) $id));
    }

    if ( ! is_cart_product($id))
    {
        // Convert serialized string to array
        $products = unserialize($products);

        // Put new id into array
        $products[] = (int) $id;

        // Serialize and resave it
        return $_SESSION['cart_product_ids'] = serialize($products);
    }

    return FALSE;
}

// Gets cart products
function get_cart_products()
{
    // Cart products saved in session
    $products = isset($_SESSION['cart_product_ids']) ? $_SESSION['cart_product_ids'] : NULL;

    // Cart is empty
    if (empty($products))
        return array();

    // Unserialize existing cart data and type cast as array
    return (array) unserialize($products);
}

// Remove a cart product
function delete_cart_product($id)
{
    // Is not a valid cart product
    if ( ! is_cart_product($id))
         return TRUE;

    // Get cart products
    $products = get_cart_products();

    // Find the array key of the product
    $product_key = array_search($id, $products);

    // Remove the key
    unset($products[$product_key]);

   // Save the remaining values
   return $_SESSION['cart_product_ids'] = serialize($products);
}

// Check if a product is already in the cart
function is_cart_product($id)
{
    // Get cart products
    $products = get_cart_products();

    // Check if exists and return true or false
    return (bool) in_array($products, $id);
}

Use the functions to neatly get and set the cart values. If you want to get them as a comma seperated list you would probably do something like this:

// Get cart products as comma separated list
$products = implode(',', get_cart_products());
asifnayem Developer · 119 posts #8

Hi, This isn't deleting the product from the cart :/

$q = delete_cart_product($id);
function delete_cart_product($id) 
{ 
    // Is not a valid cart product 
    if ( ! is_cart_product($id)) 
         return TRUE; 

    // Get cart products 
    $products = get_cart_products(); 

    // Find the array key of the product 
    $product_key = array_search($id, $products); 

    // Remove the key 
    unset($products[$product_key]); 

   // Save the remaining values 
   return $_SESSION['cart_product_ids'] = serialize($products); 
} 
arnage Lead · 811 posts #9

This?

function delete_cart_product($id)  
{  
    // Is not a valid cart product  
    if ( ! is_cart_product($id))  
         return TRUE;  

    // Get cart products  
    $products = get_cart_products();  

    // Find the array key of the product  
    $product_key = array_search($id, $products);  

    // Remove the key  
    unset($products[$product_key]);  

    $newproducts = array_values($products);

   // Save the remaining values  
   return $_SESSION['cart_product_ids'] = serialize($newproducts);  
} 
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
asifnayem Developer · 119 posts #10

arnage wrote: This?

function delete_cart_product($id)  
{  
    // Is not a valid cart product  
    if ( ! is_cart_product($id))  
         return TRUE;  

    // Get cart products  
    $products = get_cart_products();  

    // Find the array key of the product  
    $product_key = array_search($id, $products);  

    // Remove the key  
    unset($products[$product_key]);  

    $newproducts = array_values($products);

   // Save the remaining values  
   return $_SESSION['cart_product_ids'] = serialize($newproducts);  
} 

I don't know why this also not working. though I already solved this by making some modification in is_cart_product(). Thanks for reply.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close