简体   繁体   English

在PHP代码中显示/隐藏表

[英]show/hide table in PHP code

I need to hide a table using if condition inside my following PHP code. 我需要在下面的PHP代码中使用if条件隐藏表。 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. 我正在尝试为此使用CSS,但似乎CSS无法读取if条件,并且它始终运行并覆盖。我想在不将表创建标签置于if条件内的情况下实现它。 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. 您的内联CSS无法使用,因为您多次使用相同的ID。 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. 而是使用您在循环中生成的CSS类。

.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. 主要是不将CSS注入您所在的位置-您将为第二个数组中的每个值输出一个<style> 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; 另外,我很确定<style>不是<table>的允许子级, alignborder属性已弃用,并且没有float: center; .

I'm also wondering if you meant to use visibility: hidden; 我也想知道您是否打算使用visibility: hidden; vs display: none; 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. 我们输出的<style>不会超过我们需要的。

<?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. 某些改进可能是将$toHide转换$toHide数组push每个$toHide入数组,然后在以后必须处理多个类时内implode

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

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