|
Posted by John Taylor-Johnston on 10/02/05 07:18
Robert Cummings wrote:
>On Sat, 2005-10-01 at 23:57, John Taylor-Johnston wrote:
>
>
>>$TrolleyContents is a string.
>>Basically what I want to accomplish here is if $TrolleyContents already
>>exists append $AddToTrolley to $TrolleyContents, if not register
>>$TrolleyContents.
>>Am I going about it right?
>>John
>>
>><?php
>>#printcontents.php
>>
>>session_name("CCLTrolley");
>>session_start();
>>
>>if (isset($HTTP_POST_VARS["AddToTrolley"]))
>>{
>>$TrolleyContents = $TrolleyContents.",".$HTTP_POST_VARS["AddToTrolley"];
>>}else{
>>session_register("TrolleyContents");
>>}
>>echo $TrolleyContents;
>>
>>phpinfo();
>>?>
>>
>>
>
>Looks a bit odd to me :) But could be because you're using outdated
>semantics. It should be sufficient to do the following:
>
><?php
>
>session_name( 'CCLTrolley' );
>session_start();
>
>//
>// Initialize the trolley.
>//
>if( !isset( $_SESSION['TrolleyContents'] ) )
>{
> $_SESSION['TrolleyContents'] = '';
>}
>
>//
>// Add new entry.
>//
>if( isset( $_POST['AddToTrolley'] ) )
>{
> if( $_SESSION['TrolleyContents'] ) == '' )
> {
> $_SESSION['TrolleyContents'] = $_POST['AddToTrolley'];
> }
> else
> {
> $_SESSION['TrolleyContents'] .= ','.$_POST['AddToTrolley'];
> }
>}
>
>echo $_SESSION['TrolleyContents'];
>
>phpinfo();
>
>?>
>
>Is there a reason you're using a comma delimited string? I would
>recommend using an array instead:
>
><?php
>
>session_name( 'CCLTrolley' );
>session_start();
>
>//
>// Initialize the trolley.
>//
>if( !isset( $_SESSION['TrolleyContents'] ) )
>{
> $_SESSION['TrolleyContents'] = array();
>}
>
>//
>// Add new entry.
>//
>if( isset( $_POST['AddToTrolley'] ) )
>{
> $_SESSION['TrolleyContents'][$_POST['AddToTrolley']] =
>$_POST['AddToTrolley']
>}
>
>echo implode( ',', $_SESSION['TrolleyContents'] );
>
>phpinfo();
>
>?>
>
>
Why is it outdated semantics?
P.S. do folks prefer I reply on top or on bottom?
Navigation:
[Reply to this message]
|