[英]PHP If Else statement not working correctly
I have a php if else statement which checks if the query contains a string. 我有一个php if else语句,它检查查询是否包含字符串。 if it doesn't it will output a style-sheet.
如果没有,它将输出样式表。 If it contains a string it will get the users style-sheet.
如果包含字符串,它将获得用户样式表。 However it seems to only be outputting the 1st fixed style-sheet apposed to the users set one.
但是,它似乎只输出与用户设置的第一固定样式表。
<?php $user_data_load = $con->query("SELECT theme FROM users WHERE id = '".$_SESSION['user']['id']."'");?> <?php if (!isset($user_data_load) or !is_array($user_data_load) or empty($user_data_load)): ?> <link id="stylesheet" rel="stylesheet" href="css/theme_default.css" type='text/css'> <?php else: ?> <?php while($userloaded = $user_data_load->fetch_object()): ?> <link id='stylesheet' rel='stylesheet' href='<?= $userloaded->theme?>' type='text/css'> <?php endwhile;?> <?php endif;?>
Some notes to your if: 如果您有以下几点注意事项:
if (!isset($user_data_load) or !is_array($user_data_load) or empty($user_data_load)): ?>
!isset($user_data_load)
will always be false. !isset($user_data_load)
始终为false。 isset
checks if a variable is set, and since you set it yourself, it will always return true, it doesn't matter whats in that variable. isset
检查,如果一个变量被设置了,既然你自己设置它,它总是会返回true,它并不重要变量什么。
!is_array($user_data_load)
will always be true, because $con->query
never returns a array, therefore you'll always use the default stylesheet. !is_array($user_data_load)
始终为true,因为$con->query
永远不会返回数组,因此您将始终使用默认样式表。
Try it like this: 像这样尝试:
<?php
$stmt = $con->prepare("SELECT theme
FROM users
WHERE id = ?");
$stmt->bind_param('i', $_SESSION['user']['id']);
$stmt->execute();
if ($data = $stmt->get_result()->fetch_object()) {
echo '<link id="stylesheet" rel="stylesheet" href="' . $data->theme . '" type="text/css">';
} else {
echo '<link id="stylesheet" rel="stylesheet" href="css/theme_default.css" type="text/css">';
}
Every id will be 'stylesheet'. 每个ID均为“样式表”。 Take out the id to prevent rebinding.
取出ID以防止重新绑定。
<link rel='stylesheet' href='<?= $userloaded->theme?>' type='text/css'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.