Jump to content

Strange outcoming using ?action= in a hyperlink


Recommended Posts

I'm making a simple shopping cart. I have a strange issue when I add a product to the cart - the correct id registers ("1"), but the size registers as "0" when it should be "sizel". Can anyone see why is this happening?

 

<?php

if (isset ($_GET['action']) && $_GET ['action'] == add || size) {
	$id = intval($_GET['id']);
	$_SESSION['cart'][] = $id;
	$size = intval($_GET['size']);
	$_SESSION['cart'][] = $size;
}
$cart = $_SESSION['cart'];

if (!$cart) {

	echo "No product in cart.";

} elseif ($cart) {

	foreach ($_SESSION['cart'] as $id) {

		echo $id;
	}
	foreach ($_SESSION['cart'] as $size) {
		echo $size;
	}
}
?>

<A href="index.html?action=add&id=1&size=sizel">Add to cart</A>

Link to comment
Share on other sites

if (isset ($_GET['action']) && $_GET ['action'] == add || size) {

Should be throwing undefined constant errors.

If the size a required option?

// If size is not required //
if (isset($_GET['action']) && $_GET ['action'] == 'add') {
$id = intval($_GET['id']);
$_SESSION['cart'][] = $id;
$size = (isset($_GET['size']) ? $_GET['size'] : null);
$_SESSION['cart'][] = $size;
}

// if size is required //
if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') {
$id = intval($_GET['id']);
$_SESSION['cart'][] = $id;
$size = $_GET['size'];
$_SESSION['cart'][] = $size;
}

 

 

 

Link to comment
Share on other sites

That fixed it. Thanks for that man it was really driving me up the wall. I'm new to this level of PHP so theres a bit I don't understand, but I'll get the hang of it soon.

 

I've got one other small issue related related to the same code, I don't want to open another thread so I'll just ask here.

I've got:

 

} elseif ($cart) {
foreach ($_SESSION['cart'] as $id) {
	echo $id;
}
foreach ($_SESSION['cart'] as $size) {
	echo $size;
}

 

This produces double results and I can now see why, but is there a way to do something like this?

 

elseif ($cart) {
foreach ($_SESSION['cart']['id'] as $id) {
	echo $id;
}
foreach ($_SESSION['cart']['size'] as $size) {
	echo $size;
}
}

Link to comment
Share on other sites

When you are adding the elements to the array just add a key

$_SESSION['cart']['id'] = $id;

This method will only work if you are only adding one product to the cart.

 

If you want to add more than one element to the cart, you should use an array (i wont write the code for you because its better to learn if you try it yourself) but basically. Your cart will store an array of products, each product element could also be an array containing the id, size and quantity values :)

Link to comment
Share on other sites

i wont write the code for you because its better to learn if you try it yourself

 

No, that style of learning is not for my personality. I get too frustrated with trying and failing over a million times. This cannot change, its who I am.

I am not a Help Vampire. I am a visual learner. I learn best by looking at code and putting together its logic in my head.

 

Link to comment
Share on other sites

I tried your code:

 

$_SESSION['cart']['id'] = $id ... but it did not work.

 

elseif ($cart) {
foreach ($_SESSION['cart']['id'] = $id;) {
echo $id;
}

I don't see how it would work anyway, as there is a semi-column after $id

I tried this also, but it failed:

elseif ($cart) {
foreach ($_SESSION['cart']['id'] as $id) {
echo $id;
}

 

Link to comment
Share on other sites

You cannot use foreach on a string, $_SESSION['cart']['id'] is the value (hence me saying you will only be able to add one product)

 

Anyways, ill write something, you can merge it :)

$_SESSION['cart']['products'][] = array(
'size' => $size,
'id' => $id,
'qty' => $qty
);

Then when fetching the data you can use

echo '<h3>Products</h3>'
foreach ($_SESSION['products'] as $product) {
echo 'Product ID: ' , $product['id'] , '<br/>Size: ', $product['size'] , '<br/>Quantity: ', $product['qty'];
echo '<hr/>';
}

Link to comment
Share on other sites

Sorry for a 10 minute reply. I don't copy and paste code that people show me, I write it up myself because it helps me better understand what is going on from another mental dimension.

Ok, so I implimented the code style you suggested into what I had. It differs slightly form your code in terms of naming, but the code logic is the same.

The code failed. All I get is "No product in cart.":

 

<?php
if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') {
	$id = intval($_GET['id']);
	$_SESSION['cart'][] = $id;
	$size = $_GET['size'];
	$_SESSION['cart'][] = $size;
}
$_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id);
if (!$cart) {
	echo "No product in cart.";
} else foreach ($_SESSION['content'] as $content) {
	echo $content['id'] , $content['size'];
}
?>

