SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Pass multiple ids from product page to cart page

    hi all

    at present i am adding single product to cart via href

    PHP Code:
    <?
    <a href="cart.php?product_id=1"><img src="addtocart.gif"></a>
    ?>
    now i want to add "product 1" + "related_products with checkboxes" if their checkboxes are checked.

    PHP Code:
    <input type="checkbox" value="2" name="related[]" />
    <
    input type="checkbox" value="3" name="related[]" />
    <
    input type="submit" value="addtocart"
    i can get checkboxes values in array()
    PHP Code:
    <?
    $related_all 
    = array();
    $related_all $_REQUEST['related'];
    ?>
    how will i pass all these 3 values to cart.php and what will my below insert code be changed to

    PHP Code:
    <?
    if(isset($_REQUEST['product_id']))
    {
        
    $qry "Insert into cart_table"
    }
    ?>
    vineet

  2. #2
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi tried this but it outputs id as Array

    PHP Code:
    $related_all = array();
    $related_all $_REQUEST['related'];

    foreach(
    $_REQUEST['related'] as $product_id)
    {


        if(isset(
    $_REQUEST['related']))
        {
        
    $id=$_REQUEST['related'];

        echo 
    $id/* ouput as ArrayArrayArray */
        
        
    $qry "select * from product_table where product_id = $id";
        
    echo 
    $qry/* output = where product_id = Array */

        
    }
    }
    ?> 
    vineet

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    $_REQUEST['related'] is not a simple single-dimensional array of values as you might think. Put this into your php code:
    PHP Code:
    print_r($_REQUEST['related']); 
    then check some checkboxes and test it. View page source and it will become obvious to you what needs to be corrected.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ok i got it working

    but dont know why this error is being displayed

    Code:
    Warning: Invalid argument supplied for foreach() in F:\xampp\htdocs
    vineet

  5. #5
    SitePoint Member
    Join Date
    Sep 2008
    Location
    Alpharetta, GA USA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinpkl View Post
    ok i got it working

    but dont know why this error is being displayed

    Code:
    Warning: Invalid argument supplied for foreach() in F:\xampp\htdocs
    vineet
    You many need to move your...
    Code:
    if ( isset($_REQUEST['related']) )
    ... ABOVE your foreach. If no boxes were checked, the "foreach" will be trying to work on something that isn't set - and thus isn't an array.

    I think that's right. HTH.

    Jeff Cohan


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •