【问题标题】:How to loop through a list of variables and check if their values is not empty in php如何遍历变量列表并检查它们的值是否在php中不为空
【发布时间】:2019-03-18 03:35:48
【问题描述】:

美好的一天,我正在尝试检查从 ajax 传递的某些变量的值是否不为空。我已经在 jQuery (ajax) 中验证了这一点,但试图在 php 中添加一个额外的层来检查 $_POST 值是否不为空。以下是变量;

$_smChest           = wp_strip_all_tags( $_POST['send_smChest'] );
$_smSleeve          = wp_strip_all_tags( $_POST['send_smSleeve'] );
$_smShoulder        = wp_strip_all_tags( $_POST['send_smShoulder'] );
$_smJacketLength    = wp_strip_all_tags( $_POST['send_smJacketLength'] );
$_smArmHole         = wp_strip_all_tags( $_POST['send_smArmHole'] );
$_smBicep           = wp_strip_all_tags( $_POST['send_smBicep'] );
$_smStomach         = wp_strip_all_tags( $_POST['send_smStomach'] );
$_smFrontWidth      = wp_strip_all_tags( $_POST['send_smFrontWidth'] );
$_smBackWidth       = wp_strip_all_tags( $_POST['send_smBackWidth'] );
$_smWaistBand       = wp_strip_all_tags( $_POST['send_smWaistBand'] );
$_smPantsLength     = wp_strip_all_tags( $_POST['send_smPantsLength'] );
$_smSeat            = wp_strip_all_tags( $_POST['send_smSeat'] );
$_smStride          = wp_strip_all_tags( $_POST['send_smStride'] );
$_smthighs          = wp_strip_all_tags( $_POST['send_smthighs'] );
$_smKnee            = wp_strip_all_tags( $_POST['send_smKnee'] );
$_smLegOpening      = wp_strip_all_tags( $_POST['send_smLegOpening'] );
$_smJacketFit       = wp_strip_all_tags( $_POST['send_smJacketFit'] );
$_smTrouserFit      = wp_strip_all_tags( $_POST['send_smTrouserFit'] );

我看到了这个问题(PHP Check if all variables are empty) 并使用此代码检查变量的值是否不为空;

$user = wp_get_current_user();
if (!empty($_POST)) {
    foreach ($_POST as $value) {
        if (!empty($value)) {
          //Then Updated the users profile with the value and echo 1 for Ajax Response
           update_user_meta( $user->ID, 'sm_chest', $_smChest);
           update_user_meta( $user->ID, 'sm_sleeve', $_smSleeve);
           update_user_meta( $user->ID, 'sm_shoulder', $_smShoulder);
           update_user_meta( $user->ID, 'sm_jacket_length', $_smJacketLength);
           //(...)
           echo '1';
        }else {
          // echo '0' for Ajax response
          echo '0';
        }
    }
}

当我这样做时,用户配置文件得到了更新,这很好,但 ajax 响应是 11111111111111111110 而不是 1 [见下图]

我知道我可以使用这样的东西[下面的代码],但是 if 语句会很长。这就是为什么我希望使用更优化的方法

if (!empty($_smChest) && !empty($_smSleeve) && !empty($_smShoulder) && !empty($_smJacketLength) && //(...) ) {
    //Then Updated the users profile with the value and echo 1 for Ajax Response
    update_user_meta( $user->ID, 'sm_chest', $_smChest);
    update_user_meta( $user->ID, 'sm_sleeve', $_smSleeve);
    update_user_meta( $user->ID, 'sm_shoulder', $_smShoulder);
    update_user_meta( $user->ID, 'sm_jacket_length', $_smJacketLength);
    //(...)
       echo '1';
}else {
    echo '0';
}

不知道我做错了什么或者为什么我得到很多1s0

我想要实现的是;
1. 遍历每个变量的所有值,确保没有一个为空;
2. 使响应返回10,如果值分别为非空

非常感谢我可以做些什么来完成这项工作的想法。

【问题讨论】:

  • 在 for 循环中,如果为真,则在回显 1,如果为假,则回显 0。在每次迭代中,它都会将10 打印到响应的正文中。
  • 是的,我在循环中回显1 如果为真,如果为假,则为0,但在图像中看到的响应中打印了0,这意味着某处有问题。我真的很喜欢回复1 是否有/没有空值和0 如果有/有空值
  • 您是否尝试过输出值而不是 0 以查看发生了什么以及发生的位置和原因?
  • 我刚刚在您回复@Blu3 之前尝试过,将1 更改为Yes 并将0 更改为No,我得到了Yes0 的回复...不知道为什么@987654346 @已添加
  • 我的意思是尝试回显变量 post 以查看它给你什么,不要回显除了帖子的值之外的任何特定内容

标签: php ajax wordpress


【解决方案1】:

您为每个更新的项目输出 1,您可能需要决定如何生成输出,但类似。这会将值设置为'1',如果任何项目为空,则将其重置为'0'...

$user = wp_get_current_user();
$ok = '1';
if (!empty($_POST)) {
    foreach ($_POST as $value) {
        if (!empty($value)) {
          //Then Updated the users profile with the value and echo 1 for Ajax Response
           update_user_meta( $user->ID, 'sm_chest', $_smChest);
           update_user_meta( $user->ID, 'sm_sleeve', $_smSleeve);
           update_user_meta( $user->ID, 'sm_shoulder', $_smShoulder);
           update_user_meta( $user->ID, 'sm_jacket_length', $_smJacketLength);
        }else {
          // echo '0' for Ajax response
          $ok = '0';
        }
    }
}
echo $ok;

所以如果任何字段为空,这将输出0。如果设置了任何项目,您可能希望将其输出为1。您需要将其设置为 0 以启动和反转逻辑(即,如果 if (!empty($value)) { 分支,则设置 $ok

第三种选择是执行逻辑来检查字段是否设置了值,但将更新逻辑移动到第二个循环中,该循环仅在所有字段未检查为非空时才被调用。

【讨论】:

  • 我会确保所有项目都有一个值,因为所有值都是必需的。所有变量都有一个输出值为1,如果所有18 个变量中缺少一个值,则输出应为0。这是我的目标@Nigel Ren
  • 如果所有字段都有值,上面的代码只输出1。如果有空则输出 0。
  • 我得到10(而不仅仅是1),所以我尝试将示例中的1更改为Yes,将0更改为No,当我测试它时我得到了YES0 作为回复。不确定0 来自哪里?
  • 这是全部代码还是之后还有更多内容?
  • 作为一个快速测试 - 在 echo $ok; 行之后添加 exit;
猜你喜欢
  • 1970-01-01
  • 2019-02-23
  • 2023-03-19
  • 1970-01-01
  • 2021-01-22
  • 2020-07-07
  • 2019-04-09
  • 1970-01-01
  • 2021-05-12
相关资源
最近更新 更多