Link to comment
Share on other sites

You really need to turn on your error_reporting :)

It will show you whats going on.

$_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id);

Should be in your first IF statement and

} else foreach ($_SESSION['content'] as $content) {
echo $content['id'] , $content['size'];
}

should be

} else  {foreach ($_SESSION['content'] as $content) {
echo $content['id'] , $content['size'];
}
}

The other thing is I dont see anywhere in that code where $cart is defined.

Try using

if (empty($_SESSION['cart']['content'])) { // throw no products error }

Link to comment
Share on other sites


if (!$cart) {
      echo "No product in cart.";

The $cart variable is not defined anywhere, also, each time you use $_SESSION['cart'][] = ??? a new array is created in the $_SESSION['cart'] variable. Have you ever used a ternary operator?

 

 

session_start();
if ( !isset($_SESSION['cart']) )
   $_SESSION['cart'] = array();
if ( isset($_GET['action']) && !strcasecmp($_GET['action'], 'add') )
{
   $cart         = array();
   $cart['id']   = isset($_GET['id'])   ? (int)$_GET['id']   : '';
   $cart['size'] = isset($_GET['size']) ? $_GET['size'] : '';
   $_SESSION['cart'][] = $cart;
}
if ( empty($_SESSION['cart']) )
{
   echo 'No products in cart.';
}
else
{
   foreach($_SESSION['cart'] AS $item) :
      echo '<pre>' . print_r($item, 1) . '</pre>';
   endforeach;
}

Link to comment
Share on other sites

Buddski, I followed your explanation but the page totally failed and error said "PHP Parse error:  syntax error, unexpected T_VARIABLE, expecting '(' on line 38", which is:

if $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id);

 

<?php
if $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id);
else {foreach ($_SESSION['content'] as $content) {
	echo $content['id'] , $content['size'];
}
if (empty($_SESSION['cart']['content'])) {echo "No product in cart";}
?>

From my understanding of PHP, this makes no sense at all.

 

:'( I'm just going round and round and round in circles. Could you please write it from start to finish so I can see clearly how its engineered? The last explination totally blew my mind and I've totally lost it. If I can see the complete code I can sit here and see how everything works and how its done. Each code example just fucks my mind. I got ADD. I can't all this.

Link to comment
Share on other sites

I have reverted back to the last state where the page loaded. I still cannot add a product into the session.

The code sits at this:

 

<?php
if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') {
	$id = intval($_GET['id']);
	$_SESSION['cart'][] = $id;
	$size = $_GET['size'];
	$_SESSION['cart'][] = $size;
}
$_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id);
if (!$cart) {
	echo "No product in cart.";
} else foreach ($_SESSION['content'] as $content) {
	echo $content['id'] , $content['size'];
}
?>

 

A product is added with this:

<A href="index.html?action=add&id=1&size=sizel">Add to cart</A>

Link to comment
Share on other sites

I just tried this code also, but total failure!

 

<?php
if ($_SESSION['cart']['content'][] = array) {'size' => $size, 'id' => $id};
	$id = intval($_GET['id']);
	$_SESSION['cart'][] = $id;
	$size = $_GET['size'];
	$_SESSION['cart'][] = $size;
if (empty($_SESSION['$cart']['content'])) {
	echo "No product in cart.";
} else {foreach ($_SESSION['content'] as $content) {
	echo $content['id'] , $content['size'];
}
?>

 

PHP Parse error:  syntax error, unexpected ')', expecting '(' on line 38:/QUOTE]

if ($_SESSION['cart']['content'][] = array) {'size' => $size, 'id' => $id};
Link to comment
Share on other sites

<?php
if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') {
	$id = intval($_GET['id']);
	$_SESSION['cart'][] = $id;
	$size = $_GET['size'];
	$_SESSION['cart'][] = $size;
}
$_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id);
if (!$cart) {
	echo "No product in cart.";
} else foreach ($_SESSION['content'] as $content) {
	echo $content['id'] , $content['size'];
}
?>

It will show you whats going on.

$_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id);

Should be in your first IF statement and

} else foreach ($_SESSION['content'] as $content) {
echo $content['id'] , $content['size'];
}

should be

} else  {foreach ($_SESSION['content'] as $content) {
echo $content['id'] , $content['size'];
}
}

The other thing is I dont see anywhere in that code where $cart is defined.

Try using

