dongliang1865 2013-08-26 08:32
浏览 62
已采纳

我怎么能用PHP动态改变html div的宽度? [关闭]

in my website i have FAQ page which the users can add questions and images

<div class="FAQ container">
<div class="content">text text text</div>
<div class="pic"><img></div>
 </div>

if the user adds only text to the question then div .class should be 100% width if the user adds text and img the 2 divs should be 50% each, next to each other, how can i do it?

  • 写回答

3条回答 默认 最新

  • dongnan4571 2013-08-26 09:22
    关注

    I'm going to assume you are using a database like MySQL. I'm also going to assume that your your database query returns the following fields question, answer and _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>';
    

    The output would either have a class of .no-image or .has-image on a DIV wrapper for each individual FAQ.

    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>
    

    Once you have that just sprinkle in a little CSS.

     .no-image .content { width: 100%; }
     .has-image .content,
     .has-image .pic { width: 50%; }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度