0

Hello there! I just want to know how to check if session variable is array because thee code that I am trying to work is not working see below :)

 <?php
            if(is_array($_SESSION['cart'])){
                echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
                $max=count($_SESSION['cart']);
                for($i=0;$i<$max;$i++){
                    $pid=$_SESSION['cart'][$i]['productid'];
                    $q=$_SESSION['cart'][$i]['qty'];
                    $pname=get_product_name($pid);
                    if($q==0) continue;
?>
3
Contributors
12
Replies
15
Views
5 Years
Discussion Span
Last Post by diafol
Featured Replies
  • 2

    Your test should be within a loop where $i is defined: for($i=0;$i<$max;$i++) Otherwise you can not test for `$_SESSION['cart'][$i]` Read More

  • 1
    diafol 3,720   5 Years Ago

    why use js for hidden field. just use multiple submit buttons with unique name attributes Read More

  • 2

    OK, but as I stated in my previous post please send the contents of the $_SESSION before the for loop (see above). Even better if you send the contents of the $_SESSION both before and after the foor loop so we can compare. Use the following code: else if(isset($_REQUEST['command'])=='update'){ // … Read More

  • 1
    diafol 3,720   5 Years Ago

    RE: help on multiple submits... <form ...> ...other form fields... <input type="submit" name="insert" value="Update" /> <input type="submit" name="delete" value="Delete" /> <input type="submit" name="order" value="Order" /> </form> This form sent to a formhandler page, then the action is determined by the submit button used: if($_POST){ switch(true){ case isset($_POST['insert']): //do whatever break; … Read More

0

Check also whether it is not empty:

if(is_array($_SESSION['cart']) and !empty($_SESSION['cart']))

or check for elements one level below:

if(is_array($_SESSION['cart'][$i]) and !empty($_SESSION['cart'][$i]))

or check for elements two levels below:

if(isset($_SESSION['cart'][$i]['productid'])) {

    $pid=$_SESSION['cart'][$i]['productid'];

} else {

    echo 'No product ID!';
}

Edited by broj1

0

It is still not working and I have this Error. BTW thanks for replying :)

Notice: Undefined variable: i in C:\xampp\htdocs\verifycart.php on line 61

Notice: Undefined index: in C:\xampp\htdocs\verifycart.php on line 61

0

here's the whole code :)

<?php
    //error_reporting(E_ERROR | E_PARSE);
    include("db.php");
    include("functions.php");

    if(isset($_REQUEST['command'])=='delete' && $_REQUEST['pid']>0){
        remove_product($_REQUEST['pid']);
    }
    else if(isset($_REQUEST['command'])=='clear'){
        unset($_SESSION['cart']);
    }
    else if(isset($_REQUEST['command'])=='update'){
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=(isset($_SESSION['cart'][$i]['productid']));
            $q=intval($_REQUEST['product'.$pid]);
            if($q>0 && $q<=999){
                $_SESSION['cart'][$i]['qty'] = $q;
            }
            else{
                $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
            }
        }
    }

?>
<html>
<title>Verify Shopping Cart</title>
<script language="javascript">

    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }

</script>
</head>
    <body bgcolor = "black" alink="#FFFF00" ><center><br/>
    <form name="form1" method="post">
    <input type="hidden" name="pid" />
    <input type="hidden" name="command"/>

        <br/>
            <b>Your Shopping Cart</b></font><br/><br/>               
            <?php
            if(is_array($_SESSION['cart'][$i]) and !empty($_SESSION['cart'][$i])){
                $max=count($_SESSION['cart']);
                for ($i=0;$i<$max;$i++){
                    $pid=$_SESSION['cart'][$i]['productid'];
                    $q=$_SESSION['cart'][$i]['qty'];
                    $pname = get_product_name($pid);
                    if ($q==0) continue;
            ?>

            <font face = "century gothic" color = "#FFFC17" size = "2"/>
            <?php echo $i+1?>: &nbsp;<?php echo $pname?><br/>
            Price:&nbsp;Php <?php echo get_price($pid)?><br/>
            Qty:&nbsp;<input type="text" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" /><br/>                   
            Amount:&nbsp;Php <?php echo get_price($pid)* $q?>&nbsp;<br/>
            <a href="javascript:del(<?php echo $pid?>)">Remove</a><br/><br/>
            <hr color="#FFFF00" size="1"/>
             <?php                   
                }
            ?>
            <font face = "century gothic" color = "#FFFC17" size = "3"/>
            <b>Cart Subtotal: Php <?php echo get_order_total()?></b><br/><br/>
            <input type="button" value="Clear Cart" onclick="clear_cart()">&nbsp;
            <input type="button" value="Update Cart" onclick="update_cart()">&nbsp;<br/>
            <input type="button" value="Place Order >>" onclick="window.location='toa.php'">

            <?php
            }
            else{
                echo "<font color='#FF0000'>There are no items in your shopping cart!</font>";
            }
        ?>
        </form>
        </font>
    </body>
</html>
0

this is the line 61 :)

if(is_array($_SESSION['cart'][$i]) and !empty($_SESSION['cart'][$i]))
2

Your test should be within a loop where $i is defined:

for($i=0;$i<$max;$i++)

Otherwise you can not test for $_SESSION['cart'][$i]

Votes + Comments
It is still not working I really dont understand it anymore :( please help me how will I do it! Thank you so much for the reply :)
0

BTW the button update cart is not working :( and the one I am trying to fix! thanks:)

1

why use js for hidden field. just use multiple submit buttons with unique name attributes

Votes + Comments
how will I do it? please help me :)
0

Can you post the contents of the $_SESSION['cart'] before updating? Put this code between lines 12 and 13:

die(print_r($_SESSION['cart'], 1));
0

Thank you, but this part of my code is not yet working. Please help me find what is wrong with it and how to correct it so my 'update cart' may work already. Please help me!!! Thanks. :(

 else if(isset($_REQUEST['command'])=='update'){
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=(isset($_SESSION['cart'][$i]['productid']));
            $q=intval($_REQUEST['product'.$pid]);
            if($q>0 && $q<=999){
                $_SESSION['cart'][$i]['qty'] = $q;
            }
            else{
                $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
            }
        }
    }
2

OK, but as I stated in my previous post please send the contents of the $_SESSION before the for loop (see above). Even better if you send the contents of the $_SESSION both before and after the foor loop so we can compare. Use the following code:

else if(isset($_REQUEST['command'])=='update'){

    // this will display the contents of the $_SESSION['cart'] before the update
    print_r($_SESSION['cart']);

    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $pid=(isset($_SESSION['cart'][$i]['productid']));
        $q=intval($_REQUEST['product'.$pid]);
        if($q>0 && $q<=999){
        $_SESSION['cart'][$i]['qty'] = $q;
    }

    // this will display the contents of the $_SESSION['cart'] after the update
    // and end the script
    die(print_r($_SESSION['cart'], 1));

    else{
        $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
    }
}
Votes + Comments
thank you so much! :)
1

RE: help on multiple submits...

<form ...>
...other form fields...
<input type="submit" name="insert" value="Update" />
<input type="submit" name="delete" value="Delete" />
<input type="submit" name="order" value="Order" />
</form>

This form sent to a formhandler page, then the action is determined by the submit button used:

if($_POST){
    switch(true){
        case isset($_POST['insert']):
            //do whatever
            break;
        case isset($_POST['update']):
            //do whatever
            break;
        case isset($_POST['order']):
            //do whatever
            break;
        default:
            //error
            break;
    }
}

Or something like that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.