简体   繁体   English

如何在活动会话数组中添加变量?

[英]How do I add a variable to my active session array?

I may be going about this the wrong way, but I only learn by asking. 我可能会走错路,但是我只能通过提问来学习。

I have a form that gathers name, address, etc.. The form was originally designed with just one field for name called bill_name . 我有一个收集名称,地址等的表单。该表单最初设计时仅使用一个名为bill_name字段。 I have changed it to two fields bill_fname and bill_lname . 我将其更改为两个字段bill_fnamebill_lname The session data is then used for visual verification on the next page and then submitted for processed following that. 然后将会话数据用于下一页的视觉验证,然后提交以进行后续处理。

How do I combine the variables $_SESSION['donate']['bill_fname'] and $_SESSION['donate']['bill_lname'] and add them to the array as $_SESSION['donate']['bill_name'] on the verification page before sending them to be processed? 如何合并变量$_SESSION['donate']['bill_fname']$_SESSION['donate']['bill_lname']并将$_SESSION['donate']['bill_lname']作为$_SESSION['donate']['bill_name']添加到数组中在验证页面上发送它们进行处理之前?

The reason for having all 3 variables in the array is a good part of the processing code uses the $_SESSION['donate']['bill_name'] and the new code I'm adding needs the the separate values of $_SESSION['donate']['bill_fname'] and $_SESSION['donate']['bill_lname'] 在数组中拥有所有3个变量的原因是处理代码的重要组成部分使用$_SESSION['donate']['bill_name'] ,而我要添加的新代码需要$_SESSION['donate']['bill_fname']的单独值$_SESSION['donate']['bill_fname']$_SESSION['donate']['bill_lname']

I hope this makes sense. 我希望这是有道理的。

It's fairly simply, just concatenate the strings (i'm using a space here also) and assign them to the bill_name key; 这很简单,只需连接字符串(我也在这里使用空格)并将它们分配给bill_name键;

  $_SESSION['donate']['bill_name'] = $_SESSION['donate']['bill_fname'] . " " . $_SESSION['donate']['bill_lname']

Also, I don't think there's anything inherently wrong with this approach. 而且,我认为这种方法没有内在的错误。 However, I'm not much a php developer so I'm not knowledgable on best practices. 但是,我不是一个PHP开发人员,因此我对最佳实践一无所知。

try: 尝试:

$_SESSION['donate']['bill_name'] = $_SESSION['donate']['bill_fname']." ".$_SESSION['donate']['bill_lname'];
echo $_SESSION['donate']['bill_name'];

Or if you want the separate parts of the name in tact: 或者,如果您想保留名称的各个部分,请执行以下操作:

$_SESSION['donate']['bill_name'] = array(
    'bill_fname' => $_SESSION['donate']['bill_fname'],
    'bill_lname' => $_SESSION['donate']['bill_lname'],
    'full_name' => $_SESSION['donate']['bill_fname'] . ' ' . $_SESSION['donate']['bill_lname']
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM