Yii Framework Forum: [SOLVED] Session create & unset - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[SOLVED] Session create & unset create session Rate Topic: -----

#1 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 20 April 2012 - 02:40 AM

skenario nya

guest klik tombol add to cart, lalu item yang dia pilih masuk ke session bro, jadi segala pesanan di simpen di session. ada yang tau gak bro cara simpen atau create session gmn yah :)

terus kalo udah berhasil buat session cara panggilnya gmn yah? :(


kalo di php biasa kan register in session tinggal
session_register("admin");


nge set datanya
$_SESSION['admin']=blabla

manggil sessionnya atau cetaknya tinggal

echo $_SESSION['admin'];

kalo di yii gmn yah?
0

#2 User is offline   xent 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 14-November 10

Posted 21 April 2012 - 05:10 AM

coba cari di dokumentasi yii
ato
http://www.yiiframew...n-createmodify/
xent

My first app: School Information System
Find error? Post here
My Blog
0

#3 User is offline   mhdrasad 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 20-April 11
  • Location:Bumi Indonesia

Posted 21 April 2012 - 11:28 PM

View Postkfahmi, on 20 April 2012 - 02:40 AM, said:

skenario nya

guest klik tombol add to cart, lalu item yang dia pilih masuk ke session bro, jadi segala pesanan di simpen di session. ada yang tau gak bro cara simpen atau create session gmn yah :)

