Page 1 of 1
array_push
Posted: Mon Dec 11, 2006 4:26 am
by kpraman
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi,
I am trying to add cart items using session.
Code: Select all
if (!isset($_SESSION['cartId'])) $_SESSION['carId'][] = $_POST['cartId'];
else array_push($_SESSION['cartId'],$_POST['cartId']);
I am getting error, first arg must be array.
Thanx
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Re: array_push
Posted: Mon Dec 11, 2006 4:32 am
by hrubos
kpraman wrote:Hi,
I am trying to add cart items using session.
Code: Select all
if (!isset($_SESSION['cartId'])) $_SESSION['carId'][] = $_POST['cartId'];
else array_push($_SESSION['cartId'],$_POST['cartId']);
I am getting error, first arg must be array.
Thanx
So I think you should "return "
http://cz.php.net/manual/cs/function.is ... .isset.php
Posted: Mon Dec 11, 2006 4:46 am
by kpraman
I am getting Not Found page.
Re: array_push
Posted: Mon Dec 11, 2006 4:55 am
by volka
If _SESSION[cartid] does not exists it's not an array. And you can only use [] on an existing array.
try
Code: Select all
if (!isset($_SESSION['cartId']) || !is_array($_SESSION['cartId'])) {
$_SESSION['carId'] = array();
}
$_SESSION['cartId'][] = $_POST['cartId'];
Posted: Mon Dec 11, 2006 4:56 am
by CoderGoblin
Try...
Code: Select all
if (// check if $_POST['cartid'] is valid) {
if (!isset($_SESSION['cartId'])) {
$_SESSION['carId'] = array($_POST['cartId']);
} else {
$_SESSION['carId'][]=$_POST['cartId']);
}
}
Note: Check the user input for cartid.
Never trust user input...
Edit: Beat me to it Volka...
Posted: Mon Dec 11, 2006 5:09 am
by kpraman
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Code: Select all
if (!isset($_SESSION['cartId']) || !is_array($_SESSION['cartId'])) {
$_SESSION['carId'] = array($_POST['cartId']);
}
else {
$_SESSION['cartId'][] = $_POST['cartId'];
}
print_r($_SESSION['carId']);
array_push()?
I am not getting the error. But, i am not able to add new values. The values are over written in [0].
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Mon Dec 11, 2006 5:17 am
by aaronhall
You are missing a 't' in 'cartID' on line 2. Here's volka's code with the typo fixed -- it should work fine:
Code: Select all
if (!isset($_SESSION['cartId']) || !is_array($_SESSION['cartId'])) {
$_SESSION['cartId'] = array();
}
$_SESSION['cartId'][] = $_POST['cartId'];
Posted: Mon Dec 11, 2006 5:38 am
by kpraman
Thanx aaronhall. Its working fine. sorry volka for the typo.
Thanx CoderGoblin.
Posted: Mon Dec 11, 2006 5:41 am
by volka
I didn't spot it either

Posted: Mon Dec 11, 2006 9:05 am
by kpraman
I want to display in cart page. If the values are repeated in the array, i should display the number of times the product is selected. How do i do it?
Posted: Mon Dec 11, 2006 9:10 am
by aaronhall
Posted: Tue Dec 12, 2006 2:23 am
by kpraman
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Thanx aaronhall.
Now iam struck in deleting.
Code: Select all
$cardId=$_SESSION['cartId'];
// Not working- start //
if($Submit=="Del")
{
$hiddel=$_POST['hiddel'];
unset($cardId[$hiddel]);
}
// Not working - end//
$uniqueCart=array_unique($cardId);
$arrayCount=array_count_values($cardId);
foreach($uniqueCart as $cardIds)
{
//code for fetch data from database using $cardIds.
//displaying the values.
<input type='hidden' name='hiddel' value='$cardIds'><input type='Submit' name='Submit' value='Del'> //for sending value to delete.
}
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]