0

I wrote some code to display a html table with PHP. It reads data from a file and checks whether the value is true or false.

The problem is the ugly appearance of the table at the end of the function:

True   False
blue
       Red
       Red
blue

I would like to display the table like this:

True   False
blue   Red
blue   Red

Also it displays the outcome before the function is finished c.q flush(); Here is the code:

?>
<table>
    <tr>
        <th>True</th>
        <th>False</th>
    </tr>
    <?php foreach ($trimmed_array as $value) { ?>
        <tr>
            <?php if (check($value) == 0) { ?>
                <td><?php print $value ?></td>
                <td> </td>
            <?php   
            } else { ?>
                <td> </td>
                <td><?php print $value ?></td>
            <?php
            }
    sleep(1);
    ob_flush();
    flush();
    } ?>
</table>

I realize this code outputs the table like the first example but I can`t seem to figure out how to change it to fit the second example?

1
  • you should add a </tr> and probably <tbody> and <thead> tags too Commented Sep 10, 2015 at 13:48

2 Answers 2

3

Your code creates one table row per item in the array, so there will always be one blank column per row.

In the 'table' that you would like to display, the items in each row aren't related to each other.

So really you're creating 2 lists here, not a table as such.

I'd do something like this

<?php

$all_values = array('true' => array(), 'false' = array());
foreach($trimmed_array as $value){
  if (check($value) == 0){
    $all_values['true'][] = $value;
  }else{
    $all_values['false'][] = $value;
  }
}

foreach($all_values as $name => $values){
?>

<ul class="<?php print $name;?>">
  <?php 
  foreach($values as $value){
  ?>
    <li><?php print $value; ?></li>
  <?php
  }
  ?>
</ul>

<?php 
}
?>

You can then arrange your lists side by side with CSS

Sign up to request clarification or add additional context in comments.

5 Comments

This looks great but it`s a function that could take several hours (simplified example) and I also want the output to display before the function ends. Do you have any suggestion where I could implement a flush to print the output before the function is finished?
I would go with this best. Tables suck.
I would let Javascript do the HTML rendering. Would be so much easier.
you could put an ob_flush() / flush() in the foreach($values as $value){ loop as you did in your code, but obv as one list gets rendered first, if you do have giant lists then you might be waiting a while for the second one. how big are you expecting the lists to be??
The function check(); -> checks a domain name to be available or not, this takes about 2-4 sec. If the list is filled with (for example) 1000 domains, it takes approximately 40 minutes to execute.
3
$true = array();
$false = array();

foreach ($trimmed_array as $value) {
    if (check($value) == 0) {
        $true[] = $value;
    } else {
        $false[] = $value;
    }
}

for ($i = 0; $i < max(count($true), count($false)); $i++) {
    echo '<tr>
        <td>' . (isset($true[$i]) ? $true[$i] : '') . '</td>
        <td>' . (isset($false[$i]) ? $false[$i] : '') . '</td>
    </tr>';
}

8 Comments

I was just about to post something like this. Dang it!
:) I was faster.. next time :)
Not a big fan of the continue statement though... best using just an else for readability, especially for less experienced coders
@CodeGodie why you removed the arrays assignments?
@MateiMihai because you do not need them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.