[need] array db search

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [need] array db search

    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?
    PHP Code:
    $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.
    Last edited by asifnayem; 04.07.12, 02:07.

    #2
    I suggest this,

    PHP Code:
    $myshelf explode(",",$_SESSION['cart_user'); 
    then use a for loop,

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

    // your sql goes here


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


    I'm Proud to be a Sri Lankan!

    Comment


      #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

      Comment


        #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?

        Comment


          #5
          Originally posted by asifnayem View Post
          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 Code:
          <?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); //
          ?>

          Comment


            #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:
            PHP Code:
            $_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
                
            }

            Comment


              #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...

              PHP Code:

              // 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:

              PHP Code:
              // Get cart products as comma separated list
              $products implode(','get_cart_products()); 
              Last edited by CreativityKills; 17.07.12, 10:07. Reason: Tiny code bug fix

              Comment


                #8
                Hi,
                This isn't deleting the product from the cart :/
                PHP Code:
                $q delete_cart_product($id); 
                PHP Code:
                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); 

                Comment


                  #9
                  This?

                  PHP Code:
                  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

                  Comment


                    #10
                    Originally posted by arnage View Post
                    This?

                    PHP Code:
                    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.

                    Comment

                    Working...
                    X