if (empty($_SESSION['cart']['content'])) { // throw no products error }

 

I have done this and still failure. Perhaps if you can write the code out in full, then there will be no error in quote interpretation and I will be able to understand what is going on with the PHP logic. This is what I did on interpretation of your instructions.

<?php
if ($_SESSION['cart]['content'[] = array ( 'size' => $size, 'id' => $id);
	$id = intval($_GET['id']);
	$_SESSION['cart'][] = $id;
	$size = $_GET['size'];
	$_SESSION['cart'][] = $size;
}
if (empty($_SESSION['cart]['content'])) {
	echo "No product in cart.";
} else foreach ($_SESSION['content'] as $content) {
	echo $content['id'] , $content['size'];
}
?>

 

I think you can agree with me now why I am a visual lerner.

Link to comment
Share on other sites

Part of the problem is that you're panicking and throwing shit at the wall to see if it will work, without actually understanding what your code is doing.  This is evidenced by you misunderstanding what Buddski was trying to tell you.  So, take a deep breath and calm down.  Programming is frustrating, but getting worked up over it will only make it harder for you to learn.

 

Also, comments like:

 

Awesome. Blow my mind and then desert me. Good brotherhood here.

 

Will only lead to people not wanting to help you.  Everyone here - including those of us with badges under our names - are volunteers.  We don't see one red cent for being here.  No one is obligated to help you.  Remember that when you get frustrated.

 

So, all that said, here is what Buddski was trying to tell you:

 

if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add')
{
   $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); // <-- add item to your cart's content array
}

if (empty($_SESSION['cart']['content'])) // <-- if the cart doesn't contain any content
{
   echo "No products in cart";
}
else
{
   foreach ($_SESSION['cart']['content'] as $content)
   {
      echo "{$content['id']}, {$content['size']}";
   }
}

 

Finally, you would be well served to brush up on the basics.  Basic if/else syntax, arrays, echo syntax... a lot of the most basic stuff is tripping you up, making it a lot harder for you to get results.  You're trying to run without being able to even crawl.

Link to comment
Share on other sites

This is evidenced by you misunderstanding what Buddski was trying to tell you.

I couldn't understand it because it was written up poorly.

 

Also, comments like:

 

Awesome. Blow my mind and then desert me. Good brotherhood here.

 

Will only lead to people not wanting to help you.  Everyone here - including those of us with badges under our names - are volunteers.  We don't see one red cent for being here.  No one is obligated to help you.  Remember that when you get frustrated.

I was just saying the truth man. He deserted me.

I know everyone here are volunteers and they don't have to help me, but if they are going to help and I point out I suck at learning academically and that I am visual learner, then respect this and help me in a visual way or don't help me at all, not continue on helping me in a way that I said was confusing from early conversation and then desert me.. and that is why I made that comment.

 

Finally, you would be well served to brush up on the basics.  Basic if/else syntax, arrays, echo syntax.

I am useally good at them, but like you said, I panicked and started throwing shit at a wall to see what would happen.

 

I followed the code you suggested and the page has returned to working again. I have things adding to the session, but here is the strange thing.. they are added as "," (single comma), not the 'id' and 'size' names.

 

Here is the PHP code:

<?php
session_start();
if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') {
	$_SESSION['cart']['content'][] = array ( 'side' => $size, 'id' => $id);
}
if (empty($_SESSION['cart']['content'])) {
	echo "No product in cart.";
} else {
	foreach ($_SESSION['cart']['content'] as $content) {
		echo "{$content['id']}, {$content['size']}";
	}
}
?>

 

... and here is the HTML code:

<A href="index.html?action=add&id=1&size=sizel">Add to cart</A>

 

Really strange, I can't see any commas anywhere in the HTML code, its also stranger that only 1 comma is showing.

 

... and thanks for helping me understand how the logic flow works. now that I have chilled out and read it its really quite clear now.

Link to comment
Share on other sites

Firstly, I didnt "desert you", I am in no way assigned to your specific problem, this is a FORUM used for discussing problems, if somebody can take what I have advised you and in some way improve on that then that is fantastic for you. Everybody has a different approach to problems.

I post what I think will help you, being a visual learner.

I am aware that I need to work on my instructional delivery (comments and the like) but saying that I deserted you is just rude to be honest.

 

But whatever, in regards to your code are you referring to the echoing of the elements displaying with a , (comma)?

echo "{$content['id']}[color=red][b],[/b][/color] {$content['size']}";

That is where your comma is hiding. That echo statement will echo the ID from the session and then the size separated by a comma.

So your output from the below example will be

1, sizel

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.