简体   繁体   中英

show/hide table in PHP code

I need to hide a table using if condition inside my following PHP code. I'm trying to use CSS for this but it seems CSS cannot read the if condition and it always runs and overrides.I want to implement it without taking table creation tags inside the if condition. Thanks

<?php
$arr=array(array(1,2,3,4,5),array(6,7,8,9));
foreach ($arr as $val)
{
    echo '<table border="1" id="table1" align="center"   style="float:center;">';
    for($i=0;$i<sizeof($val);$i++)
    {
        echo '<tr><td>'.$val[$i].'</td></tr>';
        if($val[$i]>5)
        {?>
            <style>
            #table1 { visibility:hidden; }
            </style>
            <?php
        }
        else
        {?>
            <style>
            #table1 { visibility: visible !important;}
            </style>
            <?php
        }
    }
    echo'<br/>';
    echo '</table>';
}
?>

 <?php $arr = array(array(1,2,3,4,5), array(6,7,8,9)); $id = 1; ?> <html> <head> <style type="text/css"> .hidden { visibility: hidden; } </style> </head> <body> <?php foreach($arr as $val) : for($i = 0; $i < sizeof($val); $i++) : if($val[$i] > 5) : $hidden = true; else : $hidden = false; endif; endfor; ?> <table border="1" id="table' . $id++ . '" align="center" class="<?php if($hidden) : ?>hidden<?php endif; ?>"> <?php for($i = 0; $i < sizeof($val); $i++) : ?> <tr><td><?php echo $val[$i]; ?></td></tr> <?php endfor; ?> <br/> </table> <?php endforeach; ?> </body> </html> 

I'm not completely sure why I am attempting to answer this. I'm not even sure which table you're trying to hide. You're also not creating a well defined relationship between the data in one table and the table that should be hidden based on the value of that data.

Your inline CSS isn't going to work as you're using the same ID multiple times. You can't do:

#table1 { visibility: hidden; }
#table1 { visibility: visible !important; }

The browser will apply the last rule to both tables. Instead use a CSS class that you generate in your loop.

.table-0 { visibility: hidden; }
.table-1 { visibility: visible !important; }

Something like this:

<?php

$arr = array( array( 1, 2, 3, 4, 5 ), array( 6, 7, 8, 9 ) );
$c   = 0;
foreach ( $arr as $val ) {

    $tClass = 'table-' . $c;

    echo '<table border="1" class="' . $tClass . '" align="center"   style="float:center;">';

    for ( $i=0; $i < sizeof( $val ); $i++ ) {

        echo '<tr><td>' . $val[ $i ] . '</td></tr>';

        if ( $val[ $i ] > 5 ) {            
            ?>
            <style>.table-0 { visibility:hidden; }</style>
            <?php
        }

    }

    echo '<br>';
    echo '</table>';

}

?>

Though this will get you what you want, there's still a lot to be desired. Mainly not injecting the CSS where you are - you're going to output a <style> for each value in the second array. Also, I'm pretty sure <style> is not an allowed child of <table> , the align and border attributes are depreciated and there is no float: center; .

I'm also wondering if you meant to use visibility: hidden; vs display: none; .

Consider doing something along these lines, could still use some improvement but better. We're not outputting more <style> than we need to.

<?php

$arr    = array( array( 1, 2, 3, 4, 5 ), array( 6, 7, 8, 9 ) );
$c      = 0;
$toHide = '';

foreach ( $arr as $val ) {

    $tClass = 'table-' . $c;

    echo '<table border="1" class="' . $tClass . '">';

    for ( $i=0; $i < sizeof( $val ); $i++ ) {

        echo '<tr><td>' . $val[ $i ] . '</td></tr>';

        if ( $val[ $i ] > 6 ) {
            $toHide = $tClass;
        }

    }

    echo '<br>';
    echo '</table>';

}

echo '<style>.' . $toHide . ' { visibility: hidden; }</style>';

?>

Some improvements could be to convert $toHide to an array and push each class into the array and then implode later if you had to handle multiple classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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