简体   繁体   English

PHP Codeigniter,我应该设置数组默认值还是直接使用if .. else语句重复

[英]PHP Codeigniter, should i set array defaults or simply use if.. else statements repeatedly

So I am utilizing codeigniter. 所以我正在使用codeigniter。

To allow reusability of code i have a number of functions in my model which get different things. 为了允许代码的可重用性,我的模型中有很多函数会得到不同的结果。

eg get_details, get_features, get_products 例如get_details,get_features,get_products

I then have a function called get_all which calls all these methods, so If i want i can get them all but otherwise i can use them individually. 然后,我有一个名为get_all的函数,该函数调用所有这些方法,因此,如果我希望可以全部使用它们,否则我可以单独使用它们。

So I have my data, and I pass it to my view. 所以我有了我的数据,并将其传递给我的视图。 My view loops through each establishment and displays various data in a table row. 我的视图遍历每个机构,并在表行中显示各种数据。

At present I use if.. else statements to discern if a value is empty for example. 目前,例如,我使用if .. else语句来判断一个值是否为空。 So if an establishment has not had its features added yet I use: 因此,如果企业尚未添加其功能,则可以使用:

if(!empty($features['feature1'])){//DO STUFF e.g output 'YES'}

Anyway, my views code is no getting rather long and complicated because essentially for each and every key of each array returned using get_all I am using an if..else statement to output a "-" if it is not set. 无论如何,我的视图代码不会变得很冗长和复杂,因为从本质上讲,对于使用get_all返回的每个数组的每个键,如果没有设置,我都会使用if..else语句来输出“-”。

It works, it just seems repetitive. 它的工作原理,似乎只是重复。

The work around I thought of is to simply set a default array whereby everything is by default set to "-", then if the data does exist it is overwritten, but then I just have to write/initiate a large default array.. 我想到的解决方法是简单地设置一个默认数组,其中所有默认情况下都设置为“-”,然后如果数据确实存在,则将其覆盖,但是我只需要编写/启动一个大的默认数组。

So my question is not a life threatening one, nor is it particularly hard.. I am simply curious as to how one achieves such functionality without ugly code. 因此,我的问题不是威胁生命的问题,也不不是特别困难。.我只是好奇如何在没有丑陋代码的情况下实现这种功能。

Cheers 干杯

Maybe you can "adjust" the array in the controller, by setting its empty values to - : 也许可以通过将其空值设置为-来“调整”控制器中的数组:

$features = array_map(function($value) {
    return empty($value) ? '-' : $value;
}, $features);

without you posting your actual code i can only provide some general advice. 如果没有发布您的实际代码,我只能提供一些一般性建议。 To usually handle this, and consolidate your code to remove all the conditionals put the keys in an array. 通常要处理此问题,并合并代码以除去所有条件,然后将键放入数组中。

$keys_to_check = array('feature1', 'feature2', 'etc.....');

foreach ($keys_to_check as $key) {
  if (!empty($features[$key])) {
   // do something
  }    
}

This refactors out all those conditional statements into somethign that is more maintainable. 这会将所有这些条件语句重构为更易于维护的东西。

also in your model code when you are providing a general function get_all that calls 3 subfunctions it is extremely important to make sure that unecessary queries aren't being executed. 同样,在模型代码中,当您提供调用3 get_all函数的通用函数get_all ,确保不要执行不必要的查询也非常重要。 it seems it would be good software design to not repeat yourself, and group 3 functions into 1 but if those three functions are each executing similar queries then it is horrible for performance. 似乎最好不要重复自己的工作,并且将3个功能归为1,这是一个很好的软件设计,但是如果这三个功能各自执行类似的查询,那么性能将非常糟糕。

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

相关问题 了解嵌套的 If..Else 语句 - Understanding Nested If.. Else statements 我是否应该在PHP中使用嵌套的if ... else语句进行一系列小于比较 - Should I use nested if .. else statements for a series of less than comparisons in PHP 关于大量插入语句的PHP mySQL性能问题,我应该使用别的吗? - PHP mySQL performance issues on large series of insert statements, should I use something else? 困惑-在以下情况下,我应该使用CodeIgniter还是其他工具 - Confusion- Should I use CodeIgniter or something else for below case PHP:使用post时会同时触发if和else语句 - PHP: both if and else statements are fired when I use post 如果/ else,我应该用三元速记替换所有的if / else语句吗? (PHP) - Should I replace all my if/else statements with ternary shorthand if/else? (PHP) 如果还要使用其他语句? - How many else if statements should we use? 使用'IF .. ELSE'更改变量并将其用于'form action ...' - Using 'IF.. ELSE' to change variable and use it in 'form action…' 我应该在PHP PERFORMANCE-WISE中使用MySQL的预处理语句吗? - Should I use prepared statements for MySQL in PHP PERFORMANCE-WISE? 对于图像上传,我应该在MYSQL数据库中添加一个字段进行检查,还是仅使用PHP检查图像是否存在? - For image upload, should I add a field to the MYSQL database to check against, or simply use PHP to check if the image exists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM