0

Sorry I couldn't find a better topic for this topic.

I have a cart page and I am changing for two things... The code I am about to show you works but I want to combine them.

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

The code above check if the cart is empty

if ($_SESSION['User']['AccessLvl'] == null) { *this code check if the user is logged in or not.*

My question I have a form and I want to show this form on the page only when the cart is not empty. If is the cart is empty the form shouldn't show. also if the user is already sign in, the form shouldn't show.

echo ' <form name="login" action="login.php">

</form>';

I tired this

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

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

</form>';

} else {

}
}

Summary SHOULD ONLY, if user is NOT logged in The form should only show when they is something(item) in the cart and if the user is NOT logged it.

SHOULD NOT If the user is logged in already, the form should not show because they is no point asking someone to log in twice.

Can someone help me please. Thanks

Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
Sarah James
  • 431
  • 5
  • 19
  • You shouldn't ask a new question, you should update your [last one](http://stackoverflow.com/q/17912066/2049063), once again I believe you can be more clear on your description, from what you wrote here I gather you just need to show the form when the user is not logged in and the cart is not empty, that should be simple enough – omma2289 Jul 29 '13 at 08:42
  • @koala_dev "simple enough" sounds easy but its not so easy for me. Why don't you post your easy answer please – Sarah James Jul 29 '13 at 08:51
  • Before I do, I need to be sure that that's in fact what you need. So the only condition when the form should display is when there's items in the cart and the user is not logged in? – omma2289 Jul 29 '13 at 08:52
  • @koala_dev yes that is one of the conditions. The form shouldn't show if the user is logged in already and also if the logged in user cart is empty. – Sarah James Jul 29 '13 at 08:56

3 Answers3

3

This should do the trick:

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

The following is just the explanation of the if-condition. This is NO valid syntax:

If the card is not empty > !(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) AND the user is not logged in > $_SESSION['User']['AccessLvl'] == null it shows the form.

Tobias Golbs
  • 4,586
  • 3
  • 28
  • 49
  • this doesn't work. even when I have something in the cart and I am `NOT` logged in, the form is might to show. – Sarah James Jul 29 '13 at 08:37
  • @Sarah actually the form won't show whenever the user is logged in as you asked – omma2289 Jul 29 '13 at 08:40
  • I changed the code. So when the cart is empty or the user is not logged in, it shows the form. @koala_dev: When the user is logged in, the form should not show at all. That is the desired result she asked for. – Tobias Golbs Jul 29 '13 at 08:43
  • but it should show when the user is NOT logged in and the cart is not empty – Sarah James Jul 29 '13 at 08:48
  • @SarahJames: I changed my code again back to the first suggestion. If the card is NOT empty AND the user is NOT logged in it shows the form. If the user is logged in or the card is empty the form will not be displayed. – Tobias Golbs Jul 29 '13 at 09:02
  • the first or second code? if its the second one please arrange it – Sarah James Jul 29 '13 at 09:06
  • @SarahJames: What do you mean? THe code example in my answer. The part after that is just an explanation. – Tobias Golbs Jul 29 '13 at 09:08
  • "`If the card is not empty > !(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) AND the user is not logged in > $_SESSION['User']['AccessLvl'] == null it shows the form.`" what is this? – Sarah James Jul 29 '13 at 09:13
  • Like i said this is just the previous code example explained a little bit more. This is **NO** valid syntax so use the first one. – Tobias Golbs Jul 29 '13 at 09:18
2

The first condition:

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

Can be shortened to simply this:

if (!empty($_SESSION['cart_array']))

Because count() only returns a value less than 1 if the array is empty. Furthermore, empty() also behaves like isset() internally.

Then, you can simply add the other condition like this:

if (!empty($_SESSION['cart_array']) && !isset($_SESSION['User']['AccessLvl'])) {
    // show form
}

The isset() doesn't raise a notice when trying to access $_SESSION['User']['AccessLvl'] if it doesn't exist.

Update

To make this whole thing easier to maintain you could introduce some helper functions, e.g.:

function isCartEmpty()
{
    return empty($_SESSION['cart_array']);
}

function isLoggedIn()
{
    return isset($_SESSION['User']['AccessLvl']);
}

if (!isCartEmpty() && !isLoggedIn()) {
    // show form
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • @SarahJames I've updated the answer to make the code more readable (and maintainable) :) – Ja͢ck Jul 29 '13 at 09:28
  • thanks jack, I will use this but i cant give u the answer because someone else already answered it but i will vote for it. :D thanks again xxx – Sarah James Jul 29 '13 at 09:43
  • @SarahJames Not saying that you should, but you can change the accepted answer actually :) – Ja͢ck Jul 29 '13 at 09:43
  • lol, you can post this answer here [link](http://stackoverflow.com/questions/17912066/checking-if-login-it-and-cart-is-empty?lq=1) i will vote for you :D – Sarah James Jul 29 '13 at 09:45
  • @SarahJames Actually, since that question is almost the same as this you should delete the older question. – Ja͢ck Jul 29 '13 at 09:50
  • I have another question which needs an answer. I know you will be able to answer it, and example it, do you have time or should i post it later? – Sarah James Jul 29 '13 at 09:50
  • please [the question i was talking about](http://stackoverflow.com/questions/17921321/how-to-encrypt-product-id-in-url) – Sarah James Jul 29 '13 at 10:13
1

Okay so in essence ther are two things you want to check in your condition, that is we have two variables:

Variable 1: cart is empty

Variable 2: user is logged in

So we have 4 possibilities of events that can happen when someone visits your page:

  1. cart is empty AND user IS logged in

  2. cart is empty AND user is NOT logged in

  3. cart is NOT empty AND user IS logged in

  4. cart is NOT empty AND user is NOT logged in

Now this is your last comment regarding the conditions on which you want the form to show:

"The form shouldn't show if the user is logged in already and also if the logged in user cart is empty"

So in the first part you don't want the form when the user is logged in, that means options 1 and 3 above should NOT display the form (we are left with 2 and 4), then you say you don't want the form when the cart is empty, that means option 2 is a no no and we are left with option 4, that's when you do want the form to show:

You want the form when: Cart is NOT empty AND user is NOT logged in

Translated to code:

Cart is NOT empty:

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

User is NOT logged in:

$_SESSION['User']['AccessLvl'] == null

Combine with AND and you get

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

just as @TobiasKun posted in his answer

omma2289
  • 54,161
  • 8
  • 64
  • 68
  • so far as I understand the question she only want view the form when the cart is not empty and the user is not logged in. – Perry Jul 29 '13 at 09:16
  • Then the problem is not in the logic but in how the conditions are being tested. Try with @Jack's answer and tell us how it isn't working, is it displaying the form when you don't want to or not displaying when you want to. – omma2289 Jul 29 '13 at 09:23