0

I am sure this is a simple principle of PHP, however it is one I am yet to learn. In principle the code works:

<?php
    for($i=0;$i<count($photos);$i++){
        if($photos[$i]->image != ""){
            if(JPATH_ROOT.'/images/'.$row->id.'/medium/'.$photos[$i]->image){
                if(!$photocount) {
                    $photocount = $photocount + 1;
                    $photoclass = "property_photo_main property_photo_main_" . $photocount; // First Photo Class
                }
                ?>
                <img src="<?php echo JURI::root()?>images<?php echo $row->id;?>/medium/<?php echo $photos[$i]->image?>" class="<?php echo $photoclass; ?>" alt="<?php echo $photos[$i]->image_desc;?>" title="<?php echo $photos[$i]->image_desc;?>"/>
                <?php
            }
        }
    }
?>

That outputs the images correctly, however the "Photo Count" does not increase and thus each photo gets the "First Photo Class" (property_photo_main_1). I fully appreciate that the problem here is because the count is not within the loop to print each photo, but as that is directly before that image output, where is the loop, and how can I implement that the count increases?

The HTML Output is:

                <img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100282240dc03c72cb44ce05eb9e56021d0c05.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
                                    <img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100283f9f748ca556070c2d09553298dc26d8f.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
                                    <img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100283b280e25f329d8cf1518bda4700b07765.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
                                    <img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100283c801f9afb73308c7fd77a77ea00129bb.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
                </div>
4

1 回答 1

1

$photocount如果超出,你永远不会增加。你也永远不会重置$photoclass

foreach ($photos as $photo) {
    if (!empty($photo->image)) {
        if (JPATH_ROOT.'/images/'.$row->id.'/medium/'.$photo->image) {

            if (!isSet($photocount))
                $photocount = 1;
            else
                $photocount++;

            $photoclass = "property_photo_main property_photo_main_" . $photocount;
            //HTML...
        }
    }
}

这种方式$photoclass在每次迭代中都会重置,$photocount如果已设置,也会增加 1。我还冒昧地使用了 PHP 的内部结构,如foreachor empty,您可以阅读手册中的用法。

由于JPATH_ROOT.'/images/'.$row->id.'/medium/'.$photo->image没有逻辑并且不是空/假,它总是会返回真,也许你打算使用类似的东西file_exists

使用!$photoclass而不是!isSet($photoclass)会抛出未定义的变量通知。

于 2013-11-10T18:34:29.883 回答