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
最满意答案
我不完全确定你要做什么,但如果你想节省一些打字,你为什么不这样做:
// 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"];
相关问答
更多-
什么是在Xcode4 Interface Builder中引用outlet集合?(What is Referencing outlet collection in Xcode4 Interface Builder?)[2023-03-08]
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) { ...
-
为什么我的插座没有?(Why is my outlet nil?)[2021-08-26]
我的猜测是你实际上有多个OakController实例而不是你期望的实例。 您是否将蓝色立方体拖入IB文档并将其自定义类更改为OakController? 这将每次分配和初始化一个新对象。 我猜你的sharedInstance方法也分配并初始化一个实例。 尝试将一个awakeFromNib方法添加到OakController,并添加一个断点。 记录自己的指针值。 在你的第二种情况下,插座出乎意料地为零,也记录了自己的指针地址。 My guess is that you actually have multi ... -
带标签的插座集合(Outlet Collection with Label)[2024-01-04]
尝试这个 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; 如 ...
-
标签插座无效(The label outlet is invalid)[2022-04-30]
您需要创建UITableViewCell的自定义子类并在那里声明您的IBOutlets 。 然后在整个主视图控制器中使用该子类而不是通用UITableViewCell 。 重申:tableview单元格内的元素属于单元格,而不是包含所有内容的视图控制器。 编辑:在你的视图控制器中,你将遵循UITableView的委托和数据源协议(并实现与你正在尝试完成的任何相关的方法)。 您将在后面的数据源方法中处理填充(初始)单元格数据: func tableView(tableView: UITableView, ce ... -
为什么XIB文件不会立即反映最近的Outlet更改?(Why doesn't the XIB file reflect recent Outlet changes immediately?)[2023-09-17]
通常会发生这种情况,因为您尚未保存进行插座修改的文件。 每次你完成修改时只需保存标题就可以解决这个问题(因为@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