dongzen2675 2019-03-14 14:34
浏览 68
已采纳

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

Just a quick note, I understand my PHP code is messy and I know I could fix a lot of it with an array.

So this page is used to display vehicle details that are stored in multiple tables, the record will either equal 1 or 2. 1 being Yes it has this feature and 2 being no it doesn't. Then in my styling later on I check IF it equals 1 and if it does, then display some text so the user can see what features the vehicle has. My styling prints a little blue tick next to the text just to look nice. It only prints this tick if the vehicle has a feature, but for some reason it's printing the tick randomly. It doesn't match either, so in my code if there is 5 things the vehicle doesn't have then it might only print 1 blank tick or 2, it's not consistent and I can't figure out why!

Image of the affected area

Initial PHP Code

//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'];

I haven't posted all the Initial PHP code mainly because i've got 12 categories the same as the above, so it would be a lot of code.

Check IF variable is set to 1 or 2, then display tick and text

<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 -->

I'd really appreciate it if someone can have a look and see why the random ticks are appearing.

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

  • 写回答

2条回答 默认 最新

  • doushouj966020 2019-03-14 15:04
    关注

    I think you are a few things that could be improved here.

    1. In programming When you are dealing with an "all or nothing" condition (boolean values), then you should use 0 for NO (or FALSE) and 1 for YES (or TRUE).

    2. You have two <li> in some of the lines like the "Electric Seats" one.

    3. The if statements that you have are going to hide the entire text if the vehicle does not have that feature. You need to wrap the if statement only around the check mark.
    4. Finally, to make the code for readable, I am suggesting using the echo shorthand <?= $variable; ?>

    After applying those suggestions to your code, you get this ...

    <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 -->
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题