Jump to content

loop check to see if session is set


justinh

Recommended Posts

<?php
session_start();

$item = $_GET['item'];


function outputItem($item){

for($i = 1; isset($_SESSION['$item2']); ++$i){

$item2 = "item".$i;


    echo $_SESSION['$item']."<br />";


        }
       $_SESSION['$item2'] = $item;
    echo $item;
    
?>

 

I'm trying to make an ajax shopping cart, so i need to check if a session is set, if it is, display it, check if the next session is set, display it, so on and so on until the session is not set, this is when we assign the $_GET string to the unset session, and display it.

 

I'm wondering If my code is correct. My server is down at the moment, so I can't upload it to see if it would work. Sorry if I didn't explain my intent well enough.

 

Thanks in advance. :)

Link to comment
https://forums.phpfreaks.com/topic/141815-loop-check-to-see-if-session-is-set/
Share on other sites

Okay I tested the script, didn't work. I modified it a little and came up with this.

 

<?php
session_start();

$_SESSION['item1'] = "Duck Hunt";

function outputItem($item){

for($i = 1; isset($_SESSION['$item.$i']); ++$i){



    echo $_SESSION['$item.$i']."<br />";
    ++$t;


        }
        
        //Once second parameter in for loop returns false
       $_SESSION['$item.$i'] = $item;
    echo $item;
    }




$item = "Awesome Job!";
outputItem($item);



?>

 

This is just outputting "Awesome Job!".

 

Any help would be greatly appreciated.

 

Sorry found a mistake

 

 

<?php
session_start();

$_SESSION['item1'] = "Duck Hunt";

function outputItem($item){

for($i = 1; isset($_SESSION['item.$i']); ++$i){



    echo $_SESSION['item.$i']."<br />";


        }
        
        //Once second parameter in for loop returns false
       $_SESSION['item.$i'] = $item;
    echo $item;
    }




$item = "Awesome Job!";
outputItem($item);



?>

 

still doesn't work though =/

Hello,

 

Its only outputting "Awesome Job!" becuase your passing that string to the function and then echoing it out. Nothing is changing to the $item variable it self.

 

Give this a run, see if it helps a bit.

 

session_start();

$_SESSION['items']['item1'] = "Duck Hunt baby!";
$_SESSION['items']['item2'] = "Duck Hunt #3!";


function outputItem($item){

    $total_items = count($_SESSION['items']); // total number of items
    $next_item_id = $total_items+1; // will be 3 in this example

    for($i = 1;$i<=$total_items;$i++){
        echo $_SESSION['items']['item'.$i]."<br />";
    }
    // store the new item
    $_SESSION['items']['item'.$next_item_id] = $item;
    echo $item."<br />";
}


$item = "Awesome Job!";
outputItem($item);

 

 

You shouldn't do it that way, you should use an array of items...

 

 

<?php

session_start ();


// if no items are set or exist!


if ( ! isset ( $_SESSION['items'] ) )
{
// you should also check if $_GET is valid

$_SESSION['items'] = array ( $_GET['item'] );
}
else
{
// $SESSION['items'] is already set so check if
// the item already exists before adding it...

if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) )
{
	// you should also check if $_GET is valid

	$_SESSION['items'][] = $_GET['item'];
}
}


// or displaying items

if ( isset ( $_SESSION['items'] ) )
{
// you should also check if $_GET is valid

foreach ( $_SESSION['items'], AS $item )
{
	echo $item . "\r\n<br />\r\n";
}
}
else
{
echo 'Sorry no items exist!';
}


?>

 

 

thanks for the help guys :)

 

here it is

 

http://www.wmptest.com/hrfinal/additem.html

 

tell me what you think :)

 

<?php
session_start();
function outputItem($item){

    $total_items = count($_SESSION['items']); // total number of items
    $next_item_id = $total_items+1; // will be 3 in this example

    for($i = 1;$i<=$total_items;$i++){
        echo $_SESSION['items']['item'.$i]."<br />";
    }
    // store the new item
    $_SESSION['items']['item'.$next_item_id] = $item;
    echo $item."<br />";
}


if(isset($_GET['clearlist'])){

session_destroy();
} else {

     $item = $_GET['item'];
outputItem($item);


   }

?>

 

 

Looks very good! :)

 

I would definitly institute the concept that printf showed, checking to see if the item is already within the session - at that point you can set a quantity, so instead of showing "milk, cheese, milk" you could show "milk (x2), cheese".

 

The next step would be to start setting up a database isntead of storing your items in a session. Sessions can easily be destoyed (as you showed in your code haha). :)

Found a problem =(

 

If you add eggs, and then try to add it again it doesn't submit. I know this doesn't have to do with the session script above, im pretty sure it has to do with the event im using.

 

<select id="item" onChange="addProduct();">

 

Is there a certain event type you suggest?

Okay, I'm having a little trouble with the counting system.

You can view the app here: www.wmptest.com/hrfinal/additem.html

 

I'm just happy I got this far lol... Could anyone tell me why it is displaying funky?

 

<?php
session_start ();
if(isset($_GET['clearlist'])){
session_destroy();
}else{

if ( ! isset ( $_SESSION['items'] ) )
{

$_SESSION['items']=array($_GET['item'], "1" );
}
else
{
if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) )



{



$_SESSION['items'][] = $_GET['item'];



}
else{

$_SESSION['items'][$_GET['item']][0] = $_SESSION['items'][$_GET['item']][0] + 1;
}

}
if ( isset ( $_SESSION['items'] ) )
{

foreach ( $_SESSION['items'] AS $item )



{





echo $item." x".$item[0]."<br />";




}
}
else
{
echo 'Sorry no items exist!';
}
}
?>

 

Thanks for the help in advance :)

 

Thanks for your input prinf.. I think I'm starting to understand how your code works.

 

I'm trying to add a qty system now using your code, but I'm stuck on the display part.

 

Could you please take a look at my code and tell me if im heading in the right direction.

 

 

 

<?php
session_start();

if ( ! isset ( $_SESSION['items'] ) )
{

   $_SESSION['items'] = array ( $_GET['item'], 1);
   
} else {

if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) )
     {
     $_SESSION['items'][] = $_GET['item'];
     $_SESSION['items'][$_GET['item']][] = 1;

     } else {
     $_SESSION['items'][$_GET['item']][] = $_SESSION['items'][$_GET['item']][] + 1;
     }
     
     
}

if ( isset ( $_SESSION['items'] ) )

{

// Displaying Items

} else {



     
}

 

Thanks,

 

Justin

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.