[英]While loop test alternative
I have some really ugly code and I need help to clean it up. 我有一些非常丑陋的代码,需要帮助来清理它。 I'm sure there must be a better way.
我相信肯定有更好的方法。 I have a $_POST variable with entries start_doy0, start_doy1, start_doy2 etc.
我有一个$ _POST变量,其中包含条目start_doy0,start_doy1,start_doy2等。
The later entries my not be filled and I need to find up to what doy they are filled. 以后的条目我没有填写,我需要找出他们填写什么。 I cant start with $completed_index = -1 because there is no start_doy-1.
我无法以$ completed_index = -1开始,因为没有start_doy-1。
#Assume at least one line full, index 0.
$completed_index = 0;
while (!empty($_POST['start_doy'.$completed_index]))
{
$completed_index++;
}
# $_POST['start_doy'.$completed_index] was empty, decrement $completed_index.
$completed_index--;
Thnks, Matt Thnks,马特
You can remove some some brackets and change it to isset
, but other then that, the code looks pretty clean to me: 您可以删除一些括号并将其更改为
isset
,但isset
,代码对我来说看起来很干净:
$completed_index = 0; # assume at least one line full, index 0.
while(isset($_POST['start_doy'.$completed_index])) $completed_index++;
$completed_index--; # $_POST['start_doy'.$completed_index] was empty, decrement $completed_index.
you can use the "count" function like this : 您可以像这样使用“计数”功能:
$completed_index = count($_POST);
and then remove the other elements. 然后删除其他元素。 for example if you have two other element do this :
例如,如果您还有其他两个元素,请执行以下操作:
$completed_index -=2;
There are a couple of solutions, depending on how much you can change the calling code, and whether the start_doy fields are guaranteed to be filled in order (ie, user can't fill in start_doy9, but not start_doy2). 有两种解决方案,具体取决于您可以更改调用代码的数量以及是否保证按顺序填充start_doy字段(即,用户不能填写start_doy9,而不能填写start_doy2)。
Option 1: Change your HTML so that the form fields are submitted with array syntax, like this: 选项1:更改HTML,以便使用数组语法提交表单字段,如下所示:
One: <input type="text" name="start_doy[]"/><br/>
Two: <input type="text" name="start_doy[]"/><br/>
On the PHP side, this will be converted to an array, so your processing could be something like this: 在PHP方面,这将被转换为数组,因此您的处理可能如下所示:
<?php
$completed_indexes = array();
if(isset($_POST['start_doy']))
{
foreach($_POST['start_doy'] as $key => $start_doy)
{
if(false == empty($start_doy))
$completed_indexes[] = $key;
}
}
echo count($completed_indexes);
?>
Option 2: Or if you can't change the HTML, or prefer this solution, you could iterate the array and check the key matches what you want: 选项2:或者,如果您无法更改HTML,或者更喜欢此解决方案,则可以迭代数组并检查键是否符合您的要求:
<?php
$completed_indexes = array();
foreach($_POST as $key => $value)
{
if(substr($key,0,9) == "start_doy" && false == empty($value))
$completed_indexes[] = $key;
}
echo count($completed_indexes);
?>
Algo 阿尔戈
$clean_post = array_filter( $_POST );
then you can simply use all post values 那么您可以简单地使用所有帖子值
foreach ($clean_post as $doy => $value) {
...
}
only the non-empty values will remain in $clean_post
(and this is done faster in C by a PHP function). 只有非空值会保留在
$clean_post
(这在PHP中通过PHP函数可以更快地完成)。
-- -
In you current algo: 在您当前的算法中:
0
as well 0
Algo 阿尔戈
$completed_index = -1;
do {
$completed_index++;
} while ( ! empty($_POST['start_doy' . $completed_index]));
result: $completed_index
is the number of completed indexes (last index is $completed_index-1
or -1
if there is none) 结果:
$completed_index
是已完成索引的数目(最后一个索引是$completed_index-1
或-1
如果没有索引))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.