terus kalo udah berhasil buat session cara panggilnya gmn yah? :(


kalo di php biasa kan register in session tinggal
session_register("admin");


nge set datanya
$_SESSION['admin']=blabla

manggil sessionnya atau cetaknya tinggal

echo $_SESSION['admin'];

kalo di yii gmn yah?

sederhananya sih gini
untuk nulis
------------------------------
if (!empty($_POST['formnya'])){
$session = Yii::app()->session;
$session['contoh']= "halloo";
}


untuk panggil
--------------------------------
$session = Yii::app()->session;
if(!empty ($session['contoh'])){
$contoh = $session['contoh'];
echo $contoh;


gitu lah kira2 , maap ni klo salah . sambil makan...
Wait For True Concept or Finish it With Coding Concept .
#chadnox
0

#4 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 22 April 2012 - 11:42 PM

View Postputrilangit, on 21 April 2012 - 11:28 PM, said:

sederhananya sih gini
untuk nulis
------------------------------
if (!empty($_POST['formnya'])){
$session = Yii::app()->session;
$session['contoh']= "halloo";
}


untuk panggil
--------------------------------
$session = Yii::app()->session;
if(!empty ($session['contoh'])){
$contoh = $session['contoh'];
echo $contoh;


gitu lah kira2 , maap ni klo salah . sambil makan...



berhasil kak,, cuma mau nanya lagi nih sedikit,
nya

public function actionAddCart($id)
        {
                $model=$this->loadModel($id);
                
            if (!empty($model))
                {
                    $session = Yii::app()->session;
//format nama session cart+id_item jadinya cart3 dst
                    $namaSes = "cart".$id;
                    $session["$namaSes"]= $model->brand;

                    $this->redirect(array('view','id'=>$model->id_item));
                }
                else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	
    
        }


jadi nnti ada sessionnya
$session['cart1'];
$session['cart2'];//DST tergantung pengunjung add untuk item yang mana aja.

yang mau saya taunya gmn cara nampilinnya yah? code di view saya _cart
$session = Yii::app()->session;
                $asd = $session["cart32"];
                echo $asd;

cuma cart 32 doang padahal di session ada cart 31 dan 32
0

#5 User is offline   teguh11 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 129
  • Joined: 13-November 11

Posted 23 April 2012 - 02:56 AM

coba
echo $asd;
diganti jadi
print_r($asd);

belajar itu mudah..
0

#6 User is offline   mhdrasad 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 20-April 11
  • Location:Bumi Indonesia

Posted 23 April 2012 - 04:04 AM

View Postkfahmi, on 22 April 2012 - 11:42 PM, said:

berhasil kak,, cuma mau nanya lagi nih sedikit,
nya

public function actionAddCart($id)
        {
                $model=$this->loadModel($id);
                
            if (!empty($model))
                {
                    $session = Yii::app()->session;
//format nama session cart+id_item jadinya cart3 dst
                    $namaSes = "cart".$id;
                    $session["$namaSes"]= $model->brand;

                    $this->redirect(array('view','id'=>$model->id_item));
                }
                else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	
    
        }


jadi nnti ada sessionnya
$session['cart1'];
$session['cart2'];//DST tergantung pengunjung add untuk item yang mana aja.

yang mau saya taunya gmn cara nampilinnya yah? code di view saya _cart
$session = Yii::app()->session;
                $asd = $session["cart32"];
                echo $asd;

cuma cart 32 doang padahal di session ada cart 31 dan 32



coba nama session untuk chartnya disertakan ke URL , kira2 jadi begini
public function actionAddCart($id)
        {
                $model=$this->loadModel($id);
                
            if (!empty($model))
                {
                    $session = Yii::app()->session;
//format nama session cart+id_item jadinya cart3 dst
                    $namaSes = "cart".$id; // ini dimasukin ke url
                    $session["$namaSes"]= $model->brand;

                    $this->redirect(array('view','id'=>$model->id_item,'namaSes'=>$namaSes));
                }
                else
                        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
        
    
        }


ntar untuk manggil sessionnya kira2 begini
$session = Yii::app()->session;
                $asd = $session["$namaSes"];
                echo $asd;


belum di test sih , tapi ya kira2 begitu lah... :D
Wait For True Concept or Finish it With Coding Concept .
#chadnox
0

#7 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 23 April 2012 - 04:06 AM

View Postteguh11, on 23 April 2012 - 02:56 AM, said:

coba
echo $asd;
diganti jadi
print_r($asd);




udah solved, tapi gak tau gak ini cara yang paling efisien apa engga.

controller
  //CART
        public function actionAddCart($id)
        {
                $model=$this->loadModel($id);
                
            if (!empty($model))
                {
                    $session = Yii::app()->session;
                    $namaSes = "cart".$id;
                    $session["$namaSes"]= $model->brand;

                    $this->redirect(array('view','id'=>$model->id_item));
                }
                else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	
    
        }
        
        public function actionDelCart($name)
        {
           $session = Yii::app()->session;
           
            unset($session["$name"]);
            $this->redirect(array('search'));
        }
        


view
<?php echo CHtml::link('View Cart','#',array('class'=>'view-cart')); ?>
<div class="cart" style="display:none">
<?php $this->renderPartial('/layouts/_cart',array(
	'model'=>$model,
)); ?>
</div><!-- CART -->



_cart

$i=1;
while (list ($key, $val) = each ($_SESSION)) 
{ 
    $valid_key = "cart*";
    if (eregi($valid_key, $key))//HANYA SESSION YANG CART YANG MASUK SINI
    {
        echo "<p>$i. $val";
        echo CHtml::link('delete',array('delCart', 'name'=>"$key"));
        echo "</p>";
        $i++;
    }
  
} 


gak tau sih sementara baru kaya gini,

klik barang dengan id_item 32 jadi kebentuk session dengan nama cart32, kalo mau di delete ya ilang deh tuh unset cart32 nya. gitu lah kira2. gak tau ini udah bener atau blm hehehe cuma sharing :)
0

#8 User is offline   mhdrasad 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 20-April 11
  • Location:Bumi Indonesia

Posted 23 April 2012 - 07:27 AM

mantabzz.... jgn lupa di solved klo udah gan... :D
Wait For True Concept or Finish it With Coding Concept .
#chadnox
0

#9 User is offline   mbahsomo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 29-March 10

Posted 03 September 2012 - 12:05 AM

Mancap Mas ........


Merdeka.
Sugik Puja Kusuma [mbahsomo]
http://sugik.do-event.com
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users