dozr13344 2011-12-14 14:44
浏览 14
已采纳

PHP include()打破页面布局,直到包含的文件完成数据库查询

Consider the following:

incl.php

<div id='footer'>
  <?php
      sleep(10);
      echo "<div class='footer-float'><!-- content --></div>";
  ?>
  <div style='clear:both;'></div>
</div>

page.php

<div id='main-content'>
  <!-- normal markup structure... -->
</div>
<div id='footer-wrapper'>
  <?php include('incl.php'); ?>
</div>

I have a page (page.php) which includes a footer, the markup for which is in incl.php. The real incl.php does some wordpress database stuff to look up latest posts and display a little blurb for each one - this works fine but because there is a delay (represented here by the sleep() command) making the database connections and including the required WP files, it can break the layout of my page as it can pause for up to several seconds.

While it is waiting, the floated divs in my footer mean that the container won't stretch to fit the content already loaded until incl.php finishes executing and provides the clearing div needed to stretch the container.

What I'd like to know is this: is there a way I can get PHP to serve the markup (that is, the non-dynamic stuff) first rather than doing everything sequentially like it seems to at the moment? Otherwise, is there a better way to make this less jarring? I can't put a static height on the footer div in question as it will change depending on the content.

Any pointers much appreciated

EDIT: Just a few points for those concerned about efficiency:

This issue crops up when I'm testing with the site running locally but the database server is remote and on the other end of an unreliable and slow net connection (if you've heard about 'hurricane bawbag' that might provide a little more context) and it is not an issue I expect to crop up much if at all in the live environment.

While points on optimising and being more efficient are appreciated, I don't see how I can justify spending time worrying about a possible 2 second delay in loading some footer content that only crops up in the testing environment - and please don't make the mistake of thinking that I haven't considered performance already purely because I've said there is a delay in loading stuff (again - LOCAL site, REMOTE database server, SLOW connection).

For the purposes of this question, please assume that the delay isn't the problem, rather that the problem is how to deal with possible delays without the page looking a mess.

  • 写回答

4条回答 默认 最新

  • dqwh1209 2011-12-14 14:51
    关注

    If you want to keep the current flow without many changes you can use output buffering:

    // at the begining of your application
    ob_start();
    // rest of the application: do any kind of output
    //...
    
    // at the end, maybe at the end of the footer
    ob_end_flush();
    

    Otherwise, if you don't want to have a blank page for a few seconds, the best solution would be to use Ajax: load the page normally (maybe with a min-height of the content with a loader), and load the results afterwards.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算