if else错误消息总是显示else语句消息

时间:2014-05-23 12:53:46

标签: php mysql arrays if-statement

我认为这是一个简单的问题。但是,我不知道如何解决这个问题。我在下面发布了我的代码。在这里,在下面的代码if else语句总是显示No Sarees。我从mysql表中得到了结果,但它总是以No Sarees文本显示。我已使用var_dump($categories)检查了此内容,并返回array(0){}。我该如何解决这个问题?

<?php if ($categories) { ?>
    <!--BOF Refine Search Result-->
    <div class="refine-search-result">
        <?php if (count($categories) <= 5) { ?>

            <?php foreach ($categories as $category) { ?>
                <div class="refine-block">
                    <p><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></p>
                </div>
            <?php } ?>

        <?php } else { ?>
            <?php for ($i = 0; $i < count($categories);) { ?>
                <?php $j = $i + ceil(count($categories) / 4); ?>
                <?php for (; $i < $j; $i++) { ?>
                    <?php if (isset($categories[$i])) { ?>
                        <div class="refine-block">
                            <p><a href="<?php echo $categories[$i]['href']; ?>"><?php echo $categories[$i]['name']; ?></a></p>
                        </div>
                    <?php } ?>
                <?php } ?>
            <?php } ?>
        <?php } ?>

        <div class="clear"></div>
    </div><!--EOF Refine Search Result-->
<?php } else {
    ?>
    <div class="refine-search-result">
        <div class="refine-block">
            <p><a href="javascript://" style="cursor:default">No Sarees</a></p>

        </div>
        <div class="clear"></div>
    </div>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

如果$categories为空数组,那么在if($categories)中,您的数组将转换为布尔值。如果它是空数组(并且你说它是)那么它将被转换为false

检查一下:

$categories = array();  //empty array

var_dump((boolean)$categories); //this will show what variable will look like after converting to boolean value

if ($categories){
  echo '$categories IS NOT empty';
}else{
  echo '$categories IS empty';
}