[英]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.