简体   繁体   English

如何使用flush()或ob_flush()使表逐列而不是逐行打印

[英]How to use flush() or ob_flush() to make a table print out column by column instead of row by row

I have a table whose structure is something similar to this 我有一个表格,其结构类似于此

 <table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>

I want to be able to use PHP's flush function to print out columns by columns as the data is coming out. 我希望能够使用PHP的flush函数在数据出来时按列打印列。 So in this example I want it to print First Name, Jill, Eve, and then second column; 所以在这个例子中我希望它打印First Name,Jill,Eve,然后是第二列; Last name Smith; 姓史密斯; Jackon, and so on and so forth. 杰克,依此类推。

I don't want to wait until the data is loaded (the script takes a while and the whole point is to have it echo out while its running) and I don't want to deal with AJAX. 我不想等到数据被加载(脚本需要一段时间,整个过程就是让它在运行时回显)并且我不想处理AJAX。

The only thing I could think of was using a bunch of go to's (I know thats a terrible implementation). 我唯一能想到的就是使用一堆go(我知道这是一个糟糕的实现)。 Anyone have any thoughts? 有人有什么想法?

Firstly, you should probably speed up your script or database queries. 首先,您应该加快脚本或数据库查询的速度。 This will solve the problem without any hack. 这将解决问题,没有任何黑客攻击。

If you really want to use flush to output columns, there is a option to produce JavaScript. 如果你真的想使用flush输出列,可以选择生成JavaScript。 Browsers might execute the javascript and insert columns in your table. 浏览器可能会执行javascript并在表中插入列。 This approach is complicated and I don't recommend it . 这种方法很复杂,我不推荐它

Example: 例:

<script src="...jquery..."></script>

<table id="mytable">
    <!-- data will come later -->
</table>

<script>
    addColumnToTable = function(table, column) {
       // You need to implement this to manipulate table "mytable"
    }
</script>

// Generate in php and flush:
<script>
   var column = { "header": "Firstname", "rows": ["Jill", "Eve"] }
   addColumnToTable($('#mytable'), column)
</script>

// Generate in php and flush:
<script>
   var column = { "header": "Lastname", "rows": ["Smith", "Jackson"] }
   addColumnToTable($('#mytable'), column)
</script>

// Generate in php and flush:
<script>
   var column = { "header": "Age", "rows": ["50", "94"] }
   addColumnToTable($('#mytable'), column)
</script>

I think I came up with a pretty innovative solution. 我想我想出了一个非常创新的解决方案。 It does use ONE line of javascript, but it basically is just a manual AJAX process in PHP. 它确实使用了一行javascript,但它基本上只是PHP中的手动AJAX过程。

So right now I had one file that was running and as it was running it was outputting a table as the PHP page ran. 所以现在我有一个正在运行的文件,当它运行时,它在PHP页面运行时输出一个表。 What I did was break that up into TWO files. 我所做的是将其分解为两个文件。 This example is how you can basically erase the page (or an element on the page and reload it). 这个例子就是你如何基本上擦除页面(或页面上的元素并重新加载它)。 So you'll have page one 所以你将有第一页

    <?php

$i=0;

while($i<100){
    echo (file_get_contents("http://192.241.240.69/hakre/page2.php?var=$i"));
    flush();
    ob_flush();
    usleep(200000);
    $i++;
}

This calls to page 2 which has a javascript erase page function (could be erase a single element and reload it with the updated content (see page two code below) 这将调用具有javascript擦除页面功能的第2页(可以擦除单个元素并使用更新的内容重新加载它(请参阅下面的第2页代码)

    <?php

$var = $_GET['var'];
echo '<script type="text/javascript">'."document.body.innerHTML = ''".'</script>';
echo $var;

?>

The result would be http://192.241.240.69/hakre/page1.php - not the sexiest solution but it does use pretty much only one line of javascript and almost all PHP in conjunction with the flush function. 结果将是http://192.241.240.69/hakre/page1.php - 不是最性感的解决方案,但它确实只使用一行javascript和几乎所有PHP与flush函数一起使用。 Here is an example writing a page column by column http://192.241.240.69/hakre/Table.php 下面是按列http://192.241.240.69/hakre/Table.php编写页面列的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM