简体   繁体   English

如何在 PHP 条件语句中定义 HTML 类

[英]How to define HTML classes in PHP conditional statements

Here I am trying to define HTML classes inside PHP if else statements.在这里,我试图在 PHP if else语句中定义 HTML 类。 For example:例如:

<?php if (condition 1) { ?>
<span class = 'class1'>Line 1</span>

<?php } else if (condition 2) { ?>
<span class = 'class2'>Line 2</span>

<?php } else { ?>
<span class = 'class3'>Line 3</span>

<?php } ?>

Then I can apply custom changes to different classes.然后我可以将自定义更改应用于不同的类。 Thanks for comments the error is because of syntax errors.感谢您的评论错误是由于语法错误。 However, using this style will not help me hide those warning info before any users' input.但是,使用这种样式不会帮助我在任何用户输入之前隐藏这些警告信息。 For example, if condition 1 is $_POST['name'] == NULL , which meant to remind users to input their names but by default $_POST['name'] is null.例如条件1是$_POST['name'] == NULL ,意思是提醒用户输入他们的名字,但默认情况下$_POST['name']是null。 This is where I try the second style:这是我尝试第二种风格的地方:

<span class = "<?= condition 1 ? "class 2": "class 1";?>">Some lines</span

but do not know how to add multiple conditions.但不知道如何添加多个条件。 For example, sometimes users' input is accepted but not in the desired format.例如,有时用户的输入被接受但不是所需的格式。 Then I need to check if the input exists and whether or not is in the desired format.然后我需要检查输入是否存在以及是否为所需的格式。 Hints to either one of my attempts will be helpful.对我的任何一项尝试的提示都会有所帮助。

I dont get it but u mix them as much as u like.我不明白,但你可以随心所欲地混合它们。

if (condition 1 and condition 2 and !condition 3) {
      //do something
} else if ((condition 1 or condition 2) and !condition 3) {
    if (condition 1) {
        //do something
    }
    if (condition 2) {
        //do something
    }
} else if (condition 3 and !condition 1 and !condition 2) {
    //do something
} else if (condition 3 and condition 1) {
    //do something
} else if (condition 3 and condition 2) {
    //do something
} else {
    //do something
}
     

but u need to be carefull some condition can be true before code gets to them.但是在代码到达它们之前,你需要小心一些条件可能是真的。

Here is a simple edit user checks这是一个简单的编辑用户检查

$id = $data->user;
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$pass = filter_input(INPUT_POST, 'lozinka', FILTER_SANITIZE_STRING);
$activeStatus = filter_input(INPUT_POST, 'activeStatus', FILTER_SANITIZE_STRING);
$klient_id = filter_input(INPUT_POST, 'klient_id', FILTER_SANITIZE_STRING);


if (empty($username) || empty($email) || empty($klient_id)) {
    echo '<span class="alert alert-danger child">' . lg_fill_all_fields . '</span></div>';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo '<span class="alert alert-danger child">' . lg_invalid_email . '</span></div>';
} elseif (strlen($username) < 5) {
    echo '<span class="alert alert-danger child">' . lg_minimum_5_characters_for_username . '</span></div>';
} elseif (strlen($klient_id) != 5) {
    echo '<span class="alert alert-danger child">' . lg_5_characters_for_client_id . '</span></div>';
} elseif (empty($pass)) {
    $query = 'UPDATE wLogin_users SET klient_id = "' . $klient_id . '", active = "' . $activeStatus . '", username = "' . $username . '", email = "' . $email . '" WHERE id = "' . $id . '"';
    $data->editUser($query);
    echo '<span class="alert alert-success child" id="msg" data-id="99">' . lg_changed_successfully . '</span>';
} elseif (!empty($pass)) {
    if (strlen($pass) < 7) {
        echo '<span class="alert alert-danger child">' . lg_minimum_8_characters_for_password . '</span>';
    } elseif (strlen($pass) > 26) {
        echo '<span class="alert alert-danger child">' . lg_maximum_25_characters_for_password . '</span>';
    } else {
        $password = password_hash($pass, PASSWORD_BCRYPT);
        $query = 'UPDATE wLogin_users SET klient_id = "' . $klient_id . '", active = "' . $activeStatus . '",  username = "' . $username . '", email = "' . $email . '", password = "' . $password . '" WHERE id = "' . $id . '"';
        $data->editUser($query);
        echo '<span class="alert alert-success child" id="msg" data-id="99">' . lg_changed_successfully . '</span>';
    }
}

Your original code block is missing a closing brace您的原始代码块缺少右大括号

<?php if (condition 1) { ?>
<span class = 'class1'>Line 1</span>

<?php } else if (condition 2) { ?>
<span class = 'class2'>Line 2</span>

<?php } else { ?>
<span class = 'class3'>Line 3</span>

<?php }       // Add this 

See https://3v4l.org/uXv8Hhttps://3v4l.org/uXv8H

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM