<分区>
要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预期结果。另请参阅:Stack Overflow question checklist
关闭 9 年前。
<分区>
要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预期结果。另请参阅:Stack Overflow question checklist
关闭 9 年前。
在我的网站上,我有常见问题页面,用户可以在其中添加问题和图片
<div class="FAQ container">
<div class="content">text text text</div>
<div class="pic"><img></div>
</div>
如果用户仅向问题添加文本,则 div .class 宽度应为 100% 如果用户添加文本和 img,则 2 个 div 应该各占 50%, 彼此相邻, 我该怎么做?
最佳答案
我假设您正在使用 MySQL 这样的数据库。我还假设您的数据库查询返回以下字段question、answer 和_image_path_。
PHP
$faq_html = '<div class="faq %s"><div class="content">Q: %s<br>A: %s</div>%s</div>';
while ( $row = $result->fetch_object() ) :
$container_class = 'no-image';
$faq_img_html = '';
if ( ! empty( $row->image_path ) ) :
$container_class = 'has-image';
$faq_img_html = sprintf( '<div class="pic"><img src="%s" /></div>', $row->image_path );
endif;
$faqs .= sprintf( $faq_html, $container_class, $row->question, $row->answer, $faq_img_html );
endwhile;
echo '<div class="faqs-container">' . $faqs . '</div>';
对于每个单独的常见问题解答,输出将在 DIV 包装器上具有 .no-image
或 .has-image
类。
HTML
<div class="faq no-image">
<div class="content">
Q: text text text? <br>
A: text text text
</div>
</div>
<div class="faq has-image">
<div class="content">
Q: text text text? <br>
A: text text text
</div>
<div class="pic"><img src="your-img.jpg" /></div>
</div>
一旦你有了它,只需撒上一点 CSS。
.no-image .content { width: 100%; }
.has-image .content,
.has-image .pic { width: 50%; }
关于php - 我如何使用 php 动态更改 html div 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18439859/
相关文章:
php - 在 Windows 上使用 PHP 获取共享驱动器列表?
php - 在域 vhost.conf 中设置 php ini 设置
javascript - 文件下载命中计数器 PHP、MYSQL
javascript - 如何在 WordPress 中根据视口(viewport)大小提供不同大小的图像?
javascript - 将 Angular-Material 中的多个选定元素堆叠在一起?
javascript - 使用 vanilla JavaScript 将事件绑定(bind)到动态创建的 HTML 元素 [无 jquery]
html - Flexbox 作为 Firefox 和 Opera 上表格的替代品