We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to check if session variable is array

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
5 Days
Discussion Span
8 Months Ago
Last Updated
14
Views
weirdCreature7
Newbie Poster
16 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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!';
}
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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

weirdCreature7
Newbie Poster
16 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

whawhat's the code on line 61? show the code block before this line too.

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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>
weirdCreature7
Newbie Poster
16 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

this is the line 61 :)

if(is_array($_SESSION['cart'][$i]) and !empty($_SESSION['cart'][$i]))
weirdCreature7
Newbie Poster
16 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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]

broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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

weirdCreature7
Newbie Poster
16 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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));
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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';
            }
        }
    }
weirdCreature7
Newbie Poster
16 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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';
    }
}
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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.

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1190 seconds using 2.71MB