1

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. So in this example I want it to print First Name, Jill, Eve, and then second column; 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.

The only thing I could think of was using a bunch of go to's (I know thats a terrible implementation). Anyone have any thoughts?

Usman Shahid
  • 302
  • 1
  • 9
  • 1
    How to say this best? I think you misunderstood how the data flows from the server to the browser. HTML here will force you to transmit the tabular data row by row and not column by column. As you use HTML to exchange the data between the server and client, you can't break out of that - especially as you explicitly decline javascript usage. – hakre Jun 17 '17 at 13:49
  • Really the only way you could achieve this would be to use javascript - as @hakre sort of pointed out as PHP runs at the server before the page is sent ob_flush will not be of any use – Professor Abronsius Jun 17 '17 at 13:55
  • @hakre I'm trying to see if there is a way...if you dont have a way, that answer isnt really helpful at all – Usman Shahid Jun 17 '17 at 13:57
  • 1
    My comment might not have sounded useful to you on first read. That's why this is posted as comment and not as an answer. Then take a deep breath and come over the frustration from first read. And consider why would hakre wrote such a comment? To make me disappointed? No, not at all. So read it again and try to find out if there isn't something useful in there. If you understand why this is not possible, then you might be able to actually find a solution that works for you. Sometimes you first need to understand the negation before further progress is possible. Can you transpose? – hakre Jun 17 '17 at 14:01
  • hakre is correct. There is no way you can do this without javascript and AJAX. – Laposhasú Acsa Jun 17 '17 at 14:01
  • @hakre - I appreciate your response...but basically I was asking - "hey is this possible?" - and your response was, "I think you misunderstood how ..." basically demeaning/insulting. Obviously, I understand it enough to know this cannot work currently. But I asked because how many times has someone showed a function or method that you didn't know existed. Once more, IF someone came up with a answer here - think about what that would mean for PHP. The most rewarding things are often found in the pursuit of difficult problems - my problem with your comment was the quick dismissal – Usman Shahid Jun 17 '17 at 14:17
  • Okay, this does not work not because there wouldn't be a cool function that could do it, but because of the data-exchange format you use (HTML). This is more or less independent to PHP so no PHP function can change that. And I beg your pardon, if you know this does not work and you're reaching for an exceptional chance, then you should make that clear in your question. Otherwise other users might just not really understand what you're after. – hakre Jun 17 '17 at 14:20
  • Wishful thinking. – hakre Jun 17 '17 at 14:22
  • @hakre well in my determination to prove you wrong I think I just thought of a pretty smart solution that effectively uses almost ONLY PHP. Like a manual AJAX process - http://192.241.240.69/hakre/page1.php - SO IT CAN BE DONE with very minimal javascript (one line) and almost all PHP. I'll post the answer. – Usman Shahid Jun 17 '17 at 14:39
  • I don't say what you prove here, you definitely prove something but it's not me wrong :) Still: Can you transpose? https://stackoverflow.com/a/16919439/367456 – hakre Jun 17 '17 at 14:48
  • @hakre see my answer... – Usman Shahid Jun 17 '17 at 15:51
  • Just my last comment. Now you transfer the data multiple times. – hakre Jun 17 '17 at 15:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146949/discussion-between-usman-shahid-and-hakre). – Usman Shahid Jun 17 '17 at 15:56

2 Answers2

0

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. Browsers might execute the javascript and insert columns in your table. 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>
user3429660
  • 2,420
  • 4
  • 25
  • 41
  • Hey! I appreciate the answer...I was trying to see if there was a way to do this with PHP alone...I'm going to keep my quesiton up to see if anyone has an answer for PHP solo - if not I'll mark your as correct. Thanks for taking the time to answer though! The queries take long because they are pulling from different API's that are throttle capped – Usman Shahid Jun 17 '17 at 14:21
0

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.

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. 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)

    <?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. Here is an example writing a page column by column http://192.241.240.69/hakre/Table.php

Usman Shahid
  • 302
  • 1
  • 9