PHP如果元素存在,添加宽度;如果没有,不要

时间:2010-10-14 05:58:43

标签: php if-statement indexhibit

我正在使用Index Exhibit设置一个水平滚动网站。一个部分需要有一个视频,我无法在Indexhibit CMS后端设置(它将它添加到宽div的宽度),所以我需要将它添加到PHP生成文件。基本上,它需要检查是否存在ID为vid的元素。如果是,则应将vid_widthvid_right_padding添加到最终宽度final_img_container。如果没有,它应该被排除在外。带有视频的网站可以在这里找到:http://www.allisonnavon.com/index.php?/projects/the-alka-seltzer-challenge/正如你在这里看到的,宽度是完美的,但是当加载其他页面时,每个项目的右端都有一个巨大的800个额外像素。

以下是代码:

<?php if (!defined('SITE')) exit('No direct script access allowed');

/**
* Horizontal Format
*
* Exhbition format
* Originally created for SharoneLifschitz.com
* 
* @version 1.1
* @author Vaska 
*/

$DO = new Horizontally;

$exhibit['exhibit'] = $DO->createExhibit();
$exhibit['dyn_css'] = $DO->dynamicCSS();

class Horizontally
{
    // PADDING AND TEXT WIDTH ADJUSTMENTS UP HERE!!!
    var $picture_block_padding_right = 25;
    var $text_width = 250;
    var $text_padding_right = 75;
    var $vid_width = 800;
    var $vid_padding_right = 25;
    var $final_img_container = 0; // do not adjust this one

    function createExhibit()
    {
        $OBJ =& get_instance();
        global $rs;

        $pages = $OBJ->db->fetchArray("SELECT * 
            FROM ".PX."media, ".PX."objects_prefs 
            WHERE media_ref_id = '$rs[id]' 
            AND obj_ref_type = 'exhibit' 
            AND obj_ref_type = media_obj_type 
            ORDER BY media_order ASC, media_id ASC");

        if (!$pages) return $rs['content'];

        $s = ''; $a = ''; $w = 0;
        $this->final_img_container = ($rs['content'] != '') ? ($this->text_padding_right + $this->text_width + $this->vid_padding_right + $this->vid_width) : 0;

        foreach ($pages as $go)
        {
            $title = ($go['media_title'] == '') ? '' : "<div class='title'>" . $go['media_title'] . "</div>";
            $title .= ($go['media_caption'] == '') ? '' : "<div class='caption'>" . $go['media_caption'] . "</div>";

            $temp_x = $go['media_x'] + $this->picture_block_padding_right;
            $this->final_img_container += ($go['media_x'] + $this->picture_block_padding_right);

            $a .= "<div class='picture_holder' style='width: {$temp_x}px;'>\n";
            $a .= "<div class='picture' style='width: {$go[media_x]}px;'>\n";
            $a .= "<img src='" . BASEURL . GIMGS . "/$go[media_file]' width='$go[media_x]' height='$go[media_y]' alt='" . BASEURL . GIMGS . "/$go[media_file]' />\n";
            $a .= "<div class='captioning'>$title</div>\n";
            $a .= "</div>\n";
            $a .= "</div>\n\n";
        }

        $s .= "<div class='text'><div class='scroller'><span><a href='javascript:void(0);' class='prev'>&laquo;</a></span><span class='title'><a href='javascript:void(0);' class='origin'><%title%></a></span><span><a href='javascript:void(0);' class='next'>&raquo;</a></span></div><div id='img-container'>\n";
        if ($rs['content'] != '') $s .= "<div id='text'>" . $rs['content'] . "</div>\n";
        $s .= $a;
        $s .= "<div style='clear: left;'><!-- --></div>";
        $s .= "</div></div>\n";

        return $s;
    }


    function dynamicCSS()
    {
        return "#img-container { width: " . ($this->final_img_container + 1) . "px; }
#img-container #text { float: left; width: " . ($this->text_width + $this->text_padding_right) . "px; }
#img-container #text p { width: " . $this->text_width . "px; }
#img-container #vid { float: left; width: " . ($this->vid_width + $this->vid_padding_right) . "px; }
#img-container #vid p { width: " . $this->vid_width . "px; }
#img-container .picture_holder { float: left; }
#img-container .picture { /* padding-top: 10px; */ }
#img-container .captioning .title { margin-top: 12px; font-weight: bold; }
#img-container .captioning .caption { font-family:PerspectiveSansItalic;
    font-size:11px; color: #444; padding-top: 10px;}";
    }
}

1 个答案:

答案 0 :(得分:1)

在加载页面时用Javascript修复此问题是不是更好?

只需找到ID为“vid”的div并添加正确的css即可。类似的东西:

$(function() {
    $("div#vid").load(function() {
        $(this).addClass("whateverclassyouwanttoadd");
    })
}

我不是一个jquery大师,但我认为这将解决你的问题。

只是一些侧节点:我认为最好将您的演示文稿与业务逻辑分开。因此,使用样式表来标记HTML并在视图/业务逻辑中向HTML添加类。

只需2美分

祝你好运!