首页 \ 问答 \ PHP会话变量(php session variable)

PHP会话变量(php session variable)

我的会话变量似乎不断下降其数组值。 我可能做错了什么? 这是我的对象吗?

session_start()在开始时启动,

if(isset($_SESSION['locations'])) { 
     unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']); 
     echo "session exists"; 
}
else{
     $_SESSION['locations'] = serialize(new Location());
     unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']);
     echo "session does not exist";
}        

class Location{

    function listItems($location){

    $array;

    $array[] = $location;

    //print_r($array);

    // Re-index:

    $array = array_values($array);

    print_r($array);

    $count = count($array);

    for ($i = 0; $i < $count; $i++) {

        echo "{$array[$i]}\n";

    }

    }

}

my session variable seems to keep dropping its array values. What could I be doing wrong? Is it my object?

session_start() is initiated at the beginning,

if(isset($_SESSION['locations'])) { 
     unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']); 
     echo "session exists"; 
}
else{
     $_SESSION['locations'] = serialize(new Location());
     unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']);
     echo "session does not exist";
}        

class Location{

    function listItems($location){

    $array;

    $array[] = $location;

    //print_r($array);

    // Re-index:

    $array = array_values($array);

    print_r($array);

    $count = count($array);

    for ($i = 0; $i < $count; $i++) {

        echo "{$array[$i]}\n";

    }

    }

}

原文:https://stackoverflow.com/questions/2782936
更新时间:2022-03-30 16:03

最满意答案

我不完全确定你要做什么,但如果你想节省一些打字,你为什么不这样做:

// assign the label you want to use to a local variable

UILabel *selectedLabel;
selectedLabel = nil;
if (is_x) {
    selectedLabel = labelField_x;
} else if (is_y) {
    selectedLabel = labelField_y;
} else if (is_z) {
    selectedLabel = labelField_z;
}

// now work with selectedLabel so you don't have to repeat yourself

if (!pressedDecimal){
    selectedLabel.text = ...

或者您甚至可以将IBOutlet属性中的标签粘贴到字典中,以便快速为任何给定字符(字符串)获取正确的标签:

// create a lookup dictionary (once, e.g. in awakeFromNib)

NSDictionary *labelForCharacter;
labelForCharacter = @{
    @"x" : labelField_x,
    @"y" : labelField_y,
    @"z" : labelField_z
};

 // get the label for "z"

UILabel *selectedLabel;
selectedLabel = labelForCharacter[@"z"];

I'm not entirely sure what you're up to, but if you want to save yourself some typing, why don't you do something like this:

// assign the label you want to use to a local variable

UILabel *selectedLabel;
selectedLabel = nil;
if (is_x) {
    selectedLabel = labelField_x;
} else if (is_y) {
    selectedLabel = labelField_y;
} else if (is_z) {
    selectedLabel = labelField_z;
}

// now work with selectedLabel so you don't have to repeat yourself

if (!pressedDecimal){
    selectedLabel.text = ...

Or you could even stick the labels from your IBOutlet properties right into a dictionary so you can quickly get the right label for any given character (string):

// create a lookup dictionary (once, e.g. in awakeFromNib)

NSDictionary *labelForCharacter;
labelForCharacter = @{
    @"x" : labelField_x,
    @"y" : labelField_y,
    @"z" : labelField_z
};

 // get the label for "z"

UILabel *selectedLabel;
selectedLabel = labelForCharacter[@"z"];

相关问答

更多
  • IBOutletCollection是分组IBOutlets一种方法。 想象一下,你有3或4个UILabels ,你将在其上应用一种风格(字体,背景颜色,不透明度等)。 使用IBOutletCollection ,这样做变得微不足道。 首先你需要定义你的IBOutletCollection : @property (nonatomic, retain) IBOutletCollection(UILabel) NSArray *labelsCollection; (注意我们放在括号内的类型,尽管我们可以放一 ...
  • 您可以在自定义视图子类的@interface中手动编写IBOutlet属性声明,并假设您已经在IB中定义了子视图的基类,然后可以将代码中的出口圈拖到控件中现场。 或者,正如你所指出的那样,沃伦·伯顿(Warren Burton)在对另一个问题的回答中提出了这种技术,另外一个问题是, 不能在IB中查看 。 You can manually write the IBOutlet property declaration in the @interface of the custom view subclass, ...
  • 我不完全确定你要做什么,但如果你想节省一些打字,你为什么不这样做: // assign the label you want to use to a local variable UILabel *selectedLabel; selectedLabel = nil; if (is_x) { selectedLabel = labelField_x; } else if (is_y) { selectedLabel = labelField_y; } else if (is_z) { ...
  • 我的猜测是你实际上有多个OakController实例而不是你期望的实例。 您是否将蓝色立方体拖入IB文档并将其自定义类更改为OakController? 这将每次分配和初始化一个新对象。 我猜你的sharedInstance方法也分配并初始化一个实例。 尝试将一个awakeFromNib方法添加到OakController,并添加一个断点。 记录自己的指针值。 在你的第二种情况下,插座出乎意料地为零,也记录了自己的指针地址。 My guess is that you actually have multi ...
  • 尝试这个 for(UILabel* label in self.view.subview) { switch(label.tag) { case:0 label.text = @"A"; } break; } 编辑: 对于动画,你可以尝试这个 - view1.alpha = 1.0; [UIView animateWithDuration:2.0 delay:0.0 options:U ...
  • 你可以翻转 file_ID_doublearr变量,你就可以拥有它。 file_ID_doublearr = str2double(file_ID); file_ID = file_ID_doublearr(end:-1:1); % or 'flipud' / 'fliplr' 现在您可以完成脚本的其余部分,文件名应该从4到1排序。 我想这是您尝试更改数字的行: file_ID_doublearr = max(file_ID_doublearr) - file_ID_doublearr + 1; 如 ...
  • 您需要创建UITableViewCell的自定义子类并在那里声明您的IBOutlets 。 然后在整个主视图控制器中使用该子类而不是通用UITableViewCell 。 重申:tableview单元格内的元素属于单元格,而不是包含所有内容的视图控制器。 编辑:在你的视图控制器中,你将遵循UITableView的委托和数据源协议(并实现与你正在尝试完成的任何相关的方法)。 您将在后面的数据源方法中处理填充(初始)单元格数据: func tableView(tableView: UITableView, ce ...
  • 通常会发生这种情况,因为您尚未保存进行插座修改的文件。 每次你完成修改时只需保存标题就可以解决这个问题(因为@Steven Fisher指出命令+选项+ s保存全部),如果这不起作用,那么构建通常会(命令+ shift +) b)如果所有其他方法都失败了,那么一个好的清理(命令+ shift + k)应该直截了当。 Usually this happens because you haven't yet saved the file in which you made the outlet modifica ...
  • 如果将故事板或XIB文件中的表格视图单元格的“自定义类”设置为“ ArticleCell ”,则应该可以控制将插座再次拖动到标签。 If you set the "Custom Class" of your table view cell in your storyboard or XIB file to "ArticleCell", you should be able to control drag your outlets to your labels again.
  • 您的手机是自定义手机吗? 如果没有单击您的单元格并转到属性检查器,则在表格视图单元格下选择样式为自定义 Is your cell a custom cell. If not click on your cell and go to the attribute inspector then under table view cell choose the style as custom

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。