implode function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

implode function

Post by tonyledenko »

Hello,

I am trying to pass multiple values, via an implode function. However, it isnt working. Here is the code from the first page, then the second page.

<?php
if(isset($_POST['numItems'])) {
$checkboxes = $_POST['numItems'];
$string = implode($value,"^");
echo $string;
}
?>

<form name='CartItem' action='https://www.ram-mount.com/RamCart/CartPutItem.php' method='POST'>
<input type='checkbox' name='numItems[]' value='RAM-VB-162'>
<input type='checkbox' name='numItems[]' value='RAM-VB-154'>
<input type='submit' value='Add To Cart'>
</form>

Second Page:

<?php
include_once('./CartDBCxn.php');
if (!isset($_COOKIE['sess'])) {
session_start();
$usrSession = session_id();
setcookie('sess',$usrSession,0,'/','ram-mount.com');
} else {
$usrSession = $_COOKIE['sess'];
}
if (isset($_GET['act'])) {
if ($_GET['act'] == 'remove') {
removeCartItem($_GET['part'],$usrSession);
}
}
if (isset($_POST['numItems'])) {
// Process the update code
$numItems = $_POST['numItems'];
$items = $_POST;
echo ("\n\n\n <!-- ");
print_r($_POST);
echo (" --> \n\n\n");
for ($x=0;$x < $numItems; $x++) {
$itm = 'item'.($x+1);
$quan = 'Qty'.($x+1);
changeCartItem($items[$itm],$usrSession,$items[$quan]);
}
}
?>
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Re: implode function

Post by maliskoleather »

methinks you should be doing the implode on the second page, not before the form
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

Re: implode function

Post by tonyledenko »

If I do the following then the cart updates; however I want to provide the user a choice based on check boxes.

<form name='CartItem' action='https://www.ram-mount.com/RamCart/CartPutItem.php' method='POST'>
<input type='hidden' name='part' value='RAM-VB-154^RAM-VB-162'><input type='submit' value='Add To Cart'>
</form>
vetrivel
Forum Newbie
Posts: 7
Joined: Thu Aug 07, 2008 5:54 am

Re: implode function

Post by vetrivel »

try to change ur implode like this
$string = implode("^",$value); instead of $string = implode($value,"^");
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

Re: implode function

Post by tonyledenko »

I did do that and one of the two values passed through; however, both didn't. Any other suggestions? Thanks

<?php
if(isset($_POST['part'])) {
$items = $_POST['part'];
$string = implode("^",$items);
echo $string;
}
?>

<form name='CartItem' action='https://www.ram-mount.com/RamCart/CartPutItem.php' method='POST'>
<input type='checkbox' name='part' value='RAM-VB-162'>
<input type='checkbox' name='part' value='RAM-VB-154'>
<input type='submit' value='Add To Cart'>
</form>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: implode function

Post by Stryks »

I just wanted to make a few comments on your code. Not in a harsh way or anything ... just thought I'd point out a few things and yeah ... you can take it or leave it. :)

Firstly ... you seem to be taking great pains to store the cart in a cookie, while using sessions in a way ... well .. I cant really tell how you're using them ... but it would seem like you're using the session_id in your cookie ...

I don't know. Anyhow, a cookie is created for the user when you start your sessions, so that on the next page view, PHP can associate the incoming cookie with the correct session data. In essesnce, it's automatically doing what you're doing with

Code: Select all

if (!isset($_COOKIE['sess'])) {
session_start();
$usrSession = session_id();
setcookie('sess',$usrSession,0,'/','ram-mount.com');
} else {
$usrSession = $_COOKIE['sess'];
}
Why not just pass a session_start() at the top of each page, and then just add the cart data directly to the session. No imploding. No double handling. Just a clean list of cart contents ready as soon as you use session_start().

Code: Select all

// Start sessions at the top of the page
session_start();
 
// Add items to the cart from submitted form
If(isset($_POST['numItems'])) {
   foreach($_POST['numItems'] as $checked_item) $_SESSION['CART'][] = array('item_id'=>$checked_item, 'qty'=>1);
}
 
// List all items in the cart
foreach($_SESSION['CART'] as $cart_id=>$cart_item) echo "Item information in cart slot $cart_id is {$cart_item['qty']} x {$cart_item['item_id']}<br />";
 
// Update a items quantity
$_SESSION['CART'][$slot_id_to_update]['qty'] = $new_quantity;
 
// Delete one item
unset($_SESSION['CART'][$slot_id_to_delete]);
 
// Delete entire cart
unset($_SESSION['CART']);
 
Anyhow ... back to your actual code ...

Code: Select all

<?php
if(isset($_POST['numItems'])) {
$checkboxes = $_POST['numItems'];
$string = implode($value,"^");
echo $string;
}
?>
It's not clear to me exactly what is going on with that. Are you posting from page 1 to page 2 and then page 2 is going to redirect to page 1?

Either way, I don't think your code is doing what want. If checkboxes have been checked, then you duplicate them into $checkboxes. Then you set $string to contain the results of an implode on an unknown or undefined $value, and then you echo it out. Now you have a redundant $checkboxes set and $string which should by rights contain nothing.

This is another confusing section ...

Code: Select all

if (isset($_POST['numItems'])) {
// Process the update code
$numItems = $_POST['numItems'];
$items = $_POST;
echo ("\n\n\n <!-- ");
print_r($_POST);
echo (" --> \n\n\n");
for ($x=0;$x < $numItems; $x++) {
$itm = 'item'.($x+1);
$quan = 'Qty'.($x+1);
changeCartItem($items[$itm],$usrSession,$items[$quan]);
}
}
$items = $_POST? Including the submit button and whatever else might be added to the form? Hmmm ...

$numItems = $_POST['numItems']? Well ... OK, if you must .. .but then the only place you seem to use it again is ... for ($x=0;$x < $numItems; $x++) {

So ... loop while $x is less than array? And then calculate the quantity based on how many times it loops? What the?

I recommend you have a look at the sessions approach I outlined above, and seriously consider scrapping the cookie approach. Sessions are easier, cleaner, and wont get you into so much trouble.

There's more I could write ... but yeah ... I don't want to flood you too much.

If you need clarification or more assistance getting either approach to work .. post back and we'll see what we can do.

Cheers
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

Re: implode function

Post by tonyledenko »

Thank you for the generous replay - wow i will try sessions to try to figure the problem.

Again, thanks and best wishes
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

Re: implode function

Post by tonyledenko »

If you don't mind, how would i assemble the form to comply with the start session? Thanks so much,

Tony
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: implode function

Post by Stryks »

I'm not really sure I can give much of an answer without more information.

It appears that you want to allow a user to tick one or more checkboxes on a form and have all checked items added to the cart. There did not seem to be a quantity field specified, so I assume that a checked box means "add 1 of this to the cart".

That is fine, except you're only actually returning one field in your form, as they all have the same name. This is probably what you were asking in your first post and I missed it ... sorry.

Code: Select all

// Incorrect
<input type='checkbox' name='part' value='RAM-VB-162'>
<input type='checkbox' name='part' value='RAM-VB-154'>
 
// Correct
<input type='checkbox' name='part[]' value='RAM-VB-162'>
<input type='checkbox' name='part[]' value='RAM-VB-154'>
 
The alteration of the name lets PHP return all of the checkboxes as an array. From memory, it will be an array holding only the checked items. Unchecked items are discarded.

So ... assuming that you want to use a default quantity of 1, you could then use the script I used above in the receiving page.

Code: Select all

// Start sessions at the top of the page
session_start();
 
// Add items to the cart from submitted form
If(isset($_POST['numItems'])) {
   foreach($_POST['numItems'] as $checked_item) $_SESSION['CART'][] = array('item_id'=>$checked_item, 'qty'=>1);
}
That should work quite happily, but if not, post back the code you are using and 'll try and spot the issues.

Cheers
Post Reply