array_push

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
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

array_push

Post by kpraman »

feyd | Please use

Code: Select all

,

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

,

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]
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Re: array_push

Post 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
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

I am getting Not Found page.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: array_push

Post 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'];
Last edited by volka on Mon Dec 11, 2006 4:57 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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...
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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'];
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

Thanx aaronhall. Its working fine. sorry volka for the typo.

Thanx CoderGoblin.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I didn't spot it either ;)
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post 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?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

feyd | Please use

Code: Select all

,

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

,

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]
Post Reply