0

I am trying to combine two IF statements.

the FIRST statement checks if the cart if empty.

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) 

The SECOND statement change if the user is login in or not...

 if ($_SESSION['User']['AccessLvl'] == null)

I need to check if the cart is empty and if it is I have echo a message saying. cart if empty at the same thing i need to change if the user is login and if they is i dont want to show a form which I have and if they are not, they form should show.

I tried this but didn't work

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    echo 'cart if emepy';
    if ($_SESSION['User']['AccessLvl'] == null) {           
    }
}
else{
   echo ' <form name="login" action="login.php"></form>';
}

summary The first statement checks if they is anything in cart and if they is nothing, the form should not show. IF the user is logged in the form should not show if they is anything in the cart because the form is a login in form and they is no point for the person to log in twice... The second if statement is statement that i used to check if someone is logged in or not

Sarah James
  • 421
  • 5
  • 17
  • Try using `&&` instead of `||`, see if that works for you. As in `if (!isset($_SESSION["cart_array"]) && count($_SESSION["cart_array"]) < 1) {` - you're only checking if one or the other matches the criteria. `&&` checks for both. – Funk Forty Niner Jul 28 '13 at 19:48
  • @Fred I did, it didn't work... the form still shows even when I am login, when it shouldn't... – Sarah James Jul 28 '13 at 19:50
  • the form shouldn't show when someone is login and when the cart it cart. if the cart has something instead of it the cart should show. If someone already login in and doesn't have anything in the cart and then add something. The form shouldn't show – Sarah James Jul 28 '13 at 19:52
  • Ok. Try one of the answers below then. – Funk Forty Niner Jul 28 '13 at 19:53
  • so what is supposed to happen when the user is not logged in? – omma2289 Jul 28 '13 at 20:14
  • @koala_dev if the user is not logged in the form will show so that the can log in... if u do only shopping and when u click on the cart link and you have nothing in the cart all you see if a message tell you that the cart is empty... if you have something inside and you are not `LOGGED` in you get an option below the items, telling you to log in. – Sarah James Jul 28 '13 at 20:22
  • check Perry's updated answer, I think he got it right – omma2289 Jul 28 '13 at 20:25
  • @koala_dev sadly it doesn't – Sarah James Jul 28 '13 at 20:36

3 Answers3

1

I think it needs to be something like this:

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
echo 'cart if emepy';
} else {

if (is_null($_SESSION['User']['AccessLvl']) || !isset($_SESSION['User']['AccessLvl'])) {
echo ' <form name="login" action="login.php">

</form>';

} else {

}
}

The problem was that your check for the user was in the check if the cart wat empty or not.

Perry
  • 9,736
  • 2
  • 26
  • 36
  • why are u empying the session cart? – Sarah James Jul 28 '13 at 19:57
  • Sorry mistake that is not needed, I will remove it. Did it help to resolve your problem? – Perry Jul 28 '13 at 19:58
  • sorry still nothing... when I am log out and nothing is in the cart, it shows the empty message if I have something in the cart while I am still log off the form doesn't show... – Sarah James Jul 28 '13 at 20:05
  • I changed my answer. The form had to be in the if statement not in the else. – Perry Jul 28 '13 at 20:10
  • your code doesn't work... I will explain my question again. The first statement checks if they is anything in cart and if they is nothing, the form should not show. IF the user is logged in the form should not show if they is anything in the cart because the form is a login in form and they is no point for the person to log in twice... The second if statement is statement that i used to check if someone is logged in or not – Sarah James Jul 28 '13 at 20:17
  • In summary, the form should only show if the person is not log in and when something is in the cart. If someone is logged in already the form should not show – Sarah James Jul 28 '13 at 20:18
  • Edited my answer again – Perry Jul 28 '13 at 20:22
  • when i am logged in the form doesn't show – Sarah James Jul 28 '13 at 20:27
  • That is your question right? When the person is not loggedin then login else no login form... – Perry Jul 28 '13 at 20:30
  • if i am not logged in and have something in the cart. The form doesn't show, its the same thing is it is empty. IT is might to show if i have something in the cart and I am not logged in – Sarah James Jul 28 '13 at 20:35
  • Can you do a var dump on your session and check what value is set for the `$_SESSION['User']['AccessLvl']` – Perry Jul 29 '13 at 08:33
  • that is that to check if the user is logged in or not and also what accesslvl they have in they account weather they are admin or just standard users – Sarah James Jul 29 '13 at 08:43
  • I edited my answer this should work now – Perry Jul 29 '13 at 08:45
  • the form doesn't show... when i have something in the cart and I am not logged it – Sarah James Jul 29 '13 at 08:54
  • @SarahJames I tried the code my self and it works. Can you show me the code you are using now by updating your question? – Perry Jul 29 '13 at 09:02
1

Why you don't try to do something like this:

if (!isset($_SESSION["cart_array"]) && count($_SESSION["cart_array"]) < 1 && $_SESSION['User']['AccessLvl'] != null)
{
    echo "logged in but cart empty";
}
else if($_SESSION['User']['AccessLvl'] == null)
{
   echo "not logged in";
}
else
    echo "all right, logged in and cart not empty!";
Cristian Baldi
  • 371
  • 4
  • 9
1

How about this:

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)
{
    echo "cart is empty";
}
else
{
    //show cart here
    if ($_SESSION['User']['AccessLvl'] == null) //user is not logged in
    {
        //show login form
        echo ' <form name="login" action="login.php"></form>';
    }

}
omma2289
  • 53,090
  • 8
  • 62
  • 66
  • thanks for trying koala... i dont think anyone understand, what I mean – Sarah James Jul 28 '13 at 21:00
  • are you sure your login check is correct? So you have 4 cases: #1 cart is empty and user logged in, #2 cart is empty user NOT logged in, #3 cart has items and user is logged in, #4 cart has items and user is not logged in. So for #1 we show 'cart is empty', for #2 we show "empty" and form, #3 we show cart, #4 we show cart and form. That's how my answer works – omma2289 Jul 28 '13 at 21:05
  • i notice thats how it works but i didn't think you understood my question but doesn't matter, cris answered it. Thanks for your help and time :) x – Sarah James Jul 28 '13 at 21:11