PHP 运行 1/2 没有正确验证的 IF 语句

标签 php html css mysql

简单说明一下,我知道我的 PHP 代码很乱,而且我知道我可以用数组修复很多问题。

因此此页面用于显示存储在多个表中的车辆详细信息,记录将等于 1 或 2。1 表示有此功能,2 表示没有。然后在我稍后的样式中,我检查它是否等于 1,如果等于,则显示一些文本,以便用户可以看到车辆具有哪些功能。我的样式在文本旁边打印了一个蓝色的小勾,只是为了看起来不错。如果车辆具有功能,它只会打印此勾号,但由于某种原因,它会随机打印勾号。它也不匹配,所以在我的代码中,如果车辆没有 5 个东西,那么它可能只打印 1 个或 2 个空白勾号,它不一致,我不明白为什么!

Image of the affected area

初始 PHP 代码

//Fetch Category - Most Popular
$queryThree = "SELECT * FROM categoryMostPopular WHERE vehicleReg = :reg";
$stmt = $conn->prepare($queryThree);
$stmt->bindValue(':reg', $vehicleReg);
$stmt->execute();
$category1 = $stmt->fetch(PDO::FETCH_ASSOC);

//Put Results Into Variables
$mostPopularReg = $category1['vehicleReg'];
$airCon = $category1['airCon'];
$bluetooth = $category1['bluetooth'];
$climateControl = $category1['climateControl'];
$satNav = $category1['satNav'];
$commsPack = $category1['commsPack'];
$climateZonex2 = $category1['climateZonex2'];
$dvdPlayer = $category1['dvdPlayer'];
$fullLeather = $category1['fullleather'];
$halfLeather = $category1['halfLeather'];
$ipodConnectivity = $category1['ipodConnectivity'];
$manufacturersDirect = $category1['manufacturersDirect'];
$memorySeats = $category1['memorySeats'];
$panarRoof = $category1['panarRoof'];
$elecFoldingMirrors = $category1['elecFoldingMirrors'];
$rainSenseWipers = $category1['rainSenseWipers'];
$xenonHeadlights = $category1['xenonHeadlights'];

我没有发布所有初始PHP代码,主要是因为我有12个类别与上面相同,所以代码会很多。

检查 IF 变量设置为 1 或 2,然后显示刻度和文本

<div class="col-sm-6">
    <ul class="fa-ul list-unstyled">
        <?php
        if ($alcantaraLeather == 1) { echo "<li><i class='fa-li fa fa-check'></i>Alcantara Leather</li>"; }
        if ($clothUpholstery == 1) { echo "<li><i class='fa-li fa fa-check'></i>Cloth Upholstery</li>"; }
        if ($velourTrim == 1) { echo "<li><i class='fa-li fa fa-check'></i>Velour Trim</li>"; }
        if ($fullLeather == 1) { echo "<li><i class='fa-li fa fa-check'></i>Full Leather</li>"; }
        if ($halfLeather == 1) { echo "<li><i class='fa-li fa fa-check'></i>Half Leather</li>"; }
        if ($elecFoldingMirrors == 1) { echo "<li><i class='fa-li fa fa-check'></i>Electric Folding Mirrors</li>"; }
        if ($rainSenseWipers == 1) { echo "<li><i class='fa-li fa fa-check'></i>Rain Sense Wipers</li>"; }
        if ($xenonHeadlights == 1) { echo "<li><i class='fa-li fa fa-check'></i>Xenon Headlights</li>"; }
        if ($electricSeats == 1) { echo "<li><i class='fa-li fa fa-check'></i><li><i class='fa-li fa fa-check'></i>Electric Seats</li>"; }
        if ($frontCentreArmrest == 1) { echo "<li><i class='fa-li fa fa-check'></i><li><i class='fa-li fa fa-check'></i>Front Centre Armrest</li>"; }
        if ($headRestraints == 1) { echo "<li><i class='fa-li fa fa-check'></i><li><i class='fa-li fa fa-check'></i>Head Restraints</li>"; }
        if ($heatedSeats == 1) { echo "<li><i class='fa-li fa fa-check'></i><li><i class='fa-li fa fa-check'></i>Heated Seats</li>"; }
        ?>
    </ul>
</div> <!-- end .col-sm-6 -->

如果有人能看一看并了解为什么会出现随机刻度,我将不胜感激。

After making adjustments to the code, as mentioned by another user. This is my result

最佳答案

我认为你这里有一些可以改进的地方。

  1. 在编程中当您处理“全有或全无”条件( bool 值)时,您应该使用 0 表示否(或假),使用 1 表示是(或真)。

  2. 你有两个 <li>在某些行中,例如“电动座椅”。

  3. if如果车辆没有该功能,您拥有的语句将隐藏整个文本。您只需将 if 语句包裹在复选标记周围。
  4. 最后,为了使代码可读,我建议使用 echo 速记 <?= $variable; ?>

将这些建议应用到您的代码后,您会得到...

<div class="col-sm-6">
    <ul class="fa-ul list-unstyled">
        <?php
        $checkMark = "<i class='fa-li fa fa-check'></i> "; // Leave the extra trailing space in this string.
        ?>
        <?php if($alcantaraLeather == 1): ?><li><?= $checkMark; ?>Alcantara Leather</li><?php endif; ?>
        <?php if($clothUpholstery == 1): ?><li><?= $checkMark; ?>Cloth Upholstery</li><?php endif; ?>
        <?php if($velourTrim == 1): ?><li><?= $checkMark; ?>Velour Trim</li><?php endif; ?>
        <?php if($fullLeather == 1): ?><li><?= $checkMark; ?>Full Leather</li><?php endif; ?>
        <?php if($halfLeather == 1): ?><li><?= $checkMark; ?>Half Leather</li><?php endif; ?>
        <?php if($elecFoldingMirrors == 1): ?><li><?= $checkMark; ?>Electric Folding Mirrors</li><?php endif; ?>
        <?php if($rainSenseWipers == 1): ?><li><?= $checkMark; ?>Rain Sense Wipers</li><?php endif; ?>
        <?php if($xenonHeadlights == 1): ?><li><?= $checkMark; ?>Xenon Headlights</li><?php endif; ?>
        <?php if($electricSeats == 1): ?><li><?= $checkMark; ?>Electric Seats</li><?php endif; ?>
        <?php if($frontCentreArmrest == 1): ?><li><?= $checkMark; ?>Front Centre Armrest</li><?php endif; ?>
        <?php if($headRestraints == 1): ?><li><?= $checkMark; ?>Head Restraints</li><?php endif; ?>
        <?php if($heatedSeats == 1): ?><li><?= $checkMark; ?>Heated Seats</li><?php endif; ?>
    </ul>
</div> <!-- end .col-sm-6 -->

关于PHP 运行 1/2 没有正确验证的 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55165194/

相关文章:

html - 不为其他人加载的 Css 文件

html - 表格在其他浏览器中显示不佳

java - 如何列出 Linux 的所有 phpmyadmin 用户/密码

PHP 按图像搜索 Google cURL 返回 302 已移动

javascript - Bootstrap 日期时间选择器不工作

Javascript获取img的宽度和高度并将其放入另一个类的css中

asp.net - 在哪里可以找到标准 asp.net 控件背后的 CSS?

javascript - HTML 字段包含值时显示为空

php - laravel 5 ->getRealPath() 不显示正确的值

css 居中